DS Stack
DS Stack
Week 6 Lecture
by
Dr Vinay Pathak
Stack Data Structure
It is called as stack because it behaves like a real-world stack, piles of
books, etc. It is a linear data structure.
Understanding the Stack
• Stack is an abstract data type with a pre-defined
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
} Errors occurs in a program.
Applications of Stack
2) String reversal
• 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)
Understanding Polish Notation
• Polish Notation in the data structure is a method of
expressing mathematical, logical, and algebraic equations
universally.
Output ?? 16
20
Output ?? 16
20
Parenthesis ( ), {}, [ ]
Exponents ^
Output ?? 4^3 = 64
2^8 = 256
*Exponential operator associativity changes with the language used. There is no standard guideline.
Expression Conversion using Stack
• Infix to prefix
• Infix to postfix
• Prefix to infix
• Prefix to postfix
• Postfix to infix
Infix to Postfix Conversion
Step 1
Scan the expression from left to right
Step 2
If the reading symbol is an operand,
then immediately append it to the
Postfix Expression.
Infix to Postfix Conversion
Step 3
If the reading symbol is any operator +, –, *, /, then
Push it onto the Stack.
Step 4
If the reading symbol is left
parenthesis ‘( ‘, then Push it
onto the Stack.
Infix to Postfix Conversion
Step 5
If the reading symbol is an
operand, then append it to the
Postfix Expression. (Step 2)
Infix to Postfix Conversion
Step 6
If the reading symbol is any operator +, –, *, /, then
Push it onto the Stack.
Step 6
If the reading symbol is an operand, then
append it to the Postfix Expression. (Step 2)
Infix to Postfix Conversion
Step 7
If the reading symbol is any operator +, –, *, /,
then Push it onto the Stack.
Step 8
If the reading symbol is an operand, then
append it to the Postfix Expression. (Step 2)
Infix to Postfix Conversion
Step 9
:If the reading symbol is right parenthesis ‘)’,
then Pop all the contents of the stack until
the respective left parenthesis is popped and -9
append each popped symbol to Postfix
Expression.
Infix to Postfix Conversion
Step 10
If the input is over, pop all the
remaining symbols from the stack
and append them to the postfix.
Algorithm of Infix to Postfix Conversion
Step 1: Scan all the symbols one by one from left to right in the given Infix Expression. If the reading symbol is an operand, then
immediately append it to the Postfix Expression.
Step 2: If the reading symbol is left parenthesis ‘( ‘, then Push it onto the Stack.
Step 3: If the reading symbol is right parenthesis ‘)’, then Pop all the contents of the stack until the respective left parenthesis is popped
and append each popped symbol to Postfix Expression.
Step 4: If the reading symbol is an operator (+, –, *, /), then Push it onto the Stack. However, first, pop the operators which are already
on the stack that have higher or equal precedence than the current operator and append them to the postfix. If an open parenthesis is there
on top of the stack then push the operator into the stack.
Step 5: If the input is over, pop all the remaining symbols from the stack and append them to the postfix.
Infix to Postfix Conversion
Shortcut Approach
a* (b+c+d) a* ((b+c)+d))
a* ((bc+) +d)
a*(bc+d+)
abc+d+*
Advantage of Postfix Expression over Infix Exp
•Postfix Expressions does not require the usage of the parenthesis which are
necessary in case of infix expression.
•In postfix expression, there are no operator precedence rules. So, the
order of operations is always clear.
Problem 1:
Infix: (a+b)*c
Postfix:?????
Output: ab+c*
Assessment Time
Problem 2:
Infix: a*b+c-d
Postfix:?????
Output: ab*c+d-
KBC Assessment Time
Problem 3
Postfix: ?????
Output: abcde^/fg*h-^*+