SlideShare a Scribd company logo
1 of 152
UNIT V : Stack and Queue
By
Mr.S.Selvaraj
Asst. Professor (SRG) / CSE
Kongu Engineering College
Perundurai, Erode, Tamilnadu, India
Thanks to and Resource from : Sumitabha Das, “Computer Fundamentals and C Programming”, 1st Edition, McGraw Hill, 2018.
20CST21 – Programming and Linear Data Structures
Syllabus – Unit Wise
7/15/2021 5.1 _ Introduction to Stack 2
List of Exercises
7/15/2021 5.1 _ Introduction to Stack 3
Text Book and Reference Book
7/15/2021 5.1 _ Introduction to Stack 4
Unit IV : Contents
1. Introduction to Stack
2. Implementation of stack using
– Array and linked list
3. Application of stack
– Infix to Postfix expression conversion
– Postfix expression evaluation
4. Introduction to Queue
5. Implementation of Queue using
– Array and linked list
6. Other variations of Queue
7. Applications of Queue
7/15/2021 5
5.1 _ Introduction to Stack
Introduction to Stack
• Stack is a simple data structure which has
bounded capacity.
• In stack, data is added and removed at only one
end called the Top.
• When an item is inserted on to the stack, it is
placed on Top of the stack.
• When an item is removed from the stack, it
removes the Top most element.
• There are two basic operations performed on a
Stack:
– Push()
– Pop()
7/15/2021 5.1 _ Introduction to Stack 6
Introduction to Stack
• In stack terminology,
– insertion operation is called Push() operation and
– removal operation is called Pop() operation.
• The last element that is pushed into the stack
is the first element to be popped out of the
stack.
• That is, it follows Last-In First-Out (LIFO)
strategy.
• Some of the features of stack are as follows:
7/15/2021 5.1 _ Introduction to Stack 7
Features of Stack
• Stack is an ordered list of similar type of
elements.
• It is a linear list where all insertions and deletions
are permitted only at one end of the list called
Top.
• Stack is a LIFO (Last-In-First-Out) structure.
• When a stack is full then it is said to be in
Overflow state
• When a stack is empty then it is said to be in
Underflow state.
7/15/2021 5.1 _ Introduction to Stack 8
Stack Data Structure
7/15/2021 5.1 _ Introduction to Stack 9
Stack - Push Operation
7/15/2021 5.1 _ Introduction to Stack 10
Pop Operation
7/15/2021 5.1 _ Introduction to Stack 11
Application of Stack in Real time scenario
• A stack of plates on the dining table
• Wearing and Removing of Bangles in our hand
7/15/2021 5.1 _ Introduction to Stack 12
Applications of stack in Computer Science and Programming
• Support of function calls
– When a function is invoked, the function which is invoked last will be
completed first. This is the property of stack.
– Primary purpose is to store the return address.
• Support of recursive functions
– When a function calls itself recursively, a return address needs to be stored
for each activation of the function so that it can later be used to return from
the function activation.
• An "undo/redo" mechanism in text editors
• Expression Evaluation
– Used to evaluate prefix, postfix and infix expressions.
• Expression Conversion
– An expression can be represented in prefix, postfix or infix form. Stack can be
used to convert one form of expression to another
• Reverse a string
– Push a given word to stack letter by letter and then pop letters from the stack
one by one
• Forward and backward feature in web browsers
• Parenthesis Checking
– Stack is used to check the proper opening and closing of parenthesis
7/15/2021 5.1 _ Introduction to Stack 13
Implementation of Stack
• Stack can be implemented using any one of
the following data structures:
– Array
– Linked List
• Implementing a stack using array can store
fixed number of data values but using linked
list hold any number of elements .
7/15/2021 5.1 _ Introduction to Stack 14
Stack using Array
• Stack can be implemented using one
dimensional array.
• Array can be created in predefined size.
• We can not increase the size of the array if we
have more elements to insert.
• That is, Stack implementation with array can
hold only fixed number of elements
7/15/2021 5.1 _ Introduction to Stack 15
Stack using Array
• we need an integer variable to keep track of
Top most element in the stack.
• It should be initialized with -1 to indicate stack
empty.
7/15/2021 5.1 _ Introduction to Stack 16
Stack using Array
• In the above definition, SIZE is a constant
which represents maximum number of
elements stored in a stack.
• Value of Top = SIZE – 1 indicates stack is full.
• In a stack, initially Top is set to -1.
• Top is used to keep track of the index of the
Top most element.
7/15/2021 5.1 _ Introduction to Stack 17
Stack using Array
• The following table represents status of stack
for different values of Top.
7/15/2021 5.1 _ Introduction to Stack 18
Push(value) - Inserting value into the stack
• Step 1: Check whether stack is already FULL.
Stack is Full when Top= SIZE-1.
• Step 2: If it is FULL then display "Stack is
FULL" and stop the function.
• Step 3: If it is not FULL, then increment Top
value by one and store the value at stk[Top].
7/15/2021 5.1 _ Introduction to Stack 19
Pop() - Remove a value from the Stack
• Step 1: Check whether stack is EMPTY. It is
Empty when Top= -1
• Step 2: If it is EMPTY, then display "Stack is
Empty" and stop the function.
• Step 3: If it is not EMPTY, then delete Top
most element (ie., stack[Top]) and decrement
Top value by one.
Top = Top -1
7/15/2021 5.1 _ Introduction to Stack 20
Display() - Display the elements of a Stack
• Step 1: Check whether stack is EMPTY.
• Step 2: If it is EMPTY, then display "Stack is Empty"
and terminate the function.
• Step 3: Initialize a variable 'i' with a value Top.
• Step 4: Display the value of stack[i] and decrement
the value of i by one
• Step 5: Repeat step 4 until “i” value becomes 0.
7/15/2021 5.1 _ Introduction to Stack 21
7/15/2021 5.1 _ Introduction to Stack 22
Stack using Array
7/15/2021 5.1 _ Introduction to Stack 23
7/15/2021 5.1 _ Introduction to Stack 24
7/15/2021 5.1 _ Introduction to Stack 25
7/15/2021 5.1 _ Introduction to Stack 26
7/15/2021 5.1 _ Introduction to Stack 27
Thank you
7/15/2021 5.1 _ Introduction to Stack 28
UNIT V : Stack and Queue
By
Mr.S.Selvaraj
Asst. Professor (SRG) / CSE
Kongu Engineering College
Perundurai, Erode, Tamilnadu, India
Thanks to and Resource from : Sumitabha Das, “Computer Fundamentals and C Programming”, 1st Edition, McGraw Hill, 2018.
20CST21 – Programming and Linear Data Structures
Unit IV : Contents
1. Introduction to Stack
2. Implementation of stack using
– Array and linked list
3. Application of stack
– Infix to Postfix expression conversion
– Postfix expression evaluation
4. Introduction to Queue
5. Implementation of Queue using
– Array and linked list
6. Other variations of Queue
7. Applications of Queue
7/15/2021 30
5.2 _ Implementation of Stack
Implementation of Stack
• Stack can be implemented using any one of
the following data structures:
– Array
– Linked List
• Implementing a stack using array can store
fixed number of data values but using linked
list hold any number of elements .
7/15/2021 5.2 _ Implementation of Stack 31
Stack using Array
• Stack can be implemented using one
dimensional array.
• Array can be created in predefined size.
• We can not increase the size of the array if we
have more elements to insert.
• That is, Stack implementation with array can
hold only fixed number of elements
7/15/2021 5.2 _ Implementation of Stack 32
Stack using Array
• we need an integer variable to keep track of
Top most element in the stack.
• It should be initialized with -1 to indicate stack
empty.
7/15/2021 5.2 _ Implementation of Stack 33
Stack using Array
• In the above definition, SIZE is a constant
which represents maximum number of
elements stored in a stack.
• Value of Top = SIZE – 1 indicates stack is full.
• In a stack, initially Top is set to -1.
• Top is used to keep track of the index of the
Top most element.
7/15/2021 5.2 _ Implementation of Stack 34
Stack using Array
• The following table represents status of stack
for different values of Top.
7/15/2021 5.2 _ Implementation of Stack 35
Push(value) - Inserting value into the stack
• Step 1: Check whether stack is already FULL.
Stack is Full when Top= SIZE-1.
• Step 2: If it is FULL then display "Stack is
FULL" and stop the function.
• Step 3: If it is not FULL, then increment Top
value by one and store the value at stk[Top].
7/15/2021 5.2 _ Implementation of Stack 36
Pop() - Remove a value from the Stack
• Step 1: Check whether stack is EMPTY. It is
Empty when Top= -1
• Step 2: If it is EMPTY, then display "Stack is
Empty" and stop the function.
• Step 3: If it is not EMPTY, then delete Top
most element (ie., stack[Top]) and decrement
Top value by one.
Top = Top -1
7/15/2021 5.2 _ Implementation of Stack 37
Display() - Display the elements of a Stack
• Step 1: Check whether stack is EMPTY.
• Step 2: If it is EMPTY, then display "Stack is Empty"
and terminate the function.
• Step 3: Initialize a variable 'i' with a value Top.
• Step 4: Display the value of stack[i] and decrement
the value of i by one
• Step 5: Repeat step 4 until “i” value becomes 0.
7/15/2021 5.2 _ Implementation of Stack 38
7/15/2021 5.2 _ Implementation of Stack 39
Stack using Array
7/15/2021 5.2 _ Implementation of Stack 40
7/15/2021 5.2 _ Implementation of Stack 41
7/15/2021 5.2 _ Implementation of Stack 42
7/15/2021 5.2 _ Implementation of Stack 43
7/15/2021 5.2 _ Implementation of Stack 44
Array implementation of stack
• Pros: Easy to implement. Memory is not efficiently
utilized as pointers are not involved.
• Cons: It is not dynamic. It doesn’t grow and shrink
depending on needs at runtime
7/15/2021 45
5.2 _ Implementation of Stack
Linked list Implementation of Stack
//Initially,
struct stack
{
int data;
struct stack *next;
}*head,*temp;
//Function Prototype
void push(void);
void pop(void);
void display(void);
int isempty(void);
7/15/2021 46
5.2 _ Implementation of Stack
Linked list Implementation of Stack
void push()
{
int x;
printf("n enter the numbern");
scanf("%d",&x);
temp=(struct stack *)malloc(sizeof(struct stack));
temp->data=x;
temp->next=NULL;
if(head==NULL)
{
head=temp;
}
else
{
temp->next=head;
head=temp;
}
}
7/15/2021 47
5.2 _ Implementation of Stack
Linked list Implementation of Stack
int isempty()
{
if(head==NULL)
return 1;
else
return 0;
}
void pop()
{
if(isempty())
printf("n stack is emptyn");
else
{
printf("n The popped element is %d",head->data);
head=head->next;
}
}
7/15/2021 48
5.2 _ Implementation of Stack
Linked list Implementation of Stack
void display()
{
if(isempty())
printf("n stack is emptyn");
else
{
temp=head;
while(temp!=NULL)
{
printf("%d->",temp->data);
temp=temp->next;
}
printf("NULL");
}
}
7/15/2021 49
5.2 _ Implementation of Stack
Linked list Implementation of Stack
int main()
{
int c;
while(1)
{
printf("n1.push 2.pop 3.display 4.exitn");
scanf("%d",&c);
switch(c)
{
case 1:
push();
break;
case 2:
pop();
break;
7/15/2021 50
5.2 _ Implementation of Stack
Linked list Implementation of Stack
case 3:
display();
break;
case 4:
exit(0);
}
}
return 0;
}
7/15/2021 51
5.2 _ Implementation of Stack
Thank you
7/15/2021 5.2 _ Implementation of Stack 52
UNIT V : Stack and Queue
By
Mr.S.Selvaraj
Asst. Professor (SRG) / CSE
Kongu Engineering College
Perundurai, Erode, Tamilnadu, India
Thanks to and Resource from : Sumitabha Das, “Computer Fundamentals and C Programming”, 1st Edition, McGraw Hill, 2018.
20CST21 – Programming and Linear Data Structures
Unit IV : Contents
1. Introduction to Stack
2. Implementation of stack using
– Array and linked list
3. Application of stack
– Infix to Postfix expression conversion
– Postfix expression evaluation
4. Introduction to Queue
5. Implementation of Queue using
– Array and linked list
6. Other variations of Queue
7. Applications of Queue
7/15/2021 54
5.3 _ Application of Stack
Applications of Stack
Balancing Parenthesis
Postfix Expressions Evaluation
Infix to Postfix Conversion
Towers of Hanoi problem
String Reversal
sudoku solver
7/15/2021 55
5.3 _ Application of Stack
Infix, Prefix, Postfix Expression
• Infix expression:
– An expression is called the Infix expression if the operator
appears in between the operands in the expression.
– Simply of the form (operand1 operator operand2).
– Operators are written in-between their operands.
X + Y
A * ( B + C ) / D
• Prefix expression (also known as "Polish notation"):
– An expression is called the Prefix expression if the
operator appears before the operands in the expression.
– Simply of the form (operator operand1 operand2).
– Operators are written before their operands.
+ X Y
/ * A + B C D
7/15/2021 56
5.3 _ Application of Stack
Infix, Prefix, Postfix Expression
• Postfix expression (also known as "Reverse
Polish notation"):
– An expression is called the postfix expression if the
operator appears in the expression after the
operands.
– Simply of the form (operand1 operand2 operator).
– Operators are written after their operands.
X Y +
A B C + * D /
7/15/2021 57
5.3 _ Application of Stack
Infix, Prefix, Postfix Expression
7/15/2021 58
5.3 _ Application of Stack
Infix to Postfix Conversion
Algorithm
1. Scan the infix expression from left to right.
2. If the scanned character is an operand, output it.
3. Else,
…..3.1 If the precedence of the scanned operator is greater than
the precedence of the operator in the stack or the stack is
empty or the stack contains a ‘(‘, push it.
…..3.2 Else, Pop all the operators from the stack which are
greater than or equal to in precedence than that of the
scanned operator. After doing that Push the scanned
operator to the stack. (If you encounter parenthesis while
popping then stop there and push the scanned operator in
the stack.)
4. If the scanned character is an ‘(‘, push it to the stack.
5. If the scanned character is an ‘)’, pop the stack and output all
the operators until a ‘(‘ is encountered, and discard both
the parenthesis.
6. Repeat steps 2-6 until infix expression is scanned.
7. Print the output
8. Pop and output from the stack until it is not empty.
7/15/2021 59
5.3 _ Application of Stack
An illustration
• First, the symbol a is read, so it is passed through to the output. Then '+' is
read and pushed onto the stack.
• Next b is read and passed through to the output
• Next a '*' is read. The top entry on the operator stack has lower
precedence than '*', so nothing is output and '*' is put on the stack.
• Next, c is read and output.
7/15/2021 60
5.3 _ Application of Stack
An illustration
• The next symbol is a '+'. Checking the stack, we find that we
will pop a '*' and place it on the output, pop the other '+',
which is not of lower but equal priority, on the stack, and then
push the '+'.
• The next symbol read is an '(', which, being of highest
precedence, is placed on the stack. Then d is read and output.
7/15/2021 61
5.3 _ Application of Stack
An illustration
• We continue by reading a '*'. Since open parentheses do not
get removed except when a closed parenthesis is being
processed, there is no output. Next, e is read and output.
• The next symbol read is a '+'. We pop and output '*' and then
push '+'. Then we read and output
7/15/2021 62
5.3 _ Application of Stack
An illustration
• Now we read a ')', so the stack is emptied back to the '('. We
output a '+'.
• We read a '*' next; it is pushed onto the stack. Then g is read
and output.
7/15/2021 63
5.3 _ Application of Stack
An illustration
• The input is now empty, so we pop and output symbols from
the stack until it is empty.
7/15/2021 64
5.3 _ Application of Stack
7/15/2021 65
5.3 _ Application of Stack
7/15/2021 66
5.3 _ Application of Stack
7/15/2021 67
5.3 _ Application of Stack
PROBLEM
1. 3+4*5/6
2. (300+23)*(43-21)/(84+7)
3. (4+8)*(6-5)/((3-2)*(2+2))
7/15/2021 68
5.3 _ Application of Stack
ANSWER
1. 3 4 5 * 6 / +
2. 300 23 + 43 21 -* 84 7 + /
3. 4 8 + 6 5 -* 3 2 –2 2 + * /
7/15/2021 69
5.3 _ Application of Stack
Problems
1) Infix : (A+B) * (C-D)
Infix to Postfix : AB+CD-*
2) Infix : A-(B/C)*(A/K)-L
Infix to Postfix : ABC/-AK/L-*
7/15/2021 70
5.3 _ Application of Stack
Expression Evaluation
The simple algorithm uses a stack and is as follows:
1. Make an empty stack.
2. Read an input string.
3. Read characters one by one until end of string is
encountered.
4. If the character is a digit, convert it into an integer
and push onto the stack.
5. If the character is not a digit, pop two numbers from
the stack and perform the corresponding operation
and push the result onto the stack.
6. At end of the string, pop the result from the stack
7/15/2021 71
5.3 _ Application of Stack
ILLUSTRATION
7/15/2021
Next '+' is read, so 3 and 2 are popped from the stack and their sum, 5, is pushed.
Next 8 is pushed.
72
5.3 _ Application of Stack
7/15/2021
Now '*' is read, so 8 and 5 are popped as 8 * 5 = 40 is pushed.
73
5.3 _ Application of Stack
7/15/2021 74
5.3 _ Application of Stack
PROBLEM
1. 3+4*5/6
2. (300+23)*(43-21)/(84+7)
3. (4+8)*(6-5)/((3-2)*(2+2))
7/15/2021 75
5.3 _ Application of Stack
Thank you
7/15/2021 5.3 _ Application of Stack 76
UNIT V : Stack and Queue
By
Mr.S.Selvaraj
Asst. Professor (SRG) / CSE
Kongu Engineering College
Perundurai, Erode, Tamilnadu, India
Thanks to and Resource from : Sumitabha Das, “Computer Fundamentals and C Programming”, 1st Edition, McGraw Hill, 2018.
20CST21 – Programming and Linear Data Structures
Unit IV : Contents
1. Introduction to Stack
2. Implementation of stack using
– Array and linked list
3. Application of stack
– Infix to Postfix expression conversion
– Postfix expression evaluation
4. Introduction to Queue
5. Implementation of Queue using
– Array and linked list
6. Other variations of Queue
7. Applications of Queue
7/15/2021 78
5.4 _ Introduction to Queue and its
Implementation
Introduction to Queue
7/15/2021
5.4 _ Introduction to Queue and its
Implementation
79
Front
Rear
Introduction to Queue
• Queue is a simple linear data structure which is a
ordered list of similar type of items.
• In Queue, data is added at one end called the rear, and
data is removed at the other end called the front.
• Queue follows FIFO (First-In-First-Out) strategy i.e. The
element which is inserted first is the one removed first.
7/15/2021 80
5.4 _ Introduction to Queue and its
Implementation
Queue Representation
7/15/2021
5.4 _ Introduction to Queue and its
Implementation
81
Queue ADT
• The following operations are implemented in queue
data structure:
– Isfull() – used to check whether the queue is full.
– Isempty() – used to check whether the queue is empty.
– Enqueue() – used to add a new item to the end of the
queue. (Insertion Operation)
– Dequeue()- used to remove the front item from the queue.
(Deletion Operation)
– Display() - used to display all the elements in the queue.
7/15/2021 82
5.4 _ Introduction to Queue and its
Implementation
Applications of Queue
• CPU scheduling
– When multiple processes require CPU at the same time,
CPU scheduling algorithms schedules the tasks for
submission. Usually these algorithms are implemented
using Queue data structure.
• Printer Queue
– Jobs submitted to a printer are printed in order of their
arrival.
• Call Center phone systems
– All the calls are queued until a service representative is
free. Calls are serviced based on the order of their arrival.
7/15/2021 83
5.4 _ Introduction to Queue and its
Implementation
Implementation Of Queue
• There are two ways to implement a queue:
–Sequential allocation Using array
–Linked allocation Using linked list
7/15/2021 84
5.4 _ Introduction to Queue and its
Implementation
Linear Queue – Definition and Overflow
7/15/2021
5.4 _ Introduction to Queue and its
Implementation
85
Linear Queue – Underflow
7/15/2021
5.4 _ Introduction to Queue and its
Implementation
86
Linear Queue – Enqueue
7/15/2021
5.4 _ Introduction to Queue and its
Implementation
87
Linear Queue – Dequeue
7/15/2021
5.4 _ Introduction to Queue and its
Implementation
88
Linear Queue – Traversal / Display
7/15/2021
5.4 _ Introduction to Queue and its
Implementation
89
Array implementation of Queue
Initially,
#define MAX 3
int q[MAX], rear=-1, front=-1;
Function Prototype
void enqueue(void);
void dequeue(void);
int isfull(void);
int isempty(void);
void display(void);
7/15/2021 90
5.4 _ Introduction to Queue and its
Implementation
Array implementation of Queue
int isfull()
{
if(rear==MAX-1)
return 1;
else
return 0;
}
int isempty()
{
if(front==-1||front>rear)
return 1;
else
return 0;
}
7/15/2021 91
5.4 _ Introduction to Queue and its
Implementation
Array implementation of Queue
void enqueue()
{
int x;
if(isfull())
printf("n queue is fulln");
else
{
printf("n enter the numbern");
scanf("%d",&x);
if(rear==-1)
{
front=0;
q[++rear]=x;
}
else
{
q[++rear]=x;
}
}
}
7/15/2021 92
5.4 _ Introduction to Queue and its
Implementation
Array implementation of Queue
void dequeue()
{
if(isempty())
printf("n queue is emptyn");
else if(front==rear)
{
printf("n The dequeued element is %dn",q[front]);
front=rear=-1;
}
else
{
printf("n The dequeued element is %dn",q[front]);
front++;
}
}
7/15/2021 93
5.4 _ Introduction to Queue and its
Implementation
Array implementation of Queue
void display()
{
int i;
if(isempty())
printf("n queue is emptyn");
else
{
printf("n the elements in the queue aret");
for(i=front;i<=rear; i++)
printf("%dt",q[i]);
}
}
7/15/2021 94
5.4 _ Introduction to Queue and its
Implementation
Array implementation of Queue
int main()
{
int c;
while(1)
{
printf("n1.enqueue 2.dequeue 3.display 4.exitn");
scanf("%d",&c);
switch(c)
{
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
exit(0);
}
}
return 0;
}
7/15/2021 95
5.4 _ Introduction to Queue and its
Implementation
Linked List implementation of Queue – Define and underflow
7/15/2021
5.4 _ Introduction to Queue and its
Implementation
96
Linked List implementation of Queue - Display
7/15/2021
5.4 _ Introduction to Queue and its
Implementation
97
Linked List implementation of Queue - Enqueue
7/15/2021
5.4 _ Introduction to Queue and its
Implementation
98
Linked List implementation of Queue - Enqueue
7/15/2021
5.4 _ Introduction to Queue and its
Implementation
99
Linked List implementation of Queue - Dequeue
7/15/2021
5.4 _ Introduction to Queue and its
Implementation
100
Linked List implementation of Queue
struct queue
{
int data;
struct queue *next;
}*rear, *front,*temp;
Function Prototype
void enqueue(void);
void dequeue(void);
void display(void);
int isempty(void);
7/15/2021 101
5.4 _ Introduction to Queue and its
Implementation
Linked List implementation of Queue
void enqueue()
{
int x;
printf("n enter the numbern");
scanf("%d",&x);
temp=(struct queue *)malloc(sizeof(struct queue));
temp->data=x;
temp->next=NULL;
if(front==NULL)
{
front=rear=temp;
}
else
{
rear->next=temp;
rear=temp;
}
}
7/15/2021 102
5.4 _ Introduction to Queue and its
Implementation
Linked List implementation of Queue
int isempty()
{
if(front==NULL)
return 1;
else
return 0;
}
void dequeue()
{
if(isempty())
printf("n queue is emptyn");
else
{
printf("n The deleted element is %d",front->data);
front=front->next;
}
}
7/15/2021 103
5.4 _ Introduction to Queue and its
Implementation
Linked List implementation of Queue
void display()
{
if(isempty())
printf("n queue is emptyn");
else
{
temp=front;
while(temp!=NULL)
{
printf("%d->",temp->data);
temp=temp->next;
}
printf("NULL");
}
}
7/15/2021 104
5.4 _ Introduction to Queue and its
Implementation
Linked List implementation of Queue
int main()
{
int c;
while(1)
{
printf("n1.enqueue 2.dequeue 3.display 4.exitn");
scanf("%d",&c);
switch(c)
{
case 1:
enqueue();
break;
case 2:
dequeue();
break;
7/15/2021 105
5.4 _ Introduction to Queue and its
Implementation
Linked List implementation of Queue
case 3:
display();
break;
case 4:
exit(0);
}
}
return 0;
}
7/15/2021 106
5.4 _ Introduction to Queue and its
Implementation
Thank you
7/15/2021
5.4 _ Introduction to Queue and its
Implementation
107
UNIT V : Stack and Queue
By
Mr.S.Selvaraj
Asst. Professor (SRG) / CSE
Kongu Engineering College
Perundurai, Erode, Tamilnadu, India
Thanks to and Resource from : Sumitabha Das, “Computer Fundamentals and C Programming”, 1st Edition, McGraw Hill, 2018.
20CST21 – Programming and Linear Data Structures
Unit IV : Contents
1. Introduction to Stack
2. Implementation of stack using
– Array and linked list
3. Application of stack
– Infix to Postfix expression conversion
– Postfix expression evaluation
4. Introduction to Queue
5. Implementation of Queue using
– Array and linked list
6. Other variations of Queue
7. Applications of Queue
7/15/2021 109
5.5 _ Types of Queue and its Applications
Types of Queue
• Linear Queue
• Circular Queue
• Double Ended Queue
• Priority Queue
7/15/2021 5.5 _ Types of Queue and its Applications 110
Types of Queue
7/15/2021 5.5 _ Types of Queue and its Applications 111
Linear Queue
7/15/2021 5.5 _ Types of Queue and its Applications 112
Linear Queue
• In Linear Queue, an
– insertion takes place from one end
– deletion occurs from another end
• The end at which the
– insertion takes place is known as the rear end,
– deletion takes place is known as front end
• It strictly follows the FIFO rule.
7/15/2021 5.5 _ Types of Queue and its Applications 113
Linear Queue
• The linear Queue can be represented, as shown
in the below figure:
• The elements are inserted from the rear end, and
if we insert more elements in a Queue, then the
rear value gets incremented on every insertion.
7/15/2021 5.5 _ Types of Queue and its Applications 114
Linear Queue
• If we want to show the deletion, then it can be
represented as:
• The front pointer points to the next element, and the
element which was previously pointed by the front
pointer was deleted.
7/15/2021 5.5 _ Types of Queue and its Applications 115
Drawbacks of Linear Queue
• The major drawback of using a linear Queue is
that insertion is done only from the rear end.
• If the first three elements are deleted from the
Queue, we cannot insert more elements even
though the space is available in a Linear Queue.
• In this case, the linear Queue shows
the overflow condition as the rear is pointing to
the last element of the Queue.
• Solution is Circular Queue.
7/15/2021 5.5 _ Types of Queue and its Applications 116
Circular Queue
7/15/2021 5.5 _ Types of Queue and its Applications 117
Why was the concept of the circular queue introduced?
• There was one limitation in the array
implementation of Queue.
• If the rear reaches to the end position of the
Queue then there might be possibility that
some vacant spaces are left in the beginning
which cannot be utilized.
• So, to overcome such limitations, the concept
of the circular queue was introduced.
7/15/2021 5.5 _ Types of Queue and its Applications 118
7/15/2021 5.5 _ Types of Queue and its Applications 119
Why was the concept of the circular queue introduced?
• As we can see in the previous image, the rear is at the last position
of the Queue and front is pointing somewhere rather than the
0th position.
• In the above array, there are only two elements and other three
positions are empty.
• if we try to insert the element then it will show that there are no
empty spaces in the Queue.
• There is one solution to avoid such wastage of memory space by
shifting both the elements at the left and adjust the front and rear
end accordingly.
• It is not a practically good approach because shifting all the
elements will consume lots of time.
• The efficient approach to avoid the wastage of the memory is to
use the circular queue data structure
7/15/2021 5.5 _ Types of Queue and its Applications 120
Why was the concept of the circular queue introduced?
Circular Queue
• It is linear data structure.
• operations are performed based on FIFO (First In First Out)
principle
• last position is connected back to the first position to make a
circle.
• It is also called ‘Ring Buffer’.
7/15/2021 121
5.5 _ Types of Queue and its Applications
Circular Queue
• In a normal Queue, we can insert elements until queue becomes
full. But once queue becomes full, we can not insert the next
element even if there is a space in front of queue.
• In circular queue, we can insert the next element even rear is
reached last position.
7/15/2021 5.5 _ Types of Queue and its Applications 122
Operations of Circular Queue
• The following are the operations that can be
performed on a circular queue:
– Front: It is used to get the front element from the
Queue.
– Rear: It is used to get the rear element from the
Queue.
– enQueue(value): This function is used to insert the
new value in the Queue. The new element is always
inserted from the rear end.
– deQueue(): This function deletes an element from the
Queue. The deletion in a Queue always takes place
from the front end.
7/15/2021 5.5 _ Types of Queue and its Applications 123
Steps on Enqueue Operation
• First, we will check whether the Queue is full
or not.
• Initially the front and rear are set to -1.
• When we insert the first element in a Queue,
front and rear both are set to 0.
• When we insert a new element, the rear gets
incremented, i.e., rear=rear+1.
7/15/2021 5.5 _ Types of Queue and its Applications 124
Steps on Enqueue Operation (contd.,)
7/15/2021 5.5 _ Types of Queue and its Applications 125
Steps on Dequeue Operation
• First, we check whether the Queue is empty or
not.
• If the queue is empty, we cannot perform the
dequeue operation.
• When the element is deleted, the value of front
gets decremented by 1.
• If there is only one element left which is to be
deleted, then the front and rear are reset to -1.
7/15/2021 5.5 _ Types of Queue and its Applications 126
Circular Queue - Applications
• Memory Management: The unused memory locations in the case
of ordinary queues can be utilized in circular queues.
• Traffic system: In a computer-controlled traffic system, circular
queues are used to switch on the traffic lights one by one
repeatedly as per the time set.
• CPU Scheduling: Operating systems often maintain a queue of
processes that are ready to execute or that are waiting for a
particular event to occur (Round-robin scheduling).
7/15/2021 127
5.5 _ Types of Queue and its Applications
Double Ended Queue (Deque)
7/15/2021 5.5 _ Types of Queue and its Applications 128
Double Ended Queue (Deque)
 The deque stands for Double Ended Queue.
 In the queue, the insertion takes place from one end while
the deletion takes place from another end.
 The end at which the
 insertion occurs is known as the rear end
 deletion occurs is known as front end.
 linear data structure
7/15/2021 129
5.5 _ Types of Queue and its Applications
Properties of Deque
• Deque can be used both as stack and queue as it allows the
insertion and deletion operations on both ends.
• (1) In deque, the insertion and deletion operation can be
performed from one side.
• The stack follows the LIFO rule in which both the insertion
and deletion can be performed only from one end;
• Therefore, we conclude that deque can be considered as a
stack.
7/15/2021 5.5 _ Types of Queue and its Applications 130
Properties of Deque
• (2) In deque, the insertion can be performed on one
end, and the deletion can be done on another end.
• The queue follows the FIFO rule in which the element
is inserted on one end and deleted from another end.
• Therefore, we conclude that the deque can also be
considered as the queue.
7/15/2021 5.5 _ Types of Queue and its Applications 131
Types of Deque
• There are two types of Queues:
– Input-restricted queue
– output-restricted queue
7/15/2021 5.5 _ Types of Queue and its Applications 132
Input restricted queue
• The input-restricted queue means that some
restrictions are applied to the insertion.
• In input-restricted queue,
– the insertion is applied to one end
– the deletion is applied from both the ends.
• This kind of Queue does not follow FIFO(first in first
out).
• Uses: This queue is used in the cases where the
consumption of the data needs to be in FIFO order
but and if there is a need to remove the recently
inserted data for some reasons and one such case
can be irrelevant data, performance issue, etc.
7/15/2021 133
5.5 _ Types of Queue and its Applications
Output restricted queue
• The output-restricted queue means that some
restrictions are applied to the deletion operation.
• In an output-restricted queue,
– the deletion can be applied only from one end
– the insertion is possible from both ends
• Uses: This queue is used in the case where the inputs
have some priority order to be executed and the
input can be placed even in the first place so that it is
executed first.
7/15/2021 134
5.5 _ Types of Queue and its Applications
List of Operations performed on Deque
7/15/2021 5.5 _ Types of Queue and its Applications 135
Double Ended Queue (Deque)
• A deque can be implemented either using a doubly-linked
list or circular array.
• In both implementation, we can implement all operations in O(1)
time.
7/15/2021 5.5 _ Types of Queue and its Applications 136
Applications of Deque
• The deque can be used as a stack and queue; therefore, it
can perform both redo and undo operations in software
applications.
• It can be used as a palindrome checker means that if we
read the string from both ends, then the string would be
the same.
• It can be used for multiprocessor scheduling. Suppose we
have two processors, and each processor has one process
to execute. Each processor is assigned with a process or a
job, and each process contains multiple threads. Each
processor maintains a deque that contains threads that are
ready to execute.
• Internet browser’s history
7/15/2021 5.5 _ Types of Queue and its Applications 137
Priority Queue
• A priority queue is an abstract data type that behaves similarly to
the normal queue except that each element has some priority.
• The element with the highest priority would come first in a
priority queue.
• The priority of the elements in a priority queue will determine the
order in which elements are removed from the priority queue.
• The priority queue supports only comparable elements, which
means that the elements are either arranged in an ascending or
descending order.
• For example, suppose we have some values like 1, 3, 4, 8, 14, 22
inserted in a priority queue with an ordering imposed on the
values is from least to the greatest.
• Therefore, the
– 1 number would be having the highest priority
– 22 will be having the lowest priority
7/15/2021 5.5 _ Types of Queue and its Applications 138
Characteristics of a Priority queue
• Every element in a priority queue has some
priority associated with it.
• An element with the higher priority will be
deleted before the deletion of the lesser
priority.
• If two elements in a priority queue have the
same priority, they will be arranged using the
FIFO principle.
7/15/2021 5.5 _ Types of Queue and its Applications 139
Priority Queue Example
• We have a priority queue that contains the following values:
1, 3, 4, 8, 14, 22
• All the values are arranged in ascending order.
• Now, we will observe how the priority queue will look after
performing the following operations:
• poll(): This function will remove the highest priority element from
the priority queue. In the above priority queue, the '1' element has
the highest priority, so it will be removed from the priority queue.
• add(2): This function will insert '2' element in a priority queue. As 2
is the smallest element among all the numbers so it will obtain the
highest priority.
• poll(): It will remove '2' element from the priority queue as it has
the highest priority queue.
• add(5): It will insert 5 element after 4 as 5 is larger than 4 and
lesser than 8, so it will obtain the third highest priority in a priority
queue.
7/15/2021 5.5 _ Types of Queue and its Applications 140
Types of Priority Queue
• There are two types of priority queue:
– Ascending order priority queue
– Descending order priority queue
7/15/2021 5.5 _ Types of Queue and its Applications 141
Ascending order priority queue
• In ascending order priority queue, a lower priority
number is given as a higher priority in a priority.
• For example, we take the numbers from 1 to 5
arranged in an ascending order like 1,2,3,4,5;
• Therefore, the smallest number, i.e., 1 is given as the
highest priority in a priority queue.
7/15/2021 5.5 _ Types of Queue and its Applications 142
Descending order priority queue
• In descending order priority queue, a higher priority
number is given as a higher priority in a priority.
• For example, we take the numbers from 1 to 5
arranged in descending order like 5, 4, 3, 2, 1;
• Therefore, the largest number, i.e., 5 is given as the
highest priority in a priority queue.
7/15/2021 5.5 _ Types of Queue and its Applications 143
Implementation of Priority Queue
• The priority queue can be implemented in
four ways that include
– arrays
– linked list
– heap data structure and
– binary search tree
• The heap data structure is the most efficient
way of implementing the priority queue.
7/15/2021 5.5 _ Types of Queue and its Applications 144
Priority Queue Operations
• The common operations that we can perform
on a priority queue are
– Insertion
– deletion and
– peek
7/15/2021 5.5 _ Types of Queue and its Applications 145
Complexities using different
implementations of Priority Queue
7/15/2021 5.5 _ Types of Queue and its Applications 146
What is Heap?
• A heap is a tree-based data structure that forms a
complete binary tree, and satisfies the heap property.
• If A is a parent node of B, then A is ordered with
respect to the node B for all nodes A and B in a heap.
• It means that the value of the parent node could be
more than or equal to the value of the child node
or
• the value of the parent node could be less than or
equal to the value of the child node.
• Therefore, we can say that there are two types of
heap.
– Max Heap
– Min Heap
7/15/2021 5.5 _ Types of Queue and its Applications 147
Max heap
• The max heap is a heap in which the value of
the parent node is greater than the value of
the child nodes.
7/15/2021 5.5 _ Types of Queue and its Applications 148
Min heap
• The min heap is a heap in which the value of
the parent node is less than the value of the
child nodes.
7/15/2021 5.5 _ Types of Queue and its Applications 149
Applications of Priority Queue
• CPU Scheduling
• It is used in the Dijkstra's shortest path algorithm.
• It is used in prim's algorithm
• It is used in data compression techniques like Huffman
code.
• It is used in heap sort.
• It is also used in operating system like
– priority scheduling,
– load balancing and
– interrupt handling.
7/15/2021 5.5 _ Types of Queue and its Applications 150
Applications of Queue
• Queue is used when things don’t have to be processed
immediately, but have to be processed in First In First Out order
like Breadth First Search.
• 1) When a resource is shared among multiple consumers. Examples
include CPU scheduling, Disk Scheduling.
2) When data is transferred asynchronously (data not necessarily
received at same rate as sent) between two processes. Examples
include IO Buffers, pipes, file IO, etc.
3) In Operating systems:
a) Semaphores
b) FCFS ( first come first serve) scheduling, example: FIFO queue
c) Spooling in printers
d) Buffer for devices like keyboard
4) In Networks:
a) Queues in routers/ switches
b) Mail Queues
5) Variations: (Deque, Priority Queue, Doubly Ended Priority
Queue )
7/15/2021 5.5 _ Types of Queue and its Applications 151
Thank you
7/15/2021 5.5 _ Types of Queue and its Applications 152

More Related Content

What's hot

What's hot (20)

Linked List
Linked ListLinked List
Linked List
 
Programming flowcharts for C Language
Programming flowcharts for C LanguageProgramming flowcharts for C Language
Programming flowcharts for C Language
 
Introduction to data structure
Introduction to data structure Introduction to data structure
Introduction to data structure
 
Introduction to Object Oriented Programming
Introduction to Object Oriented ProgrammingIntroduction to Object Oriented Programming
Introduction to Object Oriented Programming
 
Python recursion
Python recursionPython recursion
Python recursion
 
Linear and Binary search
Linear and Binary searchLinear and Binary search
Linear and Binary search
 
Terminology of tree
Terminology of treeTerminology of tree
Terminology of tree
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Object-oriented concepts
Object-oriented conceptsObject-oriented concepts
Object-oriented concepts
 
linked list in Data Structure, Simple and Easy Tutorial
linked list in Data Structure, Simple and Easy Tutoriallinked list in Data Structure, Simple and Easy Tutorial
linked list in Data Structure, Simple and Easy Tutorial
 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursion
 
Algorithm and flowchart
Algorithm and flowchartAlgorithm and flowchart
Algorithm and flowchart
 
Data Flow Diagram
Data Flow DiagramData Flow Diagram
Data Flow Diagram
 
Stack
StackStack
Stack
 
Linked list
Linked list Linked list
Linked list
 
Bottom up parser
Bottom up parserBottom up parser
Bottom up parser
 
Breadth First Search & Depth First Search
Breadth First Search & Depth First SearchBreadth First Search & Depth First Search
Breadth First Search & Depth First Search
 
Data structure lecture 1
Data structure lecture 1Data structure lecture 1
Data structure lecture 1
 
Python set
Python setPython set
Python set
 
Singly & Circular Linked list
Singly & Circular Linked listSingly & Circular Linked list
Singly & Circular Linked list
 

Similar to Stack and Queue

stack_presentaton_HUSNAIN[2].pojklklklptx
stack_presentaton_HUSNAIN[2].pojklklklptxstack_presentaton_HUSNAIN[2].pojklklklptx
stack_presentaton_HUSNAIN[2].pojklklklptxHusnainNaqvi2
 
Difference between stack and queue
Difference between stack and queueDifference between stack and queue
Difference between stack and queuePulkitmodi1998
 
VCE Unit 03vv.pptx
VCE Unit 03vv.pptxVCE Unit 03vv.pptx
VCE Unit 03vv.pptxskilljiolms
 
Chapter 5 Stack and Queue.pdf
Chapter 5 Stack and Queue.pdfChapter 5 Stack and Queue.pdf
Chapter 5 Stack and Queue.pdfGirT2
 
Unit 1_Stack and Queue using Linked Organization.pdf
Unit 1_Stack and Queue using Linked Organization.pdfUnit 1_Stack and Queue using Linked Organization.pdf
Unit 1_Stack and Queue using Linked Organization.pdfKanchanPatil34
 
Chapter 5-stack.pptx
Chapter 5-stack.pptxChapter 5-stack.pptx
Chapter 5-stack.pptxHalid Assen
 
Stacks-and-Queues.pdf
Stacks-and-Queues.pdfStacks-and-Queues.pdf
Stacks-and-Queues.pdfTobyWtf
 
CD3291 2.5 stack.pptx
CD3291 2.5 stack.pptxCD3291 2.5 stack.pptx
CD3291 2.5 stack.pptxmareeswari15
 
Algorithm and Data Structure - Stack
Algorithm and Data Structure - StackAlgorithm and Data Structure - Stack
Algorithm and Data Structure - StackAndiNurkholis1
 
Ds stack & queue
Ds   stack & queueDs   stack & queue
Ds stack & queueSunipa Bera
 
stacks and queues for public
stacks and queues for publicstacks and queues for public
stacks and queues for publiciqbalphy1
 
DSA- Unit III- STACK AND QUEUE
DSA- Unit III- STACK AND QUEUEDSA- Unit III- STACK AND QUEUE
DSA- Unit III- STACK AND QUEUEswathirajstar
 
Stack Data Structure with Static Implementation (2).pptx
Stack Data Structure with Static Implementation (2).pptxStack Data Structure with Static Implementation (2).pptx
Stack Data Structure with Static Implementation (2).pptxEmroz Sardar
 
Stack and its applications
Stack and its applicationsStack and its applications
Stack and its applicationsAhsan Mansiv
 
stack slide vghbjnkmfgvhbjnmkgvbhjnkmgbhjnkm
stack slide vghbjnkmfgvhbjnmkgvbhjnkmgbhjnkmstack slide vghbjnkmfgvhbjnmkgvbhjnkmgbhjnkm
stack slide vghbjnkmfgvhbjnmkgvbhjnkmgbhjnkmxadihe1593
 

Similar to Stack and Queue (20)

stack_presentaton_HUSNAIN[2].pojklklklptx
stack_presentaton_HUSNAIN[2].pojklklklptxstack_presentaton_HUSNAIN[2].pojklklklptx
stack_presentaton_HUSNAIN[2].pojklklklptx
 
Difference between stack and queue
Difference between stack and queueDifference between stack and queue
Difference between stack and queue
 
VCE Unit 03vv.pptx
VCE Unit 03vv.pptxVCE Unit 03vv.pptx
VCE Unit 03vv.pptx
 
Stacks
StacksStacks
Stacks
 
Chapter 5 Stack and Queue.pdf
Chapter 5 Stack and Queue.pdfChapter 5 Stack and Queue.pdf
Chapter 5 Stack and Queue.pdf
 
Unit 1_Stack and Queue using Linked Organization.pdf
Unit 1_Stack and Queue using Linked Organization.pdfUnit 1_Stack and Queue using Linked Organization.pdf
Unit 1_Stack and Queue using Linked Organization.pdf
 
Chapter 5-stack.pptx
Chapter 5-stack.pptxChapter 5-stack.pptx
Chapter 5-stack.pptx
 
STACK.pptx
STACK.pptxSTACK.pptx
STACK.pptx
 
Stack in C.pptx
Stack in C.pptxStack in C.pptx
Stack in C.pptx
 
Stacks-and-Queues.pdf
Stacks-and-Queues.pdfStacks-and-Queues.pdf
Stacks-and-Queues.pdf
 
CD3291 2.5 stack.pptx
CD3291 2.5 stack.pptxCD3291 2.5 stack.pptx
CD3291 2.5 stack.pptx
 
Algorithm and Data Structure - Stack
Algorithm and Data Structure - StackAlgorithm and Data Structure - Stack
Algorithm and Data Structure - Stack
 
Ds stack & queue
Ds   stack & queueDs   stack & queue
Ds stack & queue
 
stacks and queues for public
stacks and queues for publicstacks and queues for public
stacks and queues for public
 
Stack
StackStack
Stack
 
Stack and its operations
Stack and its operationsStack and its operations
Stack and its operations
 
DSA- Unit III- STACK AND QUEUE
DSA- Unit III- STACK AND QUEUEDSA- Unit III- STACK AND QUEUE
DSA- Unit III- STACK AND QUEUE
 
Stack Data Structure with Static Implementation (2).pptx
Stack Data Structure with Static Implementation (2).pptxStack Data Structure with Static Implementation (2).pptx
Stack Data Structure with Static Implementation (2).pptx
 
Stack and its applications
Stack and its applicationsStack and its applications
Stack and its applications
 
stack slide vghbjnkmfgvhbjnmkgvbhjnkmgbhjnkm
stack slide vghbjnkmfgvhbjnmkgvbhjnkmgbhjnkmstack slide vghbjnkmfgvhbjnmkgvbhjnkmgbhjnkm
stack slide vghbjnkmfgvhbjnmkgvbhjnkmgbhjnkm
 

More from Selvaraj Seerangan

Unit 2,3,4 _ Internet of Things A Hands-On Approach (Arshdeep Bahga, Vijay Ma...
Unit 2,3,4 _ Internet of Things A Hands-On Approach (Arshdeep Bahga, Vijay Ma...Unit 2,3,4 _ Internet of Things A Hands-On Approach (Arshdeep Bahga, Vijay Ma...
Unit 2,3,4 _ Internet of Things A Hands-On Approach (Arshdeep Bahga, Vijay Ma...Selvaraj Seerangan
 
END SEM _ Design Thinking _ 16 Templates.pptx
END SEM _ Design Thinking _ 16 Templates.pptxEND SEM _ Design Thinking _ 16 Templates.pptx
END SEM _ Design Thinking _ 16 Templates.pptxSelvaraj Seerangan
 
Design Thinking _ Complete Templates.pptx
Design Thinking _ Complete Templates.pptxDesign Thinking _ Complete Templates.pptx
Design Thinking _ Complete Templates.pptxSelvaraj Seerangan
 
CAT 3 _ List of Templates.pptx
CAT 3 _ List of Templates.pptxCAT 3 _ List of Templates.pptx
CAT 3 _ List of Templates.pptxSelvaraj Seerangan
 
[PPT] _ Unit 3 _ Experiment.pptx
[PPT] _ Unit 3 _ Experiment.pptx[PPT] _ Unit 3 _ Experiment.pptx
[PPT] _ Unit 3 _ Experiment.pptxSelvaraj Seerangan
 
CAT 2 _ List of Templates.pptx
CAT 2 _ List of Templates.pptxCAT 2 _ List of Templates.pptx
CAT 2 _ List of Templates.pptxSelvaraj Seerangan
 
Design Thinking - Empathize Phase
Design Thinking - Empathize PhaseDesign Thinking - Empathize Phase
Design Thinking - Empathize PhaseSelvaraj Seerangan
 
18CSL51 - Network Lab Manual.pdf
18CSL51 - Network Lab Manual.pdf18CSL51 - Network Lab Manual.pdf
18CSL51 - Network Lab Manual.pdfSelvaraj Seerangan
 
CAT 1 _ List of Templates.pptx
CAT 1 _ List of Templates.pptxCAT 1 _ List of Templates.pptx
CAT 1 _ List of Templates.pptxSelvaraj Seerangan
 
[PPT] _ UNIT 1 _ COMPLETE.pptx
[PPT] _ UNIT 1 _ COMPLETE.pptx[PPT] _ UNIT 1 _ COMPLETE.pptx
[PPT] _ UNIT 1 _ COMPLETE.pptxSelvaraj Seerangan
 
[PPT] _ Unit 2 _ 9.0 _ Domain Specific IoT _Home Automation.pdf
[PPT] _ Unit 2 _ 9.0 _ Domain Specific IoT _Home Automation.pdf[PPT] _ Unit 2 _ 9.0 _ Domain Specific IoT _Home Automation.pdf
[PPT] _ Unit 2 _ 9.0 _ Domain Specific IoT _Home Automation.pdfSelvaraj Seerangan
 

More from Selvaraj Seerangan (20)

Unit 2,3,4 _ Internet of Things A Hands-On Approach (Arshdeep Bahga, Vijay Ma...
Unit 2,3,4 _ Internet of Things A Hands-On Approach (Arshdeep Bahga, Vijay Ma...Unit 2,3,4 _ Internet of Things A Hands-On Approach (Arshdeep Bahga, Vijay Ma...
Unit 2,3,4 _ Internet of Things A Hands-On Approach (Arshdeep Bahga, Vijay Ma...
 
Unit 5 _ Fog Computing .pdf
Unit 5 _ Fog Computing .pdfUnit 5 _ Fog Computing .pdf
Unit 5 _ Fog Computing .pdf
 
CAT III Answer Key.pdf
CAT III Answer Key.pdfCAT III Answer Key.pdf
CAT III Answer Key.pdf
 
END SEM _ Design Thinking _ 16 Templates.pptx
END SEM _ Design Thinking _ 16 Templates.pptxEND SEM _ Design Thinking _ 16 Templates.pptx
END SEM _ Design Thinking _ 16 Templates.pptx
 
Design Thinking _ Complete Templates.pptx
Design Thinking _ Complete Templates.pptxDesign Thinking _ Complete Templates.pptx
Design Thinking _ Complete Templates.pptx
 
CAT 3 _ List of Templates.pptx
CAT 3 _ List of Templates.pptxCAT 3 _ List of Templates.pptx
CAT 3 _ List of Templates.pptx
 
[PPT] _ Unit 5 _ Evolve.pptx
[PPT] _ Unit 5 _ Evolve.pptx[PPT] _ Unit 5 _ Evolve.pptx
[PPT] _ Unit 5 _ Evolve.pptx
 
[PPT] _ Unit 4 _ Engage.pptx
[PPT] _ Unit 4 _ Engage.pptx[PPT] _ Unit 4 _ Engage.pptx
[PPT] _ Unit 4 _ Engage.pptx
 
[PPT] _ Unit 3 _ Experiment.pptx
[PPT] _ Unit 3 _ Experiment.pptx[PPT] _ Unit 3 _ Experiment.pptx
[PPT] _ Unit 3 _ Experiment.pptx
 
CAT 2 _ List of Templates.pptx
CAT 2 _ List of Templates.pptxCAT 2 _ List of Templates.pptx
CAT 2 _ List of Templates.pptx
 
Design Thinking - Empathize Phase
Design Thinking - Empathize PhaseDesign Thinking - Empathize Phase
Design Thinking - Empathize Phase
 
CAT-II Answer Key.pdf
CAT-II Answer Key.pdfCAT-II Answer Key.pdf
CAT-II Answer Key.pdf
 
PSP LAB MANUAL.pdf
PSP LAB MANUAL.pdfPSP LAB MANUAL.pdf
PSP LAB MANUAL.pdf
 
18CSL51 - Network Lab Manual.pdf
18CSL51 - Network Lab Manual.pdf18CSL51 - Network Lab Manual.pdf
18CSL51 - Network Lab Manual.pdf
 
DS LAB MANUAL.pdf
DS LAB MANUAL.pdfDS LAB MANUAL.pdf
DS LAB MANUAL.pdf
 
CAT 1 _ List of Templates.pptx
CAT 1 _ List of Templates.pptxCAT 1 _ List of Templates.pptx
CAT 1 _ List of Templates.pptx
 
[PPT] _ UNIT 1 _ COMPLETE.pptx
[PPT] _ UNIT 1 _ COMPLETE.pptx[PPT] _ UNIT 1 _ COMPLETE.pptx
[PPT] _ UNIT 1 _ COMPLETE.pptx
 
CAT-1 Answer Key.doc
CAT-1 Answer Key.docCAT-1 Answer Key.doc
CAT-1 Answer Key.doc
 
Unit 3 Complete.pptx
Unit 3 Complete.pptxUnit 3 Complete.pptx
Unit 3 Complete.pptx
 
[PPT] _ Unit 2 _ 9.0 _ Domain Specific IoT _Home Automation.pdf
[PPT] _ Unit 2 _ 9.0 _ Domain Specific IoT _Home Automation.pdf[PPT] _ Unit 2 _ 9.0 _ Domain Specific IoT _Home Automation.pdf
[PPT] _ Unit 2 _ 9.0 _ Domain Specific IoT _Home Automation.pdf
 

Recently uploaded

Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwaitjaanualu31
 
Fundamentals of Internet of Things (IoT) Part-2
Fundamentals of Internet of Things (IoT) Part-2Fundamentals of Internet of Things (IoT) Part-2
Fundamentals of Internet of Things (IoT) Part-2ChandrakantDivate1
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdfKamal Acharya
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayEpec Engineered Technologies
 
INTERRUPT CONTROLLER 8259 MICROPROCESSOR
INTERRUPT CONTROLLER 8259 MICROPROCESSORINTERRUPT CONTROLLER 8259 MICROPROCESSOR
INTERRUPT CONTROLLER 8259 MICROPROCESSORTanishkaHira1
 
Databricks Generative AI Fundamentals .pdf
Databricks Generative AI Fundamentals  .pdfDatabricks Generative AI Fundamentals  .pdf
Databricks Generative AI Fundamentals .pdfVinayVadlagattu
 
Augmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptxAugmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptxMustafa Ahmed
 
Electromagnetic relays used for power system .pptx
Electromagnetic relays used for power system .pptxElectromagnetic relays used for power system .pptx
Electromagnetic relays used for power system .pptxNANDHAKUMARA10
 
Danikor Product Catalog- Screw Feeder.pdf
Danikor Product Catalog- Screw Feeder.pdfDanikor Product Catalog- Screw Feeder.pdf
Danikor Product Catalog- Screw Feeder.pdfthietkevietthinh
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startQuintin Balsdon
 
Path loss model, OKUMURA Model, Hata Model
Path loss model, OKUMURA Model, Hata ModelPath loss model, OKUMURA Model, Hata Model
Path loss model, OKUMURA Model, Hata ModelDrAjayKumarYadav4
 
Introduction to Geographic Information Systems
Introduction to Geographic Information SystemsIntroduction to Geographic Information Systems
Introduction to Geographic Information SystemsAnge Felix NSANZIYERA
 
8086 Microprocessor Architecture: 16-bit microprocessor
8086 Microprocessor Architecture: 16-bit microprocessor8086 Microprocessor Architecture: 16-bit microprocessor
8086 Microprocessor Architecture: 16-bit microprocessorAshwiniTodkar4
 
8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...josephjonse
 
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...HenryBriggs2
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdfKamal Acharya
 
Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...
Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...
Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...ssuserdfc773
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxSCMS School of Architecture
 

Recently uploaded (20)

Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
 
Fundamentals of Internet of Things (IoT) Part-2
Fundamentals of Internet of Things (IoT) Part-2Fundamentals of Internet of Things (IoT) Part-2
Fundamentals of Internet of Things (IoT) Part-2
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdf
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
INTERRUPT CONTROLLER 8259 MICROPROCESSOR
INTERRUPT CONTROLLER 8259 MICROPROCESSORINTERRUPT CONTROLLER 8259 MICROPROCESSOR
INTERRUPT CONTROLLER 8259 MICROPROCESSOR
 
Databricks Generative AI Fundamentals .pdf
Databricks Generative AI Fundamentals  .pdfDatabricks Generative AI Fundamentals  .pdf
Databricks Generative AI Fundamentals .pdf
 
Augmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptxAugmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptx
 
Electromagnetic relays used for power system .pptx
Electromagnetic relays used for power system .pptxElectromagnetic relays used for power system .pptx
Electromagnetic relays used for power system .pptx
 
Danikor Product Catalog- Screw Feeder.pdf
Danikor Product Catalog- Screw Feeder.pdfDanikor Product Catalog- Screw Feeder.pdf
Danikor Product Catalog- Screw Feeder.pdf
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
Path loss model, OKUMURA Model, Hata Model
Path loss model, OKUMURA Model, Hata ModelPath loss model, OKUMURA Model, Hata Model
Path loss model, OKUMURA Model, Hata Model
 
Introduction to Geographic Information Systems
Introduction to Geographic Information SystemsIntroduction to Geographic Information Systems
Introduction to Geographic Information Systems
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
8086 Microprocessor Architecture: 16-bit microprocessor
8086 Microprocessor Architecture: 16-bit microprocessor8086 Microprocessor Architecture: 16-bit microprocessor
8086 Microprocessor Architecture: 16-bit microprocessor
 
8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...
 
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdf
 
Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...
Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...
Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
 

Stack and Queue

  • 1. UNIT V : Stack and Queue By Mr.S.Selvaraj Asst. Professor (SRG) / CSE Kongu Engineering College Perundurai, Erode, Tamilnadu, India Thanks to and Resource from : Sumitabha Das, “Computer Fundamentals and C Programming”, 1st Edition, McGraw Hill, 2018. 20CST21 – Programming and Linear Data Structures
  • 2. Syllabus – Unit Wise 7/15/2021 5.1 _ Introduction to Stack 2
  • 3. List of Exercises 7/15/2021 5.1 _ Introduction to Stack 3
  • 4. Text Book and Reference Book 7/15/2021 5.1 _ Introduction to Stack 4
  • 5. Unit IV : Contents 1. Introduction to Stack 2. Implementation of stack using – Array and linked list 3. Application of stack – Infix to Postfix expression conversion – Postfix expression evaluation 4. Introduction to Queue 5. Implementation of Queue using – Array and linked list 6. Other variations of Queue 7. Applications of Queue 7/15/2021 5 5.1 _ Introduction to Stack
  • 6. Introduction to Stack • Stack is a simple data structure which has bounded capacity. • In stack, data is added and removed at only one end called the Top. • When an item is inserted on to the stack, it is placed on Top of the stack. • When an item is removed from the stack, it removes the Top most element. • There are two basic operations performed on a Stack: – Push() – Pop() 7/15/2021 5.1 _ Introduction to Stack 6
  • 7. Introduction to Stack • In stack terminology, – insertion operation is called Push() operation and – removal operation is called Pop() operation. • The last element that is pushed into the stack is the first element to be popped out of the stack. • That is, it follows Last-In First-Out (LIFO) strategy. • Some of the features of stack are as follows: 7/15/2021 5.1 _ Introduction to Stack 7
  • 8. Features of Stack • Stack is an ordered list of similar type of elements. • It is a linear list where all insertions and deletions are permitted only at one end of the list called Top. • Stack is a LIFO (Last-In-First-Out) structure. • When a stack is full then it is said to be in Overflow state • When a stack is empty then it is said to be in Underflow state. 7/15/2021 5.1 _ Introduction to Stack 8
  • 9. Stack Data Structure 7/15/2021 5.1 _ Introduction to Stack 9
  • 10. Stack - Push Operation 7/15/2021 5.1 _ Introduction to Stack 10
  • 11. Pop Operation 7/15/2021 5.1 _ Introduction to Stack 11
  • 12. Application of Stack in Real time scenario • A stack of plates on the dining table • Wearing and Removing of Bangles in our hand 7/15/2021 5.1 _ Introduction to Stack 12
  • 13. Applications of stack in Computer Science and Programming • Support of function calls – When a function is invoked, the function which is invoked last will be completed first. This is the property of stack. – Primary purpose is to store the return address. • Support of recursive functions – When a function calls itself recursively, a return address needs to be stored for each activation of the function so that it can later be used to return from the function activation. • An "undo/redo" mechanism in text editors • Expression Evaluation – Used to evaluate prefix, postfix and infix expressions. • Expression Conversion – An expression can be represented in prefix, postfix or infix form. Stack can be used to convert one form of expression to another • Reverse a string – Push a given word to stack letter by letter and then pop letters from the stack one by one • Forward and backward feature in web browsers • Parenthesis Checking – Stack is used to check the proper opening and closing of parenthesis 7/15/2021 5.1 _ Introduction to Stack 13
  • 14. Implementation of Stack • Stack can be implemented using any one of the following data structures: – Array – Linked List • Implementing a stack using array can store fixed number of data values but using linked list hold any number of elements . 7/15/2021 5.1 _ Introduction to Stack 14
  • 15. Stack using Array • Stack can be implemented using one dimensional array. • Array can be created in predefined size. • We can not increase the size of the array if we have more elements to insert. • That is, Stack implementation with array can hold only fixed number of elements 7/15/2021 5.1 _ Introduction to Stack 15
  • 16. Stack using Array • we need an integer variable to keep track of Top most element in the stack. • It should be initialized with -1 to indicate stack empty. 7/15/2021 5.1 _ Introduction to Stack 16
  • 17. Stack using Array • In the above definition, SIZE is a constant which represents maximum number of elements stored in a stack. • Value of Top = SIZE – 1 indicates stack is full. • In a stack, initially Top is set to -1. • Top is used to keep track of the index of the Top most element. 7/15/2021 5.1 _ Introduction to Stack 17
  • 18. Stack using Array • The following table represents status of stack for different values of Top. 7/15/2021 5.1 _ Introduction to Stack 18
  • 19. Push(value) - Inserting value into the stack • Step 1: Check whether stack is already FULL. Stack is Full when Top= SIZE-1. • Step 2: If it is FULL then display "Stack is FULL" and stop the function. • Step 3: If it is not FULL, then increment Top value by one and store the value at stk[Top]. 7/15/2021 5.1 _ Introduction to Stack 19
  • 20. Pop() - Remove a value from the Stack • Step 1: Check whether stack is EMPTY. It is Empty when Top= -1 • Step 2: If it is EMPTY, then display "Stack is Empty" and stop the function. • Step 3: If it is not EMPTY, then delete Top most element (ie., stack[Top]) and decrement Top value by one. Top = Top -1 7/15/2021 5.1 _ Introduction to Stack 20
  • 21. Display() - Display the elements of a Stack • Step 1: Check whether stack is EMPTY. • Step 2: If it is EMPTY, then display "Stack is Empty" and terminate the function. • Step 3: Initialize a variable 'i' with a value Top. • Step 4: Display the value of stack[i] and decrement the value of i by one • Step 5: Repeat step 4 until “i” value becomes 0. 7/15/2021 5.1 _ Introduction to Stack 21
  • 22. 7/15/2021 5.1 _ Introduction to Stack 22 Stack using Array
  • 23. 7/15/2021 5.1 _ Introduction to Stack 23
  • 24. 7/15/2021 5.1 _ Introduction to Stack 24
  • 25. 7/15/2021 5.1 _ Introduction to Stack 25
  • 26. 7/15/2021 5.1 _ Introduction to Stack 26
  • 27. 7/15/2021 5.1 _ Introduction to Stack 27
  • 28. Thank you 7/15/2021 5.1 _ Introduction to Stack 28
  • 29. UNIT V : Stack and Queue By Mr.S.Selvaraj Asst. Professor (SRG) / CSE Kongu Engineering College Perundurai, Erode, Tamilnadu, India Thanks to and Resource from : Sumitabha Das, “Computer Fundamentals and C Programming”, 1st Edition, McGraw Hill, 2018. 20CST21 – Programming and Linear Data Structures
  • 30. Unit IV : Contents 1. Introduction to Stack 2. Implementation of stack using – Array and linked list 3. Application of stack – Infix to Postfix expression conversion – Postfix expression evaluation 4. Introduction to Queue 5. Implementation of Queue using – Array and linked list 6. Other variations of Queue 7. Applications of Queue 7/15/2021 30 5.2 _ Implementation of Stack
  • 31. Implementation of Stack • Stack can be implemented using any one of the following data structures: – Array – Linked List • Implementing a stack using array can store fixed number of data values but using linked list hold any number of elements . 7/15/2021 5.2 _ Implementation of Stack 31
  • 32. Stack using Array • Stack can be implemented using one dimensional array. • Array can be created in predefined size. • We can not increase the size of the array if we have more elements to insert. • That is, Stack implementation with array can hold only fixed number of elements 7/15/2021 5.2 _ Implementation of Stack 32
  • 33. Stack using Array • we need an integer variable to keep track of Top most element in the stack. • It should be initialized with -1 to indicate stack empty. 7/15/2021 5.2 _ Implementation of Stack 33
  • 34. Stack using Array • In the above definition, SIZE is a constant which represents maximum number of elements stored in a stack. • Value of Top = SIZE – 1 indicates stack is full. • In a stack, initially Top is set to -1. • Top is used to keep track of the index of the Top most element. 7/15/2021 5.2 _ Implementation of Stack 34
  • 35. Stack using Array • The following table represents status of stack for different values of Top. 7/15/2021 5.2 _ Implementation of Stack 35
  • 36. Push(value) - Inserting value into the stack • Step 1: Check whether stack is already FULL. Stack is Full when Top= SIZE-1. • Step 2: If it is FULL then display "Stack is FULL" and stop the function. • Step 3: If it is not FULL, then increment Top value by one and store the value at stk[Top]. 7/15/2021 5.2 _ Implementation of Stack 36
  • 37. Pop() - Remove a value from the Stack • Step 1: Check whether stack is EMPTY. It is Empty when Top= -1 • Step 2: If it is EMPTY, then display "Stack is Empty" and stop the function. • Step 3: If it is not EMPTY, then delete Top most element (ie., stack[Top]) and decrement Top value by one. Top = Top -1 7/15/2021 5.2 _ Implementation of Stack 37
  • 38. Display() - Display the elements of a Stack • Step 1: Check whether stack is EMPTY. • Step 2: If it is EMPTY, then display "Stack is Empty" and terminate the function. • Step 3: Initialize a variable 'i' with a value Top. • Step 4: Display the value of stack[i] and decrement the value of i by one • Step 5: Repeat step 4 until “i” value becomes 0. 7/15/2021 5.2 _ Implementation of Stack 38
  • 39. 7/15/2021 5.2 _ Implementation of Stack 39 Stack using Array
  • 40. 7/15/2021 5.2 _ Implementation of Stack 40
  • 41. 7/15/2021 5.2 _ Implementation of Stack 41
  • 42. 7/15/2021 5.2 _ Implementation of Stack 42
  • 43. 7/15/2021 5.2 _ Implementation of Stack 43
  • 44. 7/15/2021 5.2 _ Implementation of Stack 44
  • 45. Array implementation of stack • Pros: Easy to implement. Memory is not efficiently utilized as pointers are not involved. • Cons: It is not dynamic. It doesn’t grow and shrink depending on needs at runtime 7/15/2021 45 5.2 _ Implementation of Stack
  • 46. Linked list Implementation of Stack //Initially, struct stack { int data; struct stack *next; }*head,*temp; //Function Prototype void push(void); void pop(void); void display(void); int isempty(void); 7/15/2021 46 5.2 _ Implementation of Stack
  • 47. Linked list Implementation of Stack void push() { int x; printf("n enter the numbern"); scanf("%d",&x); temp=(struct stack *)malloc(sizeof(struct stack)); temp->data=x; temp->next=NULL; if(head==NULL) { head=temp; } else { temp->next=head; head=temp; } } 7/15/2021 47 5.2 _ Implementation of Stack
  • 48. Linked list Implementation of Stack int isempty() { if(head==NULL) return 1; else return 0; } void pop() { if(isempty()) printf("n stack is emptyn"); else { printf("n The popped element is %d",head->data); head=head->next; } } 7/15/2021 48 5.2 _ Implementation of Stack
  • 49. Linked list Implementation of Stack void display() { if(isempty()) printf("n stack is emptyn"); else { temp=head; while(temp!=NULL) { printf("%d->",temp->data); temp=temp->next; } printf("NULL"); } } 7/15/2021 49 5.2 _ Implementation of Stack
  • 50. Linked list Implementation of Stack int main() { int c; while(1) { printf("n1.push 2.pop 3.display 4.exitn"); scanf("%d",&c); switch(c) { case 1: push(); break; case 2: pop(); break; 7/15/2021 50 5.2 _ Implementation of Stack
  • 51. Linked list Implementation of Stack case 3: display(); break; case 4: exit(0); } } return 0; } 7/15/2021 51 5.2 _ Implementation of Stack
  • 52. Thank you 7/15/2021 5.2 _ Implementation of Stack 52
  • 53. UNIT V : Stack and Queue By Mr.S.Selvaraj Asst. Professor (SRG) / CSE Kongu Engineering College Perundurai, Erode, Tamilnadu, India Thanks to and Resource from : Sumitabha Das, “Computer Fundamentals and C Programming”, 1st Edition, McGraw Hill, 2018. 20CST21 – Programming and Linear Data Structures
  • 54. Unit IV : Contents 1. Introduction to Stack 2. Implementation of stack using – Array and linked list 3. Application of stack – Infix to Postfix expression conversion – Postfix expression evaluation 4. Introduction to Queue 5. Implementation of Queue using – Array and linked list 6. Other variations of Queue 7. Applications of Queue 7/15/2021 54 5.3 _ Application of Stack
  • 55. Applications of Stack Balancing Parenthesis Postfix Expressions Evaluation Infix to Postfix Conversion Towers of Hanoi problem String Reversal sudoku solver 7/15/2021 55 5.3 _ Application of Stack
  • 56. Infix, Prefix, Postfix Expression • Infix expression: – An expression is called the Infix expression if the operator appears in between the operands in the expression. – Simply of the form (operand1 operator operand2). – Operators are written in-between their operands. X + Y A * ( B + C ) / D • Prefix expression (also known as "Polish notation"): – An expression is called the Prefix expression if the operator appears before the operands in the expression. – Simply of the form (operator operand1 operand2). – Operators are written before their operands. + X Y / * A + B C D 7/15/2021 56 5.3 _ Application of Stack
  • 57. Infix, Prefix, Postfix Expression • Postfix expression (also known as "Reverse Polish notation"): – An expression is called the postfix expression if the operator appears in the expression after the operands. – Simply of the form (operand1 operand2 operator). – Operators are written after their operands. X Y + A B C + * D / 7/15/2021 57 5.3 _ Application of Stack
  • 58. Infix, Prefix, Postfix Expression 7/15/2021 58 5.3 _ Application of Stack
  • 59. Infix to Postfix Conversion Algorithm 1. Scan the infix expression from left to right. 2. If the scanned character is an operand, output it. 3. Else, …..3.1 If the precedence of the scanned operator is greater than the precedence of the operator in the stack or the stack is empty or the stack contains a ‘(‘, push it. …..3.2 Else, Pop all the operators from the stack which are greater than or equal to in precedence than that of the scanned operator. After doing that Push the scanned operator to the stack. (If you encounter parenthesis while popping then stop there and push the scanned operator in the stack.) 4. If the scanned character is an ‘(‘, push it to the stack. 5. If the scanned character is an ‘)’, pop the stack and output all the operators until a ‘(‘ is encountered, and discard both the parenthesis. 6. Repeat steps 2-6 until infix expression is scanned. 7. Print the output 8. Pop and output from the stack until it is not empty. 7/15/2021 59 5.3 _ Application of Stack
  • 60. An illustration • First, the symbol a is read, so it is passed through to the output. Then '+' is read and pushed onto the stack. • Next b is read and passed through to the output • Next a '*' is read. The top entry on the operator stack has lower precedence than '*', so nothing is output and '*' is put on the stack. • Next, c is read and output. 7/15/2021 60 5.3 _ Application of Stack
  • 61. An illustration • The next symbol is a '+'. Checking the stack, we find that we will pop a '*' and place it on the output, pop the other '+', which is not of lower but equal priority, on the stack, and then push the '+'. • The next symbol read is an '(', which, being of highest precedence, is placed on the stack. Then d is read and output. 7/15/2021 61 5.3 _ Application of Stack
  • 62. An illustration • We continue by reading a '*'. Since open parentheses do not get removed except when a closed parenthesis is being processed, there is no output. Next, e is read and output. • The next symbol read is a '+'. We pop and output '*' and then push '+'. Then we read and output 7/15/2021 62 5.3 _ Application of Stack
  • 63. An illustration • Now we read a ')', so the stack is emptied back to the '('. We output a '+'. • We read a '*' next; it is pushed onto the stack. Then g is read and output. 7/15/2021 63 5.3 _ Application of Stack
  • 64. An illustration • The input is now empty, so we pop and output symbols from the stack until it is empty. 7/15/2021 64 5.3 _ Application of Stack
  • 65. 7/15/2021 65 5.3 _ Application of Stack
  • 66. 7/15/2021 66 5.3 _ Application of Stack
  • 67. 7/15/2021 67 5.3 _ Application of Stack
  • 68. PROBLEM 1. 3+4*5/6 2. (300+23)*(43-21)/(84+7) 3. (4+8)*(6-5)/((3-2)*(2+2)) 7/15/2021 68 5.3 _ Application of Stack
  • 69. ANSWER 1. 3 4 5 * 6 / + 2. 300 23 + 43 21 -* 84 7 + / 3. 4 8 + 6 5 -* 3 2 –2 2 + * / 7/15/2021 69 5.3 _ Application of Stack
  • 70. Problems 1) Infix : (A+B) * (C-D) Infix to Postfix : AB+CD-* 2) Infix : A-(B/C)*(A/K)-L Infix to Postfix : ABC/-AK/L-* 7/15/2021 70 5.3 _ Application of Stack
  • 71. Expression Evaluation The simple algorithm uses a stack and is as follows: 1. Make an empty stack. 2. Read an input string. 3. Read characters one by one until end of string is encountered. 4. If the character is a digit, convert it into an integer and push onto the stack. 5. If the character is not a digit, pop two numbers from the stack and perform the corresponding operation and push the result onto the stack. 6. At end of the string, pop the result from the stack 7/15/2021 71 5.3 _ Application of Stack
  • 72. ILLUSTRATION 7/15/2021 Next '+' is read, so 3 and 2 are popped from the stack and their sum, 5, is pushed. Next 8 is pushed. 72 5.3 _ Application of Stack
  • 73. 7/15/2021 Now '*' is read, so 8 and 5 are popped as 8 * 5 = 40 is pushed. 73 5.3 _ Application of Stack
  • 74. 7/15/2021 74 5.3 _ Application of Stack
  • 75. PROBLEM 1. 3+4*5/6 2. (300+23)*(43-21)/(84+7) 3. (4+8)*(6-5)/((3-2)*(2+2)) 7/15/2021 75 5.3 _ Application of Stack
  • 76. Thank you 7/15/2021 5.3 _ Application of Stack 76
  • 77. UNIT V : Stack and Queue By Mr.S.Selvaraj Asst. Professor (SRG) / CSE Kongu Engineering College Perundurai, Erode, Tamilnadu, India Thanks to and Resource from : Sumitabha Das, “Computer Fundamentals and C Programming”, 1st Edition, McGraw Hill, 2018. 20CST21 – Programming and Linear Data Structures
  • 78. Unit IV : Contents 1. Introduction to Stack 2. Implementation of stack using – Array and linked list 3. Application of stack – Infix to Postfix expression conversion – Postfix expression evaluation 4. Introduction to Queue 5. Implementation of Queue using – Array and linked list 6. Other variations of Queue 7. Applications of Queue 7/15/2021 78 5.4 _ Introduction to Queue and its Implementation
  • 79. Introduction to Queue 7/15/2021 5.4 _ Introduction to Queue and its Implementation 79 Front Rear
  • 80. Introduction to Queue • Queue is a simple linear data structure which is a ordered list of similar type of items. • In Queue, data is added at one end called the rear, and data is removed at the other end called the front. • Queue follows FIFO (First-In-First-Out) strategy i.e. The element which is inserted first is the one removed first. 7/15/2021 80 5.4 _ Introduction to Queue and its Implementation
  • 81. Queue Representation 7/15/2021 5.4 _ Introduction to Queue and its Implementation 81
  • 82. Queue ADT • The following operations are implemented in queue data structure: – Isfull() – used to check whether the queue is full. – Isempty() – used to check whether the queue is empty. – Enqueue() – used to add a new item to the end of the queue. (Insertion Operation) – Dequeue()- used to remove the front item from the queue. (Deletion Operation) – Display() - used to display all the elements in the queue. 7/15/2021 82 5.4 _ Introduction to Queue and its Implementation
  • 83. Applications of Queue • CPU scheduling – When multiple processes require CPU at the same time, CPU scheduling algorithms schedules the tasks for submission. Usually these algorithms are implemented using Queue data structure. • Printer Queue – Jobs submitted to a printer are printed in order of their arrival. • Call Center phone systems – All the calls are queued until a service representative is free. Calls are serviced based on the order of their arrival. 7/15/2021 83 5.4 _ Introduction to Queue and its Implementation
  • 84. Implementation Of Queue • There are two ways to implement a queue: –Sequential allocation Using array –Linked allocation Using linked list 7/15/2021 84 5.4 _ Introduction to Queue and its Implementation
  • 85. Linear Queue – Definition and Overflow 7/15/2021 5.4 _ Introduction to Queue and its Implementation 85
  • 86. Linear Queue – Underflow 7/15/2021 5.4 _ Introduction to Queue and its Implementation 86
  • 87. Linear Queue – Enqueue 7/15/2021 5.4 _ Introduction to Queue and its Implementation 87
  • 88. Linear Queue – Dequeue 7/15/2021 5.4 _ Introduction to Queue and its Implementation 88
  • 89. Linear Queue – Traversal / Display 7/15/2021 5.4 _ Introduction to Queue and its Implementation 89
  • 90. Array implementation of Queue Initially, #define MAX 3 int q[MAX], rear=-1, front=-1; Function Prototype void enqueue(void); void dequeue(void); int isfull(void); int isempty(void); void display(void); 7/15/2021 90 5.4 _ Introduction to Queue and its Implementation
  • 91. Array implementation of Queue int isfull() { if(rear==MAX-1) return 1; else return 0; } int isempty() { if(front==-1||front>rear) return 1; else return 0; } 7/15/2021 91 5.4 _ Introduction to Queue and its Implementation
  • 92. Array implementation of Queue void enqueue() { int x; if(isfull()) printf("n queue is fulln"); else { printf("n enter the numbern"); scanf("%d",&x); if(rear==-1) { front=0; q[++rear]=x; } else { q[++rear]=x; } } } 7/15/2021 92 5.4 _ Introduction to Queue and its Implementation
  • 93. Array implementation of Queue void dequeue() { if(isempty()) printf("n queue is emptyn"); else if(front==rear) { printf("n The dequeued element is %dn",q[front]); front=rear=-1; } else { printf("n The dequeued element is %dn",q[front]); front++; } } 7/15/2021 93 5.4 _ Introduction to Queue and its Implementation
  • 94. Array implementation of Queue void display() { int i; if(isempty()) printf("n queue is emptyn"); else { printf("n the elements in the queue aret"); for(i=front;i<=rear; i++) printf("%dt",q[i]); } } 7/15/2021 94 5.4 _ Introduction to Queue and its Implementation
  • 95. Array implementation of Queue int main() { int c; while(1) { printf("n1.enqueue 2.dequeue 3.display 4.exitn"); scanf("%d",&c); switch(c) { case 1: enqueue(); break; case 2: dequeue(); break; case 3: display(); break; case 4: exit(0); } } return 0; } 7/15/2021 95 5.4 _ Introduction to Queue and its Implementation
  • 96. Linked List implementation of Queue – Define and underflow 7/15/2021 5.4 _ Introduction to Queue and its Implementation 96
  • 97. Linked List implementation of Queue - Display 7/15/2021 5.4 _ Introduction to Queue and its Implementation 97
  • 98. Linked List implementation of Queue - Enqueue 7/15/2021 5.4 _ Introduction to Queue and its Implementation 98
  • 99. Linked List implementation of Queue - Enqueue 7/15/2021 5.4 _ Introduction to Queue and its Implementation 99
  • 100. Linked List implementation of Queue - Dequeue 7/15/2021 5.4 _ Introduction to Queue and its Implementation 100
  • 101. Linked List implementation of Queue struct queue { int data; struct queue *next; }*rear, *front,*temp; Function Prototype void enqueue(void); void dequeue(void); void display(void); int isempty(void); 7/15/2021 101 5.4 _ Introduction to Queue and its Implementation
  • 102. Linked List implementation of Queue void enqueue() { int x; printf("n enter the numbern"); scanf("%d",&x); temp=(struct queue *)malloc(sizeof(struct queue)); temp->data=x; temp->next=NULL; if(front==NULL) { front=rear=temp; } else { rear->next=temp; rear=temp; } } 7/15/2021 102 5.4 _ Introduction to Queue and its Implementation
  • 103. Linked List implementation of Queue int isempty() { if(front==NULL) return 1; else return 0; } void dequeue() { if(isempty()) printf("n queue is emptyn"); else { printf("n The deleted element is %d",front->data); front=front->next; } } 7/15/2021 103 5.4 _ Introduction to Queue and its Implementation
  • 104. Linked List implementation of Queue void display() { if(isempty()) printf("n queue is emptyn"); else { temp=front; while(temp!=NULL) { printf("%d->",temp->data); temp=temp->next; } printf("NULL"); } } 7/15/2021 104 5.4 _ Introduction to Queue and its Implementation
  • 105. Linked List implementation of Queue int main() { int c; while(1) { printf("n1.enqueue 2.dequeue 3.display 4.exitn"); scanf("%d",&c); switch(c) { case 1: enqueue(); break; case 2: dequeue(); break; 7/15/2021 105 5.4 _ Introduction to Queue and its Implementation
  • 106. Linked List implementation of Queue case 3: display(); break; case 4: exit(0); } } return 0; } 7/15/2021 106 5.4 _ Introduction to Queue and its Implementation
  • 107. Thank you 7/15/2021 5.4 _ Introduction to Queue and its Implementation 107
  • 108. UNIT V : Stack and Queue By Mr.S.Selvaraj Asst. Professor (SRG) / CSE Kongu Engineering College Perundurai, Erode, Tamilnadu, India Thanks to and Resource from : Sumitabha Das, “Computer Fundamentals and C Programming”, 1st Edition, McGraw Hill, 2018. 20CST21 – Programming and Linear Data Structures
  • 109. Unit IV : Contents 1. Introduction to Stack 2. Implementation of stack using – Array and linked list 3. Application of stack – Infix to Postfix expression conversion – Postfix expression evaluation 4. Introduction to Queue 5. Implementation of Queue using – Array and linked list 6. Other variations of Queue 7. Applications of Queue 7/15/2021 109 5.5 _ Types of Queue and its Applications
  • 110. Types of Queue • Linear Queue • Circular Queue • Double Ended Queue • Priority Queue 7/15/2021 5.5 _ Types of Queue and its Applications 110
  • 111. Types of Queue 7/15/2021 5.5 _ Types of Queue and its Applications 111
  • 112. Linear Queue 7/15/2021 5.5 _ Types of Queue and its Applications 112
  • 113. Linear Queue • In Linear Queue, an – insertion takes place from one end – deletion occurs from another end • The end at which the – insertion takes place is known as the rear end, – deletion takes place is known as front end • It strictly follows the FIFO rule. 7/15/2021 5.5 _ Types of Queue and its Applications 113
  • 114. Linear Queue • The linear Queue can be represented, as shown in the below figure: • The elements are inserted from the rear end, and if we insert more elements in a Queue, then the rear value gets incremented on every insertion. 7/15/2021 5.5 _ Types of Queue and its Applications 114
  • 115. Linear Queue • If we want to show the deletion, then it can be represented as: • The front pointer points to the next element, and the element which was previously pointed by the front pointer was deleted. 7/15/2021 5.5 _ Types of Queue and its Applications 115
  • 116. Drawbacks of Linear Queue • The major drawback of using a linear Queue is that insertion is done only from the rear end. • If the first three elements are deleted from the Queue, we cannot insert more elements even though the space is available in a Linear Queue. • In this case, the linear Queue shows the overflow condition as the rear is pointing to the last element of the Queue. • Solution is Circular Queue. 7/15/2021 5.5 _ Types of Queue and its Applications 116
  • 117. Circular Queue 7/15/2021 5.5 _ Types of Queue and its Applications 117
  • 118. Why was the concept of the circular queue introduced? • There was one limitation in the array implementation of Queue. • If the rear reaches to the end position of the Queue then there might be possibility that some vacant spaces are left in the beginning which cannot be utilized. • So, to overcome such limitations, the concept of the circular queue was introduced. 7/15/2021 5.5 _ Types of Queue and its Applications 118
  • 119. 7/15/2021 5.5 _ Types of Queue and its Applications 119 Why was the concept of the circular queue introduced?
  • 120. • As we can see in the previous image, the rear is at the last position of the Queue and front is pointing somewhere rather than the 0th position. • In the above array, there are only two elements and other three positions are empty. • if we try to insert the element then it will show that there are no empty spaces in the Queue. • There is one solution to avoid such wastage of memory space by shifting both the elements at the left and adjust the front and rear end accordingly. • It is not a practically good approach because shifting all the elements will consume lots of time. • The efficient approach to avoid the wastage of the memory is to use the circular queue data structure 7/15/2021 5.5 _ Types of Queue and its Applications 120 Why was the concept of the circular queue introduced?
  • 121. Circular Queue • It is linear data structure. • operations are performed based on FIFO (First In First Out) principle • last position is connected back to the first position to make a circle. • It is also called ‘Ring Buffer’. 7/15/2021 121 5.5 _ Types of Queue and its Applications
  • 122. Circular Queue • In a normal Queue, we can insert elements until queue becomes full. But once queue becomes full, we can not insert the next element even if there is a space in front of queue. • In circular queue, we can insert the next element even rear is reached last position. 7/15/2021 5.5 _ Types of Queue and its Applications 122
  • 123. Operations of Circular Queue • The following are the operations that can be performed on a circular queue: – Front: It is used to get the front element from the Queue. – Rear: It is used to get the rear element from the Queue. – enQueue(value): This function is used to insert the new value in the Queue. The new element is always inserted from the rear end. – deQueue(): This function deletes an element from the Queue. The deletion in a Queue always takes place from the front end. 7/15/2021 5.5 _ Types of Queue and its Applications 123
  • 124. Steps on Enqueue Operation • First, we will check whether the Queue is full or not. • Initially the front and rear are set to -1. • When we insert the first element in a Queue, front and rear both are set to 0. • When we insert a new element, the rear gets incremented, i.e., rear=rear+1. 7/15/2021 5.5 _ Types of Queue and its Applications 124
  • 125. Steps on Enqueue Operation (contd.,) 7/15/2021 5.5 _ Types of Queue and its Applications 125
  • 126. Steps on Dequeue Operation • First, we check whether the Queue is empty or not. • If the queue is empty, we cannot perform the dequeue operation. • When the element is deleted, the value of front gets decremented by 1. • If there is only one element left which is to be deleted, then the front and rear are reset to -1. 7/15/2021 5.5 _ Types of Queue and its Applications 126
  • 127. Circular Queue - Applications • Memory Management: The unused memory locations in the case of ordinary queues can be utilized in circular queues. • Traffic system: In a computer-controlled traffic system, circular queues are used to switch on the traffic lights one by one repeatedly as per the time set. • CPU Scheduling: Operating systems often maintain a queue of processes that are ready to execute or that are waiting for a particular event to occur (Round-robin scheduling). 7/15/2021 127 5.5 _ Types of Queue and its Applications
  • 128. Double Ended Queue (Deque) 7/15/2021 5.5 _ Types of Queue and its Applications 128
  • 129. Double Ended Queue (Deque)  The deque stands for Double Ended Queue.  In the queue, the insertion takes place from one end while the deletion takes place from another end.  The end at which the  insertion occurs is known as the rear end  deletion occurs is known as front end.  linear data structure 7/15/2021 129 5.5 _ Types of Queue and its Applications
  • 130. Properties of Deque • Deque can be used both as stack and queue as it allows the insertion and deletion operations on both ends. • (1) In deque, the insertion and deletion operation can be performed from one side. • The stack follows the LIFO rule in which both the insertion and deletion can be performed only from one end; • Therefore, we conclude that deque can be considered as a stack. 7/15/2021 5.5 _ Types of Queue and its Applications 130
  • 131. Properties of Deque • (2) In deque, the insertion can be performed on one end, and the deletion can be done on another end. • The queue follows the FIFO rule in which the element is inserted on one end and deleted from another end. • Therefore, we conclude that the deque can also be considered as the queue. 7/15/2021 5.5 _ Types of Queue and its Applications 131
  • 132. Types of Deque • There are two types of Queues: – Input-restricted queue – output-restricted queue 7/15/2021 5.5 _ Types of Queue and its Applications 132
  • 133. Input restricted queue • The input-restricted queue means that some restrictions are applied to the insertion. • In input-restricted queue, – the insertion is applied to one end – the deletion is applied from both the ends. • This kind of Queue does not follow FIFO(first in first out). • Uses: This queue is used in the cases where the consumption of the data needs to be in FIFO order but and if there is a need to remove the recently inserted data for some reasons and one such case can be irrelevant data, performance issue, etc. 7/15/2021 133 5.5 _ Types of Queue and its Applications
  • 134. Output restricted queue • The output-restricted queue means that some restrictions are applied to the deletion operation. • In an output-restricted queue, – the deletion can be applied only from one end – the insertion is possible from both ends • Uses: This queue is used in the case where the inputs have some priority order to be executed and the input can be placed even in the first place so that it is executed first. 7/15/2021 134 5.5 _ Types of Queue and its Applications
  • 135. List of Operations performed on Deque 7/15/2021 5.5 _ Types of Queue and its Applications 135
  • 136. Double Ended Queue (Deque) • A deque can be implemented either using a doubly-linked list or circular array. • In both implementation, we can implement all operations in O(1) time. 7/15/2021 5.5 _ Types of Queue and its Applications 136
  • 137. Applications of Deque • The deque can be used as a stack and queue; therefore, it can perform both redo and undo operations in software applications. • It can be used as a palindrome checker means that if we read the string from both ends, then the string would be the same. • It can be used for multiprocessor scheduling. Suppose we have two processors, and each processor has one process to execute. Each processor is assigned with a process or a job, and each process contains multiple threads. Each processor maintains a deque that contains threads that are ready to execute. • Internet browser’s history 7/15/2021 5.5 _ Types of Queue and its Applications 137
  • 138. Priority Queue • A priority queue is an abstract data type that behaves similarly to the normal queue except that each element has some priority. • The element with the highest priority would come first in a priority queue. • The priority of the elements in a priority queue will determine the order in which elements are removed from the priority queue. • The priority queue supports only comparable elements, which means that the elements are either arranged in an ascending or descending order. • For example, suppose we have some values like 1, 3, 4, 8, 14, 22 inserted in a priority queue with an ordering imposed on the values is from least to the greatest. • Therefore, the – 1 number would be having the highest priority – 22 will be having the lowest priority 7/15/2021 5.5 _ Types of Queue and its Applications 138
  • 139. Characteristics of a Priority queue • Every element in a priority queue has some priority associated with it. • An element with the higher priority will be deleted before the deletion of the lesser priority. • If two elements in a priority queue have the same priority, they will be arranged using the FIFO principle. 7/15/2021 5.5 _ Types of Queue and its Applications 139
  • 140. Priority Queue Example • We have a priority queue that contains the following values: 1, 3, 4, 8, 14, 22 • All the values are arranged in ascending order. • Now, we will observe how the priority queue will look after performing the following operations: • poll(): This function will remove the highest priority element from the priority queue. In the above priority queue, the '1' element has the highest priority, so it will be removed from the priority queue. • add(2): This function will insert '2' element in a priority queue. As 2 is the smallest element among all the numbers so it will obtain the highest priority. • poll(): It will remove '2' element from the priority queue as it has the highest priority queue. • add(5): It will insert 5 element after 4 as 5 is larger than 4 and lesser than 8, so it will obtain the third highest priority in a priority queue. 7/15/2021 5.5 _ Types of Queue and its Applications 140
  • 141. Types of Priority Queue • There are two types of priority queue: – Ascending order priority queue – Descending order priority queue 7/15/2021 5.5 _ Types of Queue and its Applications 141
  • 142. Ascending order priority queue • In ascending order priority queue, a lower priority number is given as a higher priority in a priority. • For example, we take the numbers from 1 to 5 arranged in an ascending order like 1,2,3,4,5; • Therefore, the smallest number, i.e., 1 is given as the highest priority in a priority queue. 7/15/2021 5.5 _ Types of Queue and its Applications 142
  • 143. Descending order priority queue • In descending order priority queue, a higher priority number is given as a higher priority in a priority. • For example, we take the numbers from 1 to 5 arranged in descending order like 5, 4, 3, 2, 1; • Therefore, the largest number, i.e., 5 is given as the highest priority in a priority queue. 7/15/2021 5.5 _ Types of Queue and its Applications 143
  • 144. Implementation of Priority Queue • The priority queue can be implemented in four ways that include – arrays – linked list – heap data structure and – binary search tree • The heap data structure is the most efficient way of implementing the priority queue. 7/15/2021 5.5 _ Types of Queue and its Applications 144
  • 145. Priority Queue Operations • The common operations that we can perform on a priority queue are – Insertion – deletion and – peek 7/15/2021 5.5 _ Types of Queue and its Applications 145
  • 146. Complexities using different implementations of Priority Queue 7/15/2021 5.5 _ Types of Queue and its Applications 146
  • 147. What is Heap? • A heap is a tree-based data structure that forms a complete binary tree, and satisfies the heap property. • If A is a parent node of B, then A is ordered with respect to the node B for all nodes A and B in a heap. • It means that the value of the parent node could be more than or equal to the value of the child node or • the value of the parent node could be less than or equal to the value of the child node. • Therefore, we can say that there are two types of heap. – Max Heap – Min Heap 7/15/2021 5.5 _ Types of Queue and its Applications 147
  • 148. Max heap • The max heap is a heap in which the value of the parent node is greater than the value of the child nodes. 7/15/2021 5.5 _ Types of Queue and its Applications 148
  • 149. Min heap • The min heap is a heap in which the value of the parent node is less than the value of the child nodes. 7/15/2021 5.5 _ Types of Queue and its Applications 149
  • 150. Applications of Priority Queue • CPU Scheduling • It is used in the Dijkstra's shortest path algorithm. • It is used in prim's algorithm • It is used in data compression techniques like Huffman code. • It is used in heap sort. • It is also used in operating system like – priority scheduling, – load balancing and – interrupt handling. 7/15/2021 5.5 _ Types of Queue and its Applications 150
  • 151. Applications of Queue • Queue is used when things don’t have to be processed immediately, but have to be processed in First In First Out order like Breadth First Search. • 1) When a resource is shared among multiple consumers. Examples include CPU scheduling, Disk Scheduling. 2) When data is transferred asynchronously (data not necessarily received at same rate as sent) between two processes. Examples include IO Buffers, pipes, file IO, etc. 3) In Operating systems: a) Semaphores b) FCFS ( first come first serve) scheduling, example: FIFO queue c) Spooling in printers d) Buffer for devices like keyboard 4) In Networks: a) Queues in routers/ switches b) Mail Queues 5) Variations: (Deque, Priority Queue, Doubly Ended Priority Queue ) 7/15/2021 5.5 _ Types of Queue and its Applications 151
  • 152. Thank you 7/15/2021 5.5 _ Types of Queue and its Applications 152