Evaluate Postfix Expression using Stack with Examples | DSA using Java #22

Evaluate Postfix Expression using Stack with Examples | DSA using Java #22

ForMyScholars

3 года назад

8,821 Просмотров

Evaluate Postfix Expression using Stack with Examples | DSA using Java 2021
Postfix Expression Evaluation
A postfix expression is a collection of operators and operands in which the operator is placed after the operands. That means, in a postfix expression the operator follows the operands.
Postfix Expression Evaluation using Stack Data Structure
A postfix expression can be evaluated using the Stack data structure. To evaluate a postfix expression using Stack data structure we can use the following steps...

Read all the symbols one by one from left to right in the given Postfix Expression
If the reading symbol is operand, then push it on to the Stack.
If the reading symbol is operator (+ , - , * , / etc.,), then perform TWO pop operations and store the two popped oparands in two different variables (operand1 and operand2). Then perform reading symbol operation using operand1 and operand2 and push result back on to the Stack.
Finally! perform a pop operation and display the popped value as final result.


For solving a mathematical expression, we need prefix or postfix form. After converting infix to postfix, we need postfix evaluation algorithm to find the correct answer.

Here also we have to use the stack data structure to solve the postfix expressions.

From the postfix expression, when some operands are found, pushed them in the stack. When some operator is found, two items are popped from the stack and the operation is performed in correct sequence. After that, the result is also pushed in the stack for future use. After completing the whole expression, the final result is also stored in the stack top.

The Postfix notation is used to represent algebraic expressions. The expressions written in postfix form are evaluated faster compared to infix notation as parenthesis are not required in postfix. We have discussed infix to postfix conversion. In this post, evaluation of postfix expressions is discussed.

One more Example:
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).
Following is algorithm for evaluation postfix expressions.
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 an 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

Topics Covered
evaluation of postfix expression in data structure
evaluation of postfix expression
postfix evaluation using stack example
evaluation of postfix expression in data structure example
evaluation of postfix expression using stack
postfix expressions
prefix expressions
infix expressions
postfix expression evaluation
UGC net computer science study material
data structures and algorithms
DSA using Java 2021
Hands-on Data Structures Training
Data Structures Overview
Stack Data Structure Tutorial
Data structure tutorials
what is stack in data structure
Data Structures Overview
Data Structures Interview Questions
Stack

Link to Data structure using Java Playlist:
https://www.youtube.com/playlist?list=PLH9iLcrNpXtQYQiudzpZpGw0mptHc06Su

Join the Telegram Group for interview preparation material and updates:
https://t.me/formyscholarsdeepali

You can also reach me at:
Instagram: https://www.instagram.com/formyscholarsdeepali/
LinkedIn: https://www.linkedin.com/in/formyscholarsdeepali/

#formyscholars #DSA #datastructure

For Online/Offline Training & Business Enquiry:
Email-id: [email protected]

Disclaimer: The information given in this video is as per my research and knowledge. Kindly do your research also before taking any step to go further.

Тэги:

#formyscholars #deepali_agarwal #programming #java #django_tutorial #jdbc_tutorial #Evaluate_Postfix_Expression_using_Stack #DSA_using_Java_2021 #ugc_net_computer_science_study_material #evaluation_of_postfix_expression #postfix_expressions #prefix_expressions #Postfix_Expression_Evaluation #Postfix_Expression_Evaluation_using_Stack_Data_Structure #converting_infix_to_postfix #postfix_evaluation_algorithm #Postfix_Expression_using_Stack_with_Examples #Evaluate_Postfix_Expression
Ссылки и html тэги не поддерживаются


Комментарии: