0% found this document useful (0 votes)
24 views

Chapter 3 Stack and Queue

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Chapter 3 Stack and Queue

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 84

CHAPTER 3

Stacks and Queues


STACK
• A stack is an important data structure which is used in computer applications/programming.
• Stack is an important data structure which stores its elements in an ordered manner.
• We will explain the concept of stacks using an analogy.
• Consider the simple example of plates stacked over one another in a canteen as shown in Fig. Now, when
you want to remove a plate, you remove the topmost plate first. Hence, you can add and remove an
element (i.e., a plate) only at/from one position which is the topmost position.

Add new plate Remove plate

• A stack is a linear data structure which uses the same principle.


STACK
• Stack is a linear data structure in which insertion and deletion operations are performed only from one end
which is called the TOP of the stack .
• stack is based on a LIFO (Last-In-First-Out) principle, as the element that was inserted last is the first one to
be removed.
• Representation of stack in memory:
40 TOP

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 3: set stack[top] = value [Insert element]

• 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

stack Pop Pop Pop Pop Stack top


ALGORITHM FOR POP OPERATION
• Step 1:
if (top==-1)
{
[Check for stack Empty/Underflow]
Print “Stack Underflow”
GOTO Step 4
}
• Step 2: Set value=stack[top] [delete element]

• Step 3: set top=top-1 [ Decrement stack top pointer by one ]

• 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

 The way to write arithmetic expression is known as a notation.


 An arithmetic expression can be written in three different but equivalent
notations, i.e., without changing the essence or output of an expression.
 These notations are −
Infix Notation
Prefix Notation
Postfix Notation
 These notations are named as how they use operator in expression.
operators

a+b-c
operands
• Infix Notation: operators are used in-between operands.

a+b

• Prefix Notation:operator is written before operands.

+ab
Prefix notation is also known as Polish Notation.

• Postfix Notation: the operator is written after the operands.

Prefix Postfix
ab+ Infix Expression Expression Expression
A+B +AB AB+

A+B*C +A*BC ABC*+


• To parse any arithmetic expression, we need to take care of operator precedence and
associativity .
• Operator Precedence
• When an operand is in between two different operators, which operator will take the
operand first, is decided by the precedence of an operator over others.

a-b*c
Sr.No. Operator Precedence Associativity

1 Exponentiation ^ Highest Right Associative


2 Multiplication ( ∗ ) & Division ( / ) Second Highest Left Associative
3 Addition ( + ) & Subtraction ( − ) Lowest Left Associative
STACK APPLICATION

Conversion of infix to postfix


expression
CONVERT FOLLOWING EXPRESSION INTO POSTFIX
FORM. GIVE STEPWISE PROCEDURE.

a + ( b * c)
2 1

Input Symbol Stack Postfix expression

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

Input Symbol Stack Postfix expression

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

Input Symbol Stack Postfix expression

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

Infix Expression : (A+B)*C Prefix Expression : *+ABC


CONVERT INFIX EXPRESSION INTO PREFIX EXPRESSION:
(A+B)*(C/G)+F
1. Read input symbols from right to left

(A+B)*(C/G)+F

Input Stack Prefix Expression

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

Infix Expression : (A+B)*(C/G)+F

Prefix Expression : +*+AB/CGF


EVALUATION OF POSTFIX
EXPRESSION
EVALUATE THE FOLLOWING POSTFIX EXPRESSION:
456*+
SHOW DIAGRAMMATICALLY EACH STEP OF EVOLUTION
USING STACK.
Scanned
Calculate
Input Operand 1 Operand 2 Stack
Value
Symbol
4 4 Push(4)

5 4 5 Push(5)

6 4 5 6 Push(6)

* 5 6 30 4 30 Push(30)

+ 4 30 34 34 Push(34)

Result Of Expression is : 34 Pop


EVALUATE THE FOLLOWING POSTFIX EXPRESSION: 7 8 + 3 2 + /
SHOW DIAGRAMMATICALLY EACH STEP OF EVOLUTION USING STACK.
Scanned
Calculate
Input Operand 1 Operand 2 Stack
Value
Symbol

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)

Result Of Expression is : 3 Pop


EVALUATE THE FOLLOWING POSTFIX EXPRESSION:
5, 6, 2, +, *, 12, 4, /, -
SHOW DIAGRAMMATICALLY EACH STEP OF EVOLUTION USING STACK.

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)

Result Of Expression is : 9 Pop


EVALUATE THE FOLLOWING PREFIX EXPRESSION: - / * 2 * 5 + 3 6 5 2
SHOW DIAGRAMMATICALLY EACH STEP OF EVALUATION USING STACK.
Scanned
Calculate
Input Operand 1 Operand 2 Stack
Value
Symbol
2 2 Push(2)

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)

Result Of Expression is : 5 Pop


QUEUE
QUEUE

 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

Step 1: if( FRONT = -1 || FRONT > REAR)


Print “UNDERFLOW”
goto step 2
else
VAL = QUEUE[FRONT]
FRONT = FRONT + 1
Step 2: EXIT
QUEUE FULL AND QUEUE EMPTY
 Queue full: A queue is full when its rear pointer points to max -1 position. Max is
maximum number of elements in a queue. If rear pointer is not equal to max1 then a
new element can be added to a queue.

(0) (1) (2) (3) (4) (5) (6)

 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

Enqueue/insert - adds an item to the (rear) end of the queue


Dequeue/delete - remove an item from front of the queue
isEmpty - tests for whether or not queue is empty
isFull - tests to see if queue is full.
INSERT OPERATION

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)

Front=0 Rear=2 Front=0 Rear=3


DELETE OPERATION
Queue Full delete
delete

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

(0) (1) (2) (3)


(0) (1) (2) (3)
Front=-1
Rear=3 Rear=-1 Queue Empty
Front=3
IMPLEMENTATION OF QUEUE USING C
do
#include <stdio.h> {
#include<stdlib.h> printf("1.Insert element to queue \n");
#include<conio.h> printf("2.Delete element from queue \n");
#define MAX 50 printf("3.Display all elements of queue \n");
void insert(); printf("4.Quit \n");
void dele(); printf("Enter your choice : ");
void display(); scanf("%d", &ch);
int Queue[MAX]; switch(ch)
int rear = - 1; {
int front = - 1; case 1:
void main() insert();
{ break;
int ch; case 2:
clrscr(); dele();
break;
case 3:
void insert()
display(); {
break; int item;
case 4: if(rear == MAX - 1)
exit(1); printf("Queue Overflow \n");
default: else
printf("Wrong choice \n"); {
} if(front== - 1)
} while(ch!=4); front = 0;
getch(); printf("Inset the element in queue : ");
} scanf("%d", &item);
rear = rear + 1;
Queue[rear] = item;
}
}
void dele()
void display()
{
{
if(front == - 1 || front > rear)
int i;
{
if(front == - 1)
printf("Queue Underflow \n");
printf("Queue is empty \n");
break;
else
}
{
else
printf("Queue is : \n");
{
for(i = front; i <= rear; i++)
printf("Element deleted from queue is : %d\n",
printf("%d ", Queue[i]);
Queue[front]);
printf("\n");
front = front + 1;
}
}
}
}
TYPES OF QUEUES IN DATA STRUCTURE

1.Simple Queue/linear Queue


2.Circular Queue
3.Priority Queue
4.Dequeue (Double Ended Queue)
CIRCULAR QUEUE

 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...

The queue after inserting all the elements into it is as follows...


Now consider the following situation after deleting three elements from the queue..

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?

A Circular Queue can be defined as follows..


A circular queue is a linear data structure in which the operations are performed based on FIFO
(First In First Out) principle and the last position is connected back to the first position to make a
circle.
Graphical representation of a circular queue is as follows...
rear rear

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

 The above diagram represents a circular queue using array.


 It has rear pointer to insert an element and front pointer to delete an
element.
 It works in FIFO manner where first inserted element is deleted first.
 Initially front and rear both are initialized to -1 to represent queue empty.
First element inserted in circular queue is stored at 0th index position
pointed by rear pointer. For the very first element, front pointer is also set to
0th position. Whenever a new element is inserted in a queue rear pointer is
incremented by one.
 If rear is pointing to max-1 and no element is present at 0th position
then rear is set to 0th position to continue cycle. Before inserting an
element, queue full condition is checked. If rear is set to max-1 position and
front is set to 0 then queue is full. Otherwise if rear =front+1 then also queue
is full.
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

 A priority queue is a data structure in which each element is assigned a priority.


 The priority of the element will be used to determine the order in which the elements will be
processed.
 The general rules of processing the elements of a priority queue are
1.An element with higher priority is processed before an element with a lower priority.
2.Two elements with the same priority are processed on a first-come-first-served (FCFS)
basis.
 when an element has to be removed from the queue, the one with the highest-priority is
retrieved first
EXAMPLE

Input Element Priority


B 3
A 1
C 4
D 2

Priority Queue

A,1 D,2 B,3 C,4

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

You might also like