This document contains information about evaluating postfix expressions using a stack. It provides an example postfix expression (843*6/-), explains the steps to evaluate it using a stack, and shows the contents of the stack at each step. The key steps are: (1) push operands onto the stack as they are read, (2) pop two elements for operators and evaluate, pushing the result back on the stack, (3) continue until the end is reached, then the final stack element is the result.
1. ELECTRINICS & COMMUNICATION
ENGINEERING DEPARTMENT
TOPIC: EVALUATION OF POSTFIX EXPRESSION USING STACK
Name : Shubhajit Chatterjee
Student Code : BWU/BEC/19/028
Registration Number : 19013000936 of 2019-2020
Roll Number : 19010308024
Course Name :Data Structures and Algorithm
Course code : ESC(ECE)301
Programme : Bachelor of Technology in Electronics & Communication Engineering
Semester : 3rd
2. ELECTRINICS & COMMUNICATION
ENGINEERING DEPARTMENT
• What is Postfix Expression :
The ease of evaluation acts as the driving force for computers to translate
an infix notation into a postfix notation. That is, given an algebraic
expression written in infix notation, the computer first converts the
expression into the equivalent postfix notation and then evaluates the
postfix expression.
Both these tasks—converting the infix notation into postfix notation and
evaluating the postfix expression—make extensive use of stacks as the
primary tool.
Using stacks, any postfix expression can be evaluated very easily.
3. ELECTRINICS & COMMUNICATION
ENGINEERING DEPARTMENT
Evaluation rule of a Postfix Expression states:
1.While reading the expression from left to right, push the element in the stack if it is an
operand.
2.Pop the two operands from the stack, if the element is an operator(*,+,-,/,..etc) and then
evaluate it.
3.Push back the result of the evaluation. Repeat it till the end of the expression.
4. ELECTRINICS & COMMUNICATION
ENGINEERING DEPARTMENT
ALGORITHM FOR POSTFIX EXPRESSION:
STEP 1:- Add ‘#’ to postfix expression.
STEP 2:- Read postfix expression Left to Right until ‘#’ encountered
STEP 3:- If operand is encountered, push it onto Stack
[End If]
STEP 4:- If operator is encountered, Pop two elements
i) A->Top element
ii) B-> Next to Top element
iii) Evaluate B operator A
push B operator A onto Stack
STEP 5:- Set result = pop
STEP 6:- END
6. ELECTRINICS & COMMUNICATION
ENGINEERING DEPARTMENT
o Example : 843*6/-
8
4
3
Push 8,4,3Stack is
empty
Top
Top
Pop 3,4
8
12
4*3=12
Push 12
Top
8
12
6
Push 6
Top
8
Pop 6,12
Top
12/6=2
Push 2
8
2
Top
Top
6
8-2=6
Push 6
Pop 2,8
Stack
empty
Pop6
Ans=6
8
Empty