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

preview_player
Показать описание
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:

Join the Telegram Group for interview preparation material and updates:

You can also reach me at:

#formyscholars #DSA #datastructure

For Online/Offline Training & Business Enquiry:

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.
Рекомендации по теме
Комментарии
Автор

Get more videos on:









ForMyScholars
Автор

Thanks for this kind of explanation mam,
You are gonna make dsa easy for us by your excellent teaching skill

AbhinavJha
Автор

thank you a lot for your efforts ma'am

subhammohanty
Автор

First viewer and the way you explain ❤️

robertbuggati
Автор

Mam bracket sirf end m hi kyu lgae.... Phele kyu nhi... Mam please 🙏

nishudwivedi
Автор

MA'AM PLEASE FIND THE ERROR



import java.util.Scanner;

public class ll_add_on_specific_position {
static class node{
int data;
node next;
node(int data){
this.data=data;
this.next=null;
}
}
node head=null;

//creation
public void creation(){
int n;
Scanner sc=new Scanner(System.in);
do{
System.out.println("ENTER DATA");
System.out.println("enter elemet you want to enter");
int data=sc.nextInt();
node new_node=new node(data);
if(head==null){
head=new_node;
}
else{

System.out.println("enter 1 to add in begning, enter 2 to add in ending, enter 3 to add in specific position");
int c=sc.nextInt();
switch (c)
{
case 1:
{
new_node.next=head;
head=new_node;
break;
}
case 2:
{
node temp=head;
while(temp!=null){
temp=temp.next;
}
temp.next=head;
break;
}
case 3:
{
System.out.println("enter tjhe position");
int p=sc.nextInt();
node temp1=head;
for (int i=0;i<(p-1);i++){
temp1=temp1.next;
}
new_node.next=temp1.next;
temp1.next=new_node;
}
}
new_node.next=head;
head=new_node;
}
System.out.println("you want to add more data press 1");
n=sc.nextInt();
}
while(n==1);
}

//traverse
public void traverse(){
node temp=head;
if(head==null){
System.out.println("LL does not exist");
}
else{
while(temp!=null){

temp=temp.next;
}
}
}

public static void main(String[] args) {
ll_add_on_specific_position ll=new
ll.creation();
ll.traverse();
}
}

rishilodhi