DS Week 8 Lecture Rev Operations of Stacks Part 2 by DR Gaurav
DS Week 8 Lecture Rev Operations of Stacks Part 2 by DR Gaurav
Week 8 Lecture
by
Dr Gaurav Kumar
Asst. Prof, Bennett University
Quick Recap of Previous Weeks’ Learnings
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
Operators Symbols
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.
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-^*+
Postfix Expression Evaluation
Step 1: Scan the expression from left to right.
4*+ 36 Push 4
Input Stack
34*25*+ empty Push 3
4*25*+ 3 Push 4
*2 5 * + 43 Pop 4 and 3 from the stack and perform
4*3 = 12. Push 12 into the stack.
25*+ 12 Push 2
5*+ 2 12 Push 5
*+ 5 2 12 Pop 5 and 2 from the stack and perform
5*2 = 10. Push 10 into the stack.
Problem 1: 2 3 1 * + 9 –
Output:
(A) 4
(B) - 4
(C) 3
(D) 5
Problem 1: 8 2 3 ^ / 2 3 * + 5 1 * –
Note that ^ is the exponentiation operator. The top
two elements of the Stack after the first * are:
(A) 6,1
(B) 5,7
(C) 3,2
(D) 1,5
Correct Answer is A
Next Week Topics for Reading