You are on page 1of 6

Department of Computer Science & Engineering

UIT-RGPV

Topic: Applications of Stack

Course: Data Structures (CS303)

By:
Mr. Praveen Yadav
Assistant Professor
UIT-RGPV, Bhopal
Applications of Stack
• Stack Frames (Used in Recursion).
• Reversing a String.
• Parsing (Used in Compiler).
• Calculation of Postfix Expression.

Data Structures By Asst. Prof. Praveen Yadav, DoCSE, UIT-RGPV 2


Evaluation of a Postfix Expression

Data Structures By Asst. Prof. Praveen Yadav, DoCSE, UIT-RGPV 3


Evaluation of a Postfix Expression cont…

Example: Given Postfix Expression

P = 5, 6, 2, +, *, 12, 4, /, -
Solution:
First Add “)” at the end of P (Postfix expression)

P = 5, 6, 2, +, *, 12, 4, /, -, )

Data Structures By Asst. Prof. Praveen Yadav, DoCSE, UIT-RGPV 4


Evaluation of a Postfix Expression cont…
Symbol Scanned STACK Operation performed
5 5 Push 5
6 5, 6 Push 6
2 5, 6, 2 Push 2
+ 5, 8 Pop 6 & 2 and perform operation 6+2 = 8 then Push 8
* 40 Pop 8 & 5 and perform operation 8*5 = 40 then Push 40

12 40, 12 Push 12
4 40, 12, 4 Push 4

/ 40, 3 Pop 4 & 12 and perform operation 12/3 = 4 then Push 3


- 37 Pop 3 & 40 and perform operation 40-3 = 37 then Push 37
) Pop 37 and Announce Result = 37

Data Structures By Asst. Prof. Praveen Yadav, DoCSE, UIT-RGPV 5


Thank You

Data Structures By Asst. Prof. Praveen Yadav, DoCSE, UIT-RGPV 6

You might also like