Thursday, September 29, 2022

Data Structures and Algorithms (Some Questions for You - Answer to Question 8) - Tirthankar Pal - MBA from IIT Kharagpur, GATE, GMAT, IIT Written Test, Interview were a part of MBA Entrance, B.S. in Computer Science from NIELIT

Write an algorithm to evaluate a postfix expression

Evaluation of Postfix

Example -1:

Let the given expression be “2 3 1 * + 9 -“. We scan all elements one by one.
1) Scan ‘2’, it’s a number, so push it to stack. Stack contains ‘2’
2) Scan ‘3’, again a number, push it to stack, stack now contains ‘2 3’ (from bottom to top)
3) Scan ‘1’, again a number, push it to stack, stack now contains ‘2 3 1’
4) Scan ‘*’, it’s an operator, pop two operands from stack, apply the * operator on operands, we get 3*1 which results in 3. We push the result ‘3’ to stack. Stack now becomes ‘2 3’.
5) Scan ‘+’, it’s an operator, pop two operands from stack, apply the + operator on operands, we get 3 + 2 which results in 5. We push the result ‘5’ to stack. Stack now becomes ‘5’.
6) Scan ‘9’, it’s a number, we push it to the stack. Stack now becomes ‘5 9’.

7) Scan ‘-‘, it’s an operator, pop two operands from stack, apply the – operator on operands, we get 5 – 9 which results in -4. We push the result ‘-4’ to stack. Stack now becomes ‘-4’.

8) There are no more elements to scan, we return the top element from stack (which is the only element left in stack) 

Example-2 : 456*+

StepInput symbolOperationStackCalculation
1.4Push4
2.5Push4,5
3.6Push4,5,6
4.*Pop( 2 elements) and evaluate45*6=30
5.Push result 304,30
6.+Pop( 2 elements) and evaluateEmpty4+30=34
7.Push result 3434
8.No more elements to popEmpty34(result)

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 and then evaluate it.
  3. Push back the result of the evaluation. Repeat it till the end of the expression.
  4. Algorithm
    1) Create a stack to store operands (or values).
    2) Scan the given expression and do following for every scanned element.

         a) If the element is a number, push it into the stack
         b) If the element is a operator, pop operands for the operator from stack. Evaluate the operator and push the result back to the stack
    3) When the expression is ended, the number in the stack is the final answer

    Tirthankar Pal

    MBA from IIT Kharagpur with GATE, GMAT, IIT Kharagpur Written Test, and Interview

    2 year PGDM (E-Business) from Welingkar, Mumbai

    4 years of Bachelor of Science (Hons) in Computer Science from the National Institute of Electronics and Information Technology

    Google and Hubspot Certification

    Brain Bench Certification in C++, VC++, Data Structure and Project Management

    10 years of Experience in Software Development out of that 6 years 8 months in Wipro

    Selected in Six World Class UK Universities:-

    King's College London, Durham University, University of Exeter, University of Sheffield, University of Newcastle, University of Leeds

No comments:

Post a Comment