Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
43 views8 pages

Infix, Postfix and Prefix Evalution

The document discusses different notation styles for mathematical expressions - infix, postfix, and prefix - and how to use a stack to evaluate expressions in each notation. It explains that infix is how humans write expressions but is harder for computers, while postfix and prefix make use of stacks. The key steps provided to evaluate expressions in each notation using a stack are: 1) push operands to the stack; 2) when an operator is encountered, pop operands and push the result. Examples are given of evaluating sample expressions in each notation using this stack process.

Uploaded by

Hager Massoud
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views8 pages

Infix, Postfix and Prefix Evalution

The document discusses different notation styles for mathematical expressions - infix, postfix, and prefix - and how to use a stack to evaluate expressions in each notation. It explains that infix is how humans write expressions but is harder for computers, while postfix and prefix make use of stacks. The key steps provided to evaluate expressions in each notation using a stack are: 1) push operands to the stack; 2) when an operator is encountered, pop operands and push the result. Examples are given of evaluating sample expressions in each notation using this stack process.

Uploaded by

Hager Massoud
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Infix, Postfix and Prefix expressions evaluation using Stack

• Infix Expression: Operators are written between the numbers they operate on, e.g.
3 + 4.

Infix Expressions are harder for Computers to evaluate because of the additional
work needed to decide precedence. Infix notation is how expressions are written and
recognized by humans and, generally, input to programs

So, stack is used by Computer to be evaluated

How to use Stack to evaluate Infix Expression:

Step 1: Create two stacks - the number stack and the operator stack.
Step 2: Push the number to the number stack if it is an number.
Step 3: If it is an operator, check if the operator stack is empty.
Step 4: If the operator stack is empty, push it to the operator stack.
Step 5: If the operator stack is not empty, compare the priority of the operator and the
top character in the stack.
If the character’s precedence is greater than or equal to the precedence of the stack top
of the operator stack, then push the character to the operator stack.
Otherwise, pop the elements from the stack until the character’s precedence is less or
the stack is empty.
Step 6: If the character is “(“, push it into the operator stack.
Step 7: If the character is “)”, then pop until “(” is encountered in the operator stack.
The Priority of the Operators
• Parenthesis ()
• Multiplication and division * /
• Addition and subtraction + –

Example

Infix expression : 6/2-3+4*2


So, The Result of this Infix expression: 6/2-3+4*2 is 8
After that pop the result from the Stack
• postfix Expression: Operators are written after the numbers they operate on, e.g. 3
4+.

How to use Stack to evaluate postfix Expression:

Step 1: Start from left of the expression.


Step 2: Create an number stack.
Step 3: If the character is an number, push it to the operand stack.
Step 4: If the character is an operator, pop two numbers from the stack, operate
and push the result back to the stack.
Step 5:After the entire expression has been traversed, pop the final result from
the stack.

Example

postfix expression : 6 2 / 3 - 4 2 *
So The Result of this Postfix expression: 6/2-3+4*2 is 8
After that pop the final result from Stack
• Prefix Expression: Operators are before the numbers they operate on, e.g. 3 4+.

How to use Stack to evaluate prefix Expression:

Step 1: start form right of the expression.


Step 2: Create an number stack.
Step 3: If the character is an number, push it to the number stack.
Step 4: If the character is an operator, pop two operands from the stack, operate
and push the result back to the stack.
Step 5:After the entire expression has been traversed, pop the final result from
the stack.

Example

Prefix expression : - / * 2 * 5 + 3 6 5 2
So, The Result of this Prefix expression:- / * 2 * 5 + 3 6 5 2 is 16
After that pop the result from Stack

You might also like