0% found this document useful (0 votes)
10 views39 pages

Stack and Queue Unit 2 - 23-24-1

The document provides an overview of stack and queue data structures, detailing their definitions, operations, and implementations. It explains stack operations such as push, pop, and peek, as well as applications like reversing lists and expression conversions. Additionally, it covers queue characteristics, types, and operations, including circular queues and priority queues.

Uploaded by

rajmali2207
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views39 pages

Stack and Queue Unit 2 - 23-24-1

The document provides an overview of stack and queue data structures, detailing their definitions, operations, and implementations. It explains stack operations such as push, pop, and peek, as well as applications like reversing lists and expression conversions. Additionally, it covers queue characteristics, types, and operations, including circular queues and priority queues.

Uploaded by

rajmali2207
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

Stack and

Queue
Dr. Sheetal Patil
EXCS Department
VIT
Introduction to Stack
Stack is a data structure that stores its element in an ordered manner.

Linear Data Structure → Implementation


• Array
• Linked List
Two operation:
• Insertion
• Deletion
LIFO(Last in First Out)
Stack Abstract Data Type
Finite sequence of elements of same data type

Some operations use to manipulate the data store in the stack

Operations:

• Initialize stack to be empty


• Check if stack is empty or not
• Check if stack is full or not
• Insert new element on the stack if stack is not full.
• Display the value of the topmost element of the stack if stack is not empty.
• Delete the topmost element of the stack.
Stack Abstract Data Type

Stack Abstract Data Type = Stack Element + Stack operation


Array Representation of Stack

Linear Array

Variable TOP

Variable MAX

TOP = NULL

TOP = MAX-1
1. Push operation:
• Algorithm

➢Step 1: If TOP = MAX-1 then


print “OVERFLOW” [If ends]
➢Step 2: SET TOP = TOP+1
➢Step 3: SET STACK[TOP] =
VALUE
➢ Step4:END

Operation on
stack:
2. Pop Operation
• Algorithm:

➢Step 1: If Top = NULL then


print “UNDERFLOW” [END
if]
➢Step 2: Set VAL=
stack[TOP]
➢Step 3: Set TOP = TOP-1
➢Step 4: END
Operation on
Stack
3. Peek or Peep Operation
• Algorithm:

➢Step 1: If Top = NULL then


print “STACK IS EMPTY”
Go to step 3
➢Step 2: Return stack[TOP]
➢Step 3: END

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

A list of numbers can be reversed by reading each number from the


array starting from the first index and pushing it on stack

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

If not then attempt to


Check if the
correct the expression
parentheses are
by adding the missing
balanced
parentheses.
Polish Notations:

Infix Notation • Operand Operator Operand : A+B

Postfix Notation • Operand Operand Operator: AB+

Prefix Notation • Operator Operand Operand : +AB


Precedance of Operators
• Parenthesis
Conversion of
• Power
Infix Expression
• Multiplication and Division
into Postfix
• Addition and Subtraction
Expression
Associtivity:
• Operators with same precedence
work with associativity L to R except
power which work R to L
Conversion of Infix Expression into Postfix
Expression using Stack
1. Print operand as they arrive
2. If stack is empty or contains a left parenthesis on the the top push the
incoming operator into the stack
3. If the incoming symbol is ‘(‘ push it into the stack
4. If incoming symbol is ‘)’ pop the stack and print the operator until left
parenthesis is found
5. If incoming operator has higher precedence than top of the stack push it
on the stack
6. If incoming operator has lower precedence than top of the stack pop
and print the top. Then test incoming operator against the new top of
the stack
Conversion of Infix Expression into Postfix
Expression using Stack
7. If incoming operator has equal precedence with the top of the stack
use associativity rule
8. At the end of the expression pop and print all operators of the stack.
• Associativity:
• L-R then pop and print the top of the stack and then push the incoming
operator
• R- L then push the incoming operand
Conversion of Infix Expression into Prefix
Expression using Stack
1. First reverse the given expression
2. Print operand as they arrive
3. If stack is empty or contains a ‘right’ parenthesis on the the top push the
incoming operator into the stack
4. If the incoming symbol is ‘)‘ push it into the stack
5. If incoming symbol is ‘(’ pop the stack and print the operator until right
parenthesis is found
6. If incoming operator has higher precedence than top of the stack push it
on the stack
7. If incoming operator has lower precedence than top of the stack pop
and print the top. Then test incoming operator against the new top of
the stack
Conversion of Infix Expression into Prefix
Expression using Stack
If incoming operator has equal precedence with the top of the stack
use associativity rule
8. At the end of the expression pop and print all operators of the stack
and reverse the expression.
• Associativity:
• L-R then push the incoming operator
• R- L then pop and print the top of the stack and then push the incoming
operator
Evaluation of Postfix Expression:

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

• Data structure which stores its elements in an ordered manner.


• A queue is a First in First out (FIFO) data structure in which the element that
was inserted first is the first one to be taken out.
• The elements in a queue are added at one end called rear and removed from
another end called front.
• Queues can be implemented by either using arrays or linked list.
• Queues can be implemented using two stacks too.
Queue Abstract Data Type

• 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:

of Deque • Insertion can be done only at one


end
• Deletion can be done from both
end

Output Restricted Deque:

• Deletion can be done only at one


end
• Insertion can be done from both
ends
Priority ADT in which each element is assigned a
Queue priority.

Priority is used to determine the order in


which these elements will be processed.

Rules of processing:

• An element with higher priority is processed


before an element with lower priority
• Two elements with same priority are processed on
a first come first serve basis
Implementation of Priority Queue
1. Sorted List:
• Use sorted list to store element
• While deleting no need of Search
• Deletion is easy →Delete at the beginning
• Insertion is hard → Need to find proper location
• Linked list for Implementation
2. Unsorted List:
• Insertion is always done at the end
• Insertion is easy
• Deletion is hard → need to find element locations
• Array for Implementation.

You might also like