Stack and Queue (1)
Stack and Queue (1)
PR E S E N T E D BY :
M R . K I R A N K I S H O R AWA L E ,
Data Structure
The top of the stack is the most recently object pushed onto
the stack
Top=Top + 1
Top=Top - 1
Push and Pop Operations in Stack (Cont…)
Consider the following example of pushing
elements A & B into the Stack and then Popping
the element at TOP.
Some Exceptional Conditions
Overflow:
In executing the procedure Push, one must test whether there is
room in the stack for new item, if not the we have a condition
called as Overflow.
In short if we try to insert into a full stack we encounter
Overflow.
Underflow:
In executing the procedure Pop, one must first test whether
there is an element in the stack, if not then we have a condition
called as Underflow.
If we try to delete from empty stack we encounter Underflow
condition.
Review
Fundamental operations:
• Push: Equivalent to an
insert
• Associated Exceptional
condition: Overflow
• Pop: Deletes the most
recently inserted element
• Associated Exceptional
condition: Underflow
Implementation of Stacks
2. Tower of Hanoi
5. Stack is used by compilers to check for balancing of parentheses, brackets and braces.
6. Sorting
Quick Sort is an application of Stack
7. Stack is used for traversal of Non-Linear Data Structures (Trees and Graph)
1. Recursion as Application of Stack
1. Recursion
Function call and return is generally handled by stack
2. Tower of Hanoi
6. Sorting
Quick Sort is an application of Stack
7. Stack is used for traversal of Non-Linear Data Structures (Trees and Graph)
2. Tower of Hanoi
2. Tower of Hanoi
2. Tower of Hanoi (Cont…)
1. Recursion
Function call and return is generally handled by stack
2. Tower of Hanoi
6. Sorting
Quick Sort is an application of Stack
7. Stack is used for traversal of Non-Linear Data Structures (Trees and Graph)
Transforming Polish Notations
Polish Notation:
Infix
A+B
Postfix
AB+
Prefix
+AB
Transforming Polish Notations (Cont…)
Transforming Polish Notations (Cont…)
1. Recursion
Function call and return is generally handled by stack
2. Tower of Hanoi
6. Sorting
Quick Sort is an application of Stack
6. Stack is used for traversal of Non-Linear Data Structures (Trees and Graph)
Evaluation of Arithmetic Expressions
Evaluation of Arithmetic Expressions (Cont…)
QUEUE
Queue Definition
Enqueue:
Insertion operation to the Queue is called as Enqueue.
Mathematical Operation: Rear = Rear + 1
Condition to be Checked: Overflow
Dequeue:
Deletion operation to the Queue is called as Dequeue.
Mathematical Operation: Front = Front + 1
Condition to be Checked: Underflow
Static and Dynamic Queues
2
Limitation of Queue (Linear Queue) (Cont…)
5. Now Try to insert into the Queue (Enqueue), this will still cause Overflow
though there is space available.
Types Of Queue
Rules:
1. An element with higher priority will processed before an
element with a lower priority.
2. Two elements with the same priority are processed on a
First Come First Serve basis.
Array Representation of Priority Queue
Insertion Operation:
While inserting elements in priority queue we will
add it at the appropriate position depending on its
priority
It is inserted in such a way that the elements are
always ordered either in Ascending or descending
sequence
Deletion Operation:
While deletion, the element at the front is always
deleted.
Insertion in Priority Queue
Deletion in Priority Queue
Deque / Double Ended Queue
Types of Deque:
Input Restricted Deque:-
In this dequeue, insertion can be done only at one of the
end, while deletion can be done from both ends.
Output Restricted Deque:-
In this dequeue, deletion can be done only at one of the
ends, while insertions can be done on both ends
Applications of Queue