stacks and queues
TRANSCRIPT
1 of 32
FOA
Stacks and Queues
2 of 32
Stacks
“A Stack is a special kind of list in which all insertions and deletions take place at one end, called the Top”
Other Names Pushdown List Last In First Out (LIFO)
3 of 32
Stacks
Examples:
Books on a floor
Dishes on a shelf
4 of 32
Common Operations on Stacks
1. MAKENULL(S): Make Stack S be an empty stack.
2. TOP(S): Return the element at the top of stack S.
3. POP(S): Remove the top element of the stack.4. PUSH(S): Insert the element x at the top of the
stack.5. EMPTY(S): Return true if S is an empty stack;
return false otherwise.
5 of 32
An Array Implementation of StacksFirst Implementation Elements are stored in contiguous cells of an array. New elements can be inserted to the tail of the list.
Last Element
Second ElementFirst Element
List
Emptymaxlength
top
6 of 32
An Array Implementation of Stacks
Problem with this implementation Every PUSH and POP requires moving the entire
array up and down.
1
1
2
1
2
3
1
2
7 of 32
Since, in a stack the insertion and deletion take place only at the top, so…
A better Implementation: Anchor the bottom of the stack at the
bottom of the array Let the stack grow towards the top of the
array Top indicates the current position of the
first stack element.
An Array Implementation of Stacks
8 of 32
A better Implementation:
First Element
Last Elementmaxlength
top
Second Element1
2 ..
An Array Implementation of Stacks
9 of 32
A Linked-List Implementation of Stacks
PUSH and POP operate only on the header cell and the first cell on the list.
x y .zTop
10 of 32
Queues
“A Queue is a special kind of list, where items are inserted at one end (the rear) And deleted at the other end (the front)”
Other Name: First In First Out (FIFO)
Difference from Stack:Insertion go at the end of the list, rather than the beginning of the list.
11 of 32
Common Operations on Queues
1. MAKENULL(Q): Makes Queue Q be an empty list.
2. FRONT(Q): Returns the first element on Queue Q.
3. ENQUEUE(x,Q): Inserts element x at the end of Queue Q.
4. DEQUEUE(Q): Deletes the first element of Q.5. EMPTY(Q): Returns true if and only if Q is an
empty queue.
12 of 32
A pointer Implementation of Queues
Keep two pointers: FRONT: A pointer to the first element of the
queue. REAR: A pointer to the last element of the
queue.
x y .zFront
Rear
13 of 32
A pointer Implementation of Queues
Q.front
Q.Rear
MAKENULL(Q)
.
Q.front
Q.Rear
ENQUEUE(x,Q)
.x
14 of 32
Q.frontQ.Rear
ENQUEUE(y,Q)
x .y
Q.front
Q.Rear
DEQUEUE(Q)
x .y
A pointer Implementation of Queues
15 of 32
An Array Implementation of QueuesSame as Stack:
First ElementSecond Element
Last Element
List
Emptymaxlength
top
16 of 32
An Array Implementation of Queues
No Problem in ENQUEUE
Problem with DEQUEUE:Removing the first element requires the entire queue be moved up one position in the array.
Thus, DEQUEUE takes O(n) times if the queue has length n.
17 of 32
A better Implementation:
First Element
Last Elementmaxlength
top
Second Element1
2 ..
An Array Implementation of Stacks
bottom
18 of 32
A better Implementation:
T
An Array Implementation of Queue
B
T
B
19 of 32
A Circular Array Implementation of QueuesAn even better implementation
1maxlength2
queue
Q.rear Q.front
...
..
20 of 32
The First position follows the last
The queue is found somewhere around the circle in consecutive positions
That is, a queue of length four could occupy the last two and first two positions of the array.
A Circular Array Implementation of QueuesAn even better implementation
21 of 32
ENQUEUE: Move the Q.rear pointer one position clockwise. 1
maxlength2
Q.rear Q.front
. .
..X
A Circular Array Implementation of QueuesAn even better implementation
22 of 32
DEQUEUE: Move the Q.front pointer one position clockwise. 1
maxlength2
Q.rear
. .
..
Q.frontX
A Circular Array Implementation of QueuesAn even better implementation
23 of 32
Problem with above implementation:
No way to distinguish an Empty Queue from a Completely Filled Queue.
A Circular Array Implementation of QueuesAn even better implementation
24 of 32
A Circular Array Implementation of Queues
Q.rear Q.front Q.rear Q.frontQ.rear
ab
dc
i
e
hgf
i
A Completely
Filled Queue
A Queue with
Only 1 Element
25 of 32
Q.rear Q.front Q.rear Q.frontQ.rear
ab
dc
i
e
hgf
i
A Completely
Filled Queue
An Empty Queue
DEQUEUE
A Circular Array Implementation of Queues
26 of 32
=> Although the array has maxlength elements but Queue should not grow more than maxlength - 1
Alternatively, introduce a separate bit to indicate the Queue Empty or Queue Filled status.
A Circular Array Implementation of Queues