Chapter 2 Stack and Queue
Chapter 2 Stack and Queue
Chapter 2 Stack and Queue
Table of Contents
Stack....................................................................................................................................................... 2
Queue ..................................................................................................................................................... 4
1. Linear queue................................................................................................................................ 4
Stack
Stack is an ordered collection of data in which insertion and deletion operation is performed
at only one end called the top of the stack.
Stack operation follows LIFO approach
Stack operations
o Push: Adds an item in the stack. If the stack is full, then it is said to be an Overflow
condition.
o Pop: Removes an item from the stack. The items are popped in the reversed order in
which they are pushed. If the stack is empty, then it is said to be an Underflow
condition.
Initial value of top is -1.
abstract empty(s)
STACK(elType) s;
postcondition empty == (len(s) == 0) // len(s) returns the length of s
Algorithm
Push
1. Start
2. if(top=size-1)
print “stack is full”
otherwise,
i. Increase top by 1
ii. Read data and store at top position
3. Stop
Pop
1. Start
2. if(top=-1)
print “stack is empty”
otherwise,
decrease top by 1
3. Stop
//implementation in c
struct stack
{
int data[size];
int top;
}
Queue
Queue is an ordered collection of data in which data can be inserted from one end called rear
and that can be removed from another end called front of the queue.
It follows FIFO system.
Queue operations
o enqueue: Adds an item to the queue. If the queue is full, then it is said to be an
Overflow condition.
o dequeue: Removes an item from the queue. The items are popped in the same order in
which they are pushed. If the queue is empty, then it is said to be an Underflow
condition.
1. Linear queue
o Initial condition: front=0 and rear=-1
o Algorithm
Insert
1. Start
2. if(rear=qsize-1)
print “queue is full”
otherwise,
i. Increase rear by 1
ii. Read data and store at rear position of the queue
3. Stop
Delete(without shift)
1. Start
2. if(rear<front)
print “queue is empty”
otherwise,
i. Increase front by 1
3. Stop
Delete(with shift)
1. Start
2. if(rear<front)
print “queue is empty”
otherwise,
i. shift all the elements from front+1 to rear by 1 position towards front
ii. Decrease rear by 1
3. Stop
2. Circular Queue
o Initial condition: front=0 and rear=0
o Algorithm
Insert
1. Start
2. If((rear+1) % qsize=front)
print “queue is full”
otherwise,
i. Increase rear as rear= (rear+1) % qsize
ii. Read data and store at rear position of the queue
3. Stop
Delete
1. Start
2. if(rear=front)
print “queue is empty”
otherwise,
front=(front+1) % qsize
3. Stop
3. Priority queue
o Priority Queue is an extension of queue with following properties.
o Every item has a priority associated with it.
o An element with high priority is dequeued before an element with low priority.
o Two elements have the same priority, they are served according to their order in
the queue.
o An ascending priority queue is a collection of items into which items can be inserted
arbitrarily and from which only the smallest item can be removed.
o A descending priority queue is similar but allows deletion of only the largest item.
o For example
o A stack may be viewed as descending priority queue whose elements are ordered
by the time of insertion. The element that was inserted last has the greatest
insertion-time value and is the only item that can be retrieved.
o Similarly, a queue may be viewed as ascending priority queue whose elements are
ordered by the time of insertion.
2 3
1 1 3 3 9
1+2 3*3
Ans=9
Example 2: ABC*DEF^/G*-H*+
Let A=2,B=3C=9,D=8,E=1,F=4,G=2,H=7
4
1 1 1
9 8 8 8 8 8
3 3 27 27 27 27 27 27
2 2 2 2 2 2 2 2 2
2 2
8 8 16 7
27 27 27 11 11 77
2 2 2 2 2 2 79
Ans= 79