Day 3
Day 3
By
Samuel K. Opoku
Computer Science Department
Kumasi Technical University
Contents
• Overview of Stacks
• Overview of Queues
1
Videos Links
• Overview of Stacks and Queues
– https://blackboard.skopoku.org/tutorials/stackqueue.mp4
2
CONTENTS
• Overview of Stack
• Implementation of stack
• Uses of Stacks
Stack
• A stack is a linear list in which insertion and deletion are done one at a
time and only at one end of the list. This is often called Last-In-First-
Out (LIFO)
Examples:
3
Uses of Stack in Computing
1. Use in compilers for parsing arithmetic expressions when converting
them from written form (infix) into reverse polish (suffix) from which is
parenthesis-free
4
9
STACK IMPLEMENTATION
• A stack is processed by means of a vector X and a stack pointer (that is
a pointer variable T). T points to the top of the stack
• Data can be put onto the stack by incrementing the stack pointer and
transferring the data item onto the stack.
• To move data from the stack, data is transferred from the stack and
the stack pointer decremented.
11
5
INSERTION AND DELETION OF ITEMS
Let a stack with capacity n be represented by the vector X and the pointer T
TT+1 If T = 0 then
stack underflow, Stop
If T > n then Else
Overflow, Stop data item X(T)
Else TT–1
X(T) data item End if
End if
12
• In this form, the expression can be evaluated in only one scan: left to right
for Reverse Polish form and right to left for polish form
6
EXAMPLES
Convert the following infix expressions to prefix and suffix
1. C + D
Solution: prefix: +CD suffix: CD+
2. X * Y * Z
Solution: prefix: **XYZ suffix: XY*Z*
3. (A + B)/C
Solution: prefix: /+ABC suffix: AB+C/
4. A/B
Solution: prefix: /AB suffix: AB/
5. B = (D + B * C)/A
Solution: prefix: =B/+D*BCA suffix: BDBC*+A/=
MORE EXAMPLES
1. Convert the following to suffix and prefix forms
i. X = Y * (Z – Y/Z)
ii. (f – (x - a) / f )^ s
iii. K+ (R-T)/5
iv. D = (T-R)/(K-R) + M
7
Algorithm – for converting infix to suffix (1/2)
1.Get next character from input string
8. Go to 5
8
Queues and its Applications
18
CONTENTS
• Overview of Queues
• Types of Queues
• Applications of Queues in computing
19
9
OVERVIEW OF QUEUES (1/3)
• A queue is a linear list in which the items are deleted in the same
order in which they are inserted.
• A collection whose elements are added at one end (the rear or tail of
the queue) and removed from the other end (the front or head of the
queue)
20
Front of queue
New element is
added to the rear of
the queue
6-21
10
CONCEPTUAL VIEW OF A QUEUE
Removing an element
Element is removed
from the front of the
queue
6-22
11
QUEUE OPERATIONS
• Enqueue : add an element to the tail of a queue
OPERATIONS ON A QUEUE
Operation Description
6-25
12
6-26
IMPLEMENTATION OF QUEUES
• A vector and TWO pointers F (Front pointer – Pointing one location
ahead of the next element to be removed or service) and R (Rear
pointer – Pointing to the last item that was inserted)
• Empty Queue: F = R = 0
INSERTION ALGORITHM
RR+1
• 1st Item:
If R > N Then
Overflow, Stop
Else
• Overflowed (Full Queue)
X(R) data item
End If
27
13
IMPLEMENTATION OF QUEUES
• Neither overflow nor underflow occurs in the following:
DELETION ALGORITHM
If F = R Then
Underflow, Stop
• Underflow (empty queue)
Else
FF+1
End If
28
• Circular queue is used to overcome this problem. If X(1), X(2) ... X(N)
are the nodes of the vector then X(1) is made to follow X(N). Thus when
the end of the queue is reached, it is made to move to the beginning if
there is room 29
14
Conceptual Example of a Circular Queue
front
After 5
1 After 7 enqueues 1 dequeues
0 0
front
12 12
11 11
rear rear
10 10
rear
1
0
front
12
11
After 8 more enqueues
10
6-30
31
15
CIRCULAR QUEUES
• This is resolved by never filling the queue completely during insertion.
One space is left unfilled and the queue is said to be full. In such as
situation, the initial setting of the is F = R = 1 but not F = R = 0.
If R = N Then If F = R Then
R1 Underflow, Stop
Else End If
RR+1
End if If F = N Then
F1
If R = F Then Else
Overflow, Stop FF+1
Else data item X(F)
X(R) data item End If
End If
32
If F = N Then If R = F Then
F1 Underflow, Stop
Else Else
X(F) data item data item X(R)
End if End If
If F = R Then If R = 1 Then
Overflow, Stop RN
Else Else
FF+1 RR-1
End If End If 33
16
USING QUEUES: CODED MESSAGES
• A Caesar cipher is a substitution code that encodes a message by
shifting each letter in a message by a constant amount k
– If k is 5, a becomes f, b becomes g, etc.
• Example: n qtaj ofaf
– Used by Julius Caesar to encode military messages for his
generals (around 50 BC)
– This code is fairly easy to break!
34
Encoded message
n o v a n j g h l m u u r x l v
Key 3 1 7 4 2 5 3 1 7 4 2 5 3 1 7 4
k n o w l e d g e i s p o w e r
Decoded message 6-35
17
More Questions
1. Encrypt “Data Structures and Algorithms” using
i. Caesar cipher with a key of 7
ii. ROT 13
iii. Using the keys 9, 5, 7, 3
2. Decrypt
i. “SBQ” using Caesar cipher with a key of 8
ii. “evpr” using ROT 13
iii. “M mrof clfv” using the keys 4, 1, 9
36
37
18
Assignment
38
Question A
1.Convert the following expressions into polish and reverse
polish form:
i. Z = (Q - D) * (A + B) / T
ii. W = (~X) / Y * T + Q
iii. P = A+ D * (B - C) / T
iv. W = (X + Y) / (~ T) + Q
19
Question B
1. Encrypt “Kumasi Technical University” using
i. Caesar cipher with a key of 3
ii. ROT 13
iii. Using the keys 6, 5, 7, 9
2. Decrypt
i. “dvd” using Caesar cipher with a key of 7
ii. “ynaqybeq” using ROT 13
iii. “M mxzf hen” using the keys 4, 1, 9
40
Question C
1. A Stack S of size n, stack pointer T which points to first free space
in the stack. Give
(i) underflow and overflow conditions
(ii) write an algorithm to push the data item X onto the stack
41
20
HIT ANY KEY TO END DAY 3
42
21