Stack and Queue Unit 2 - 23-24-1
Stack and Queue Unit 2 - 23-24-1
Queue
Dr. Sheetal Patil
EXCS Department
VIT
Introduction to Stack
Stack is a data structure that stores its element in an ordered manner.
Operations:
Linear Array
Variable TOP
Variable MAX
TOP = NULL
TOP = MAX-1
1. Push operation:
• Algorithm
Operation on
stack:
2. Pop Operation
• Algorithm:
Operation on
Stack
Program:
Write a program
to implement • Push new element into stack
stack using array • Pop existing element from stack
to perform • Peek/peep topmost element
following from the stack
operation on • Display content of the stack
stack:
Applications of Stacks
• Reversing List
• Correction of Parenthesis
• Polish notations
• Conversion from infix to postfix expressions
• Evaluation of a postfix expression
• Conversion of an infix expression into prefix expression
• Evaluation of a prefix expression
• Recursion
• Recursion Vs Iterations
Reversing List
Once all numbers have been read the numbers can be popped at a
time and then stored in the array starting from the first index.
Correction of Parenthesis
Algorithm:
1. Scan expression from left to right
2. If operand encounters, add it to the stack
3. If operator encounters, then:
a. Pop two operands from stack and perform the operation
b. Push the result into the stack
Evaluation of Prefix Expression:
Algorithm:
1. Scan expression from right to left
2. If operand encounters, add it to the stack
3. If operator encounters, then:
a. Pop two operands from stack and perform the operation
b. Push the result into the stack
Queue
Introduction to Queues
• Two Characteristics:
• Logical way in which the elements are related to each other.
• Operations that can be performed on the elements of Abstract Data Type.
• Operations Performed on Queue Data Type:
• Initialize Queue to be Empty
• Check if the Queue is Empty or not
• Check if the Queue is Full or not
• Insert new element on the Queue if it is not Full
• Retrieve the value of first element of the Queue if the Queue is not empty
• Delete the first element of Queue if it is not empty.
Queue Abstract Data Type
Queue Abstract Data Type = Queue Element + Queue Operations.
Algorithm to insert an element in the Queue
• Step 1: If REAR = MAX – 1, then; Write OVERFLOW (End of If)
• Step 2: If FRONT == -1 and REAR= = -1 then
Set FRONT= REAR = 0
Else
Set REAR = REAR+1
Step 3: SET QUEUE[REAR] =VALUE
Step 4: Exit
Algorithm to delete an element from the Queue
• Step 1: If FRONT = -1 or FRONT > REAR then
Write UNDERFLOW
else
SET VALUE = QUEUE[FRONT]
FRONT = FRONT+1
Step 2: Exit
Program to Implement Queue
• Write a program to implement queue using array to perform following operations on
queue
1. Insert new element into queue
2. Delete existing element from queue
3. Display contents of queue.
Types of Queue
1. Circular Queue
2. Deque
3. Priority Queue
4. Multiple Queue
Drawback of Linear Queue
• Insertion one end: rear
Deletion other end: front
Two Solutions
• Shift the element to the left
• Cicular Queue
Circular Queue
• Full only when front =0 and rear = N-1
• Code will be different for Enqueue and Dequeue
Enqueue Operation
• If front = 0 and rear = N-1, then circular queue is full.
• If rear ! = N-1 then value can be inserted, and rear can be incremented.
• If front !=0 and rear = N-1, then it means queue is not full. So set rear=0 and insert the
new element.
Algorithm for Enqueue operation in circular
queue:
• Step 1: If FRONT = 0 AND REAR = N-1 then write “OVERFLOW”
elseif
FRONT = -1 and REAR = -1 then
set FRONT = REAR= 0;
elseif REAR = N-1 and FRONT != 0;
SET REAR = 0;
else SET REAR = REAR +1;
• Step 2: Set queue[REAR] = VAL
• Step 3: Exit
Dequeue Operation
• If Front = -1 then there is no element in the queue so UNDERFLOW condition
• After returning value on the front if front = rear then queue is empty and then set front
and rear = -1
• If the queue is not empty and after returning the value on the front, front = N-1 then set
front to zero
Algorithm for Dequeue operation in circular
queue:
• Step 1: If Front = -1 then
print “UNDERFLOW”
• Step 2: set VAL = QUEUE[FRONT]
• Step 3: If FRONT = REAR
SET FRONT= REAR= -1
else
If FRONT = N-1
Set FRONT = 0
Else Set FRONT = FRONT +1
• Step 4: Exit
Deques
• A deque is a list in which the elements can be inserted or deleted at either end.
• Head Tail Linked List
• Two pointers are maintained LEFT and RIGHT which point to either end of deque.
• The elements in a deque stretch from left to right since its circular.
Two types Input Restricted Deque:
Rules of processing: