Chapter 3 Stack and Queue
Chapter 3 Stack and Queue
30
20
10
Working of Stack
40 40 TOP
30 30
20 20
10 TOP
10
stack stack
OPERATIONS ON A STACK
A stack supports two basic operations:
1.push
2.pop
The push operation adds an element to the top of the stack and the pop operation removes the element from the
top of the stack.
1. Push Operation
The push operation is used to insert an element into the stack.
The new element is added at the topmost position of the stack.
However, before inserting the value, we must first check if the stack is full then no more insertions can be done.
If an attempt is made to insert a value in a stack that is already full, this condition is called as Stack Overflow.
PUSH OPERATION
Push 77
Size of stack
(MAX)=5
top==MAX-1 Stack is full
Stack overflow
4 4 4 4 4 4
92 top
3 3 3 3 3
88 top 3 88
2 2 2 2
80 top
2
80 2
80
1 1 1 60 top 1
60 1
60 1
60
0 0
50 top 0 50 0
50
0
50 0
50
stack Push 50 Push 60 Push 80 Push 92
top Push 88
ALGORITHM FOR PUSH OPERATION
• Step 1:
if (top==MAX-1)
{
[Check for stack full/ overflow]
Print “Stack Overflow”
GOTO Step 4
}
• Step 2: Set top=top+1 [ Increment stack top pointer by one]
• Step 4: End
2.Pop Operation
• The pop operation is used to delete the topmost element from the stack.
• However, before deleting the value, we must first check if the stack is empty and no more deletions can be done.
• If an attempt is made to delete a value from a stack that is already empty this condition is called as Stack
Underflow.
POP OPERATION
Pop
Size of stack
(MAX)=5
top==-1 Stack is Empty
Stack Underflow
4 92 top 4 4 4 4 4
3 88 3
88 top 3 3 3 3
2 80 2 80 2 80 top 2 2 2
1 60 1
60 1 60 1
60 top
1 1
0 50 0
50 0 50 0
50
0
50 top
0
• Step 4: End
Example : Show the effect of PUSH and POP operation on to the stack
of size 10. The stack contains 40, 30, 52, 86, 39, 45, 50 with 50 being at
top of the stack. Show diagrammatically the effect of:
(i) PUSH 59 (ii) PUSH 85
(iii) POP (iv) POP
(v) PUSH 59 (vi) POP
Sketch the final structure of stack after performing the above said
operations.
9 9 9 9 9
8 8 top8 85 8 8
7 59 59 top7 59 7
top7 7
top 6 50 6 50 6 50 6 50 top6 50
5 45 5 45 5 45 5 45 5 45
4 39 4 39 4 39 4 39 4 39
3 86 3 86 3 86 3 86 3 86
2 52 2 52 2 52 2 52 2 52
1 30 1 30 1 30 1 30 1 30
0 40 0 40 0 40 0 40 0 40
Initial Stack PUSH 59 PUSH 85 POP POP
9 9
8 8
top7 59 7
6 50 top6 50
5 45 5 45
4 39 4 39
3 86 3 86
2 52 2 52
1 30 1 30
0 40 0 40
PUSH 59 POP
Example : Show the effect of PUSH and POP operation on the stack of size
10.
PUSH(10)
PUSH(20)
POP
PUSH(30)
9 9 9 9 9
8 8 8 8 8
7 7 7 7 7
6 6 6 6 6
5 5 5 5 5
4 4 4 4 4
3 3 3 3 3
2 2 2 2 2
1 1 top 1 20 1 top 1 30
top0
0 top 0 10 0 10 10 0 10
Initial Stack PUSH 10 PUSH 20 POP PUSH 30
Top=-1
IMPLEMENTATION
OF STACK
USING
#include<stdio.h>
C
#include<conio.h>
int stack[100],choice,MAX,top,x,i;
void push(void);
void pop(void);
void display(void);
void main()
{
clrscr();
top=-1;
printf("\n Enter the size of STACK[MAX=100]:");
scanf("%d",&MAX);
printf("\n\t STACK OPERATIONS USINGARRAY");
printf("\n\t ");
printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT");
do
{
printf("\n Enter the Choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
CASE 4:
{
printf("\n\t EXIT POINT"); break;
}
default:
{
printf ("\n\t Please Enter a
Valid Choice(1/2/3/4)");
}
}
}
while(choice!=4);
void push()
{
if(top==MAX-1)
{
printf("\n\tSTACK is over flow");
}
else
{
printf(" Enter a value to be pushed:");
scanf("%d",&x);
top=top+1;
stack[top]=x;
}
}
void pop()
{
if(top==-1)
{
printf("\n\t Stack is under flow");
}
else
{
printf("\n\t The popped elements is %d",stack[top]);
top=top-1;
}
}
void display()
{
if(top>=0)
{
printf("\n The elements in STACK \n");
for(i=top; i>=0; i--)
printf("\n\t%d",stack[i]);
printf("\n Press Next Choice");
}
else
{
printf("\n The STACK is empty");
}
}
APPLICATIONS OF STACKS
• In this section we will discuss typical problems where stacks can be easily applied for a
simple and efficient solution.
Reversing a list
Polish notations/Evaluation of Arithmetic Expressions
Conversion of an infix expression into a postfix expression
Evaluation of a postfix expression
Conversion of an infix expression into a prefix expression
Evaluation of a prefix expression
Recursion
Tower of Hanoi
REVERSING A LIST
A list of numbers can be reversed by reading each number from an array
starting from the first index and pushing it on a stack. Once all the
numbers have been read, the numbers can be popped one at a time and
then stored in the array starting from the first index.
Example: reverse list { 1 2 3 4 5 6}
Push 6
Reverse Of list is : 6 5 4 3 2 1
Push 5
6
Push 4
6 6 5
Push 3
5 5 5 5 4
Push 2 4 4 4
4 4 4 3
Push 1 3 3 3 3 3 3 3 3 2
2 2 2 2 2 2 2 2 2
2 1
1 1 1 1 1 1 1 1 1 1 1 1
pop pop pop pop pop pop pop
REVERSING A STRING
Example: reverse string {MUMBAI} Reverse Of list is I A B M U M
Push I
Push A
I
Push B
I I A
Push M
A A A A B
Push U B B B
B B B M
Push M M M M M M M M M U
U U U U U U U U U
U M
M M M M M M M M M M M M
pop pop po pop pop pop pop
p
EVALUATION OF ARITHMETIC EXPRESSIONS
a+b-c
operands
• Infix Notation: operators are used in-between operands.
a+b
+ab
Prefix notation is also known as Polish Notation.
Prefix Postfix
ab+ Infix Expression Expression Expression
A+B +AB AB+
a-b*c
Sr.No. Operator Precedence Associativity
a + ( b * c)
2 1
Empty Empty
a a
+ + a
( + ( a
b + ( a b
Input Symbol Stack Postfix expression
* + ( * a b
c + ( * a b c
) + ( * a b c *
+ a b c *
a b c * +
CONVERT FOLLOWING EXPRESSION INTO POSTFIX FORM.
GIVE STEPWISE PROCEDURE.
a-(b+c*d)/e
4 2 1
3
Empty Empty
a a
- - a
( - ( a
b - ( a b
Input Symbol Stack Postfix expression
+ - ( + a b
c - ( + a b c
* - ( + * a b c
d - ( + * a b c d
) - ( + * a b c d * +
/ - / a b c d * +
e - / a b c d * + e
a b c d * + e / -
CONVERT FOLLOWING EXPRESSION INTO POSTFIX FORM. GIVE STEPWISE
PROCEDURE.
((A+B)*D)^(E–F)
((A+B)*D)^(E–F)
1 2 4 3
Empty Empty
( (
( ( (
( ( A
A
+ ( ( + A
Input Symbol Stack Postfix expression ((A+B)*D)^(E–F)
B ( ( + A B
) ( ( + A B +
* ( * A B +
D ( * A B + D
) ( * A B + D *
^ ^ A B + D *
( ^ ( A B + D *
E ^ ( A B + D * E
- ^ ( - A B + D * E
Stack Postfix expression
Input Symbol
F ^ ( - A B + D * E F
) ^ ( - A B + D * E F -
^ A B + D * E F -
^ A B + D * E F - ^
CONVERSION OF INFIX EXPRESSION TO
PREFIX EXPRESSION
CONVERT INFIX EXPRESSION INTO PREFIX
EXPRESSION: (A+B)*C
1. Read unput symbols from right to left
(A+B)*C
Input Stack Prefix Expression
empty empty
C C
* C
*
* ) C
)
* ) BC
B
Input Stack Prefix Expression
* ) + BC
+
* ) + ABC
A
* ) + + ABC
(
* +ABC
*+ABC
(A+B)*(C/G)+F
empty empty
F F
+ + F
) +) F
G +) GF
Input Stack Prefix Expression
/ +) / GF
C +) / CGF
( +) / / CGF
* + * /CGF
) + * ) /CGF
B + * ) B /CGF
+ + * )+ B/CGF
Input Stack Prefix Expression
A + * )+ A B/CGF
( + * )+ + AB/CGF
+ * +* +AB/CGF
+*+AB/CGF
5 4 5 Push(5)
6 4 5 6 Push(6)
* 5 6 30 4 30 Push(30)
+ 4 30 34 34 Push(34)
7 7 Push(7)
8 7 8 Push(8)
+ 7 8 15 15 Push(15)
3 15 3 Push(3)
15 3 2 Push(2)
2
+ 3 2 5 15 5 Push(5)
/ 15 5 3 3 Push(3)
Scanned
Calculate
Input Operand 1 Operand 2 Stack
Value
Symbol
5 5 Push(5)
6 5 6 Push(6)
2 5 6 2 Push(2)
+ 6 2 8 5 8 Push(8)
* 5 8 40 40 Push(40)
12 40 12 Push(12)
4 40 12 4 Push(4)
/ 12 4 3 40 3 Push(3)
- 40 3 37 37 Push(37)
Result Of Expression is : 37 Pop
• Evaluate the following arithmetic expression P
written in postfix notation:
P : 4, 2, ^, 3, *,3,-,8,4 ,/,+
Answer: 47
EVALUATION OF PREFIX
EXPRESSION
EVALUATE THE FOLLOWING PREFIX EXPRESSION: - * + 4 3 2 5
SHOW DIAGRAMMATICALLY EACH STEP OF EVALUATION USING
STACK.
Scanned
Calculate
Input Operand 1 Operand 2 Stack
Value
Symbol
5 5 Push(5)
2 5 2 Push(2)
3 5 2 3 Push(3)
4 5 2 3 4 Push(4)
+ 4 3 7 5 2 7 Push(7)
* 7 2 14 5 14 Push(14)
- 14 5 9 9 Push(9)
5 2 5 Push(5)
6 2 5 6 Push(6)
3 2 5 6 3 Push(3)
+ 3 6 9 2 5 9 Push(9)
5 2 5 9 5 Push(5)
* 5 9 45 2 5 45 Push(45)
2 2 5 45 2 Push(2)
* 45 2 90 2 5 90 Push(90)
/ 90 5 18 2 18 Push(18)
- 18 2 16 16 Push(16)
Result Of Expression is : 16 pop
EVALUATE THE FOLLOWING PREFIX EXPRESSION: + A - * B C D
LET A=4;B=3;C=2;D=5 +4-*325
SHOW DIAGRAMMATICALLY EACH STEP OF EVALUATION USING STACK.
Scanned
Calculate
Input Operand 1 Operand 2 Stack
Value
Symbol
5 5 Push(5)
2 5 2 Push(2)
3 5 2 3 Push(3)
* 3 2 6 5 6 Push(6)
- 6 5 1 1 Push(1)
4 1 4 Push(4)
+ 4 1 5 5 Push(5)
Introduction
Representation of queue
Types of queues
Operations on queue
Queue full and queue empty conditions
Applications of queues
INTRODUCTION
Let us explain the concept of queues using the analogies given below.
People waiting for a bus. The first person standing in the line will be the first one to get
into the bus.
People standing outside the ticketing window of a cinema hall. The first person in the line
will get the ticket first and thus will be the first one to move out of it.
Luggage kept on conveyor belts. The bag which was placed first will be the first to come
out at the other end.
Cars lined at a toll bridge. The first car to reach the bridge will be the first to leave
REAL LIFE EXAMPLE OF QUEUE
QUEUE
In all these examples, we see that the element at the first position is served first. Same
is the case with queue data structure.
Queue is a linear data structure in which the insertion and deletion operations are
performed at two different ends.
The elements in a queue are added at one end called the rear and removed from the
other end called the front.
Queue is work on the principal of First-In-First-Out (FIFO), it means first entered
item remove first
QUEUE REPRESENTATION USING ARRAY
Example
Queue after inserting 25, 30, 51, 60 and 85.
INSERT AND DELETE OPERATIONS
EXAMPLE
Front rear
Fron rear
t
Front rear
QUEUE OVERFLOW AND UNDERFLOW
CONDITIONS
An overflow will occur when we try to insert an element into a queue that is already full.
When REAR = MAX – 1, where MAX is the size of the queue, we have an overflow condition.
Note that we have written MAX – 1 because the index starts from 0.
However, before inserting an element in a queue, we must check for overflow conditions.
An underflow condition occurs when we try to delete an element from a queue that
is already empty. If FRONT = –1 and REAR = –1, it means there is no element in the queue.
Similarly, before deleting an element from a queue, we must check for underflow
conditions.
ALGORITHM TO INSERT AN ELEMENT IN A QUEUE
Step 1:
If (REAR = MAX-1)
{
print “OVERFLOW”
Goto step 3
}
Step 2:
if (FRONT = -1&&REAR = -1)
FRONT = REAR =0
else
REAR = REAR + 1
QUEUE[REAR] = NUM
Step 3: EXIT
ALGORITHM TO DELETE AN ELEMENT FROM A
QUEUE
Queue empty: A queue is empty when its front pointer points to -1 position. When front
pointer is -1 then one cannot delete any data from a queue.
Front=-1
Rear=-1
(0) (1) (2) (3) (4) (5) (6)
QUEUE OPERATIONS
Insert(10) Insert(20)
10 10 20
(0) (1) (2) (3) (0) (1) (2) (3) (0) (1) (2) (3)
Front=-1 Queue Empty
Rear=-1 Front=Rear=0 Front=0 Rear=1
Insert(30) Insert(40)
10 20 30 10 20 30 40
Queue Full
(0) (1) (2) (3) (0) (1) (2) (3)
10 20 30 40 30 40
20 30 40
(0) (1) (2) (3) (0) (1) (2) (3)
(0) (1) (2) (3)
Front=0 Rear=3
Front=1 Rear=3 Front=2 Rear=3
delete
40
In a normal Queue Data Structure, we can insert elements until queue becomes full. But once the
queue becomes full, we can not insert the next element until all the elements are deleted from the
queue. For example, consider the queue below...
This situation also says that Queue is Full and we cannot insert the new element because 'rear' is still
at last position. In the above situation, even though we have empty positions in the queue we can not
make use of them to insert the new element. This is the major problem in a normal queue data
structure. To overcome this problem we use a circular queue data structure.
WHAT IS CIRCULAR QUEUE?
10 20 30 40 50
FrontFront
Rear =7
6 6 6
7 7 Front=Rear=0 7
Front=0
62 92
5 5 5
0 11 0 11 0
42
52 22
1 1 1
4 4 4
33 32
3 2 3 2 3 2
Front=-1
Rear=-1 Circular Queue Empty Circular Queue Full
Rear =7 Rear =7
Rear =7 6
6 7
6 7
7 62 92
Front=0 92
62 5
62 92 0
5 42
5 0
11 0 42
42
52
22 1
22 52 4
52 1
1 4 33 32
4 33 32
33 32 Front=1
3 2
3 2
3 2
delete delete Front=2
Rear =7
6 6
7 7
62 92 92 Rear =0
62
5 5
0 Rear reached to max-1 and there are 0
42 42
empty spaces available at starting
position so in circular queue if this is
the case then set rear=0
52 52
4 4
33 32 33 32
3 2 3 2
Front=3 Front=3
delete
CIRCULAR QUEUE
If queue is full then new element cannot be added in a queue. For deletion, front pointer
position is checked and queue empty condition is checked.
If front pointer is pointing to -1 then queue is empty and deletion operation cannot be
performed.
If queue contains any element then front pointer is incremented by one to remove an
element.
If front pointer is pointing to max-1 and element is present at 0 th position then front pointer
is initialize to 0th position to continue cycle.
Circular queue has advantage of utilization of space. Circular queue is full
only when there is no empty position in a queue. Before inserting an element
in circular queue front and rear both the pointers are checked. So if it
indicates any empty space anywhere in a queue then insertion takes place
DOUBLE ENDED QUEUE(DEQUEUE)
• Double Ended Queue is also a Queue data structure in which the insertion and deletion
operations are performed at both the ends (front and rear).
• That means, we can insert at both front and rear positions and can delete from both
front and rear positions.
• Thus, it does not follow FIFO rule (First In First Out).
TYPES OF DOUBLE ENDED QUEUE
• Double Ended Queue can be represented in TWO ways, those are as follows...
Input Restricted Double Ended Queue
Output Restricted Double Ended Queue
INPUT RESTRICTED DOUBLE ENDED
QUEUE:
• In input restricted double-ended queue, the insertion operation is performed at only
one end and deletion operation is performed at both the ends.
OUTPUT RESTRICTED DOUBLE
ENDED QUEUE:
• In output restricted double ended queue, the deletion operation is performed at only
one end and insertion operation is performed at both the ends.
PRIORITY QUEUE
Priority Queue
Front Rear
APPLICATIONS OF QUEUE
1.In real life scenario, Call Center phone systems uses Queues to hold
people calling them in an order, until a service representative is free.
2.Serving requests on a single shared resource, like a printer, CPU task
scheduling etc.
3.Queues are used as buffers on MP3 players and portable CD players,
iPod playlist.
4. Queues are used in Playlist for jukebox to add songs to the end, play
from the front of the list
DIFFERENCE BETWEEN STACK AND QUEUE