DS Week 7 Lecture Stacks by Dr Gaurav
DS Week 7 Lecture Stacks by Dr Gaurav
DS Week 7 Lecture Stacks by Dr Gaurav
Week 7 Lecture
by
Dr Gaurav Kumar
Asst. Prof, Bennett University
Quick Recap of Previous Weeks’ Learnings
capacity.
Algorithm:
Step-1: If TOP = NULL
PRINT “Stack is Empty”
Goto Step 3
Step-2: Return Stack[TOP]
Step-3: END
Operations on Stack
1) Balancing of symbols
During compilation of program, when the opening braces
come, it push the braces in a stack, and when the closing
int main() braces appear, it pop the opening braces from the stack.
{ Therefore, the net value comes out to be zero.
cout<<"Hello";
cout<<"Students"; If any symbol is left in the stack, it means that some Syntax
• There are three states, a, ab, and abc, which are stored in
a stack.
c • When undo is called, pop operations from Undo stack and push it
b to Redo stack.
a
Applications of Stack
2) UNDO/REDO • There are three states, a, ab, and abc, If we want to perform UNDO
operation, and want to achieve 'ab' state, then we implement pop
operation.
• When undo is called, pop operations from Undo stack and push it
b to Redo stack.
a c
• When redo is called, pop operations from Redo stack and push it
to Undo stack.
Applications of Stack
When function call happens, previous variables gets stored in the Stack
3) Recursion
• To maintain the previous states, the compiler Returning value from base case to Caller function
This search is implemented on a Graph, and Graph uses the stack data
structure.
Applications of Stack
5) Backtracking
Stack is also used for expression conversion. This is one of the most
important applications of stack.
Ex- 2 + 4 → 24+
(Infix Notation) (Postfix Notation)
Next Week Readings
• Polish Notation
• Infix-Postfix Conversion
• Postfix Evaluation
Any Queries?
Office MCub311
Discussion Time: 3-5 PM
Mob: +91-8586968801