0% found this document useful (0 votes)
101 views2 pages

Infix Expression Evaluation Using Stack

The document describes an algorithm for evaluating infix expressions using stacks, which involves using two stacks - an operand stack to store operands and an operator stack to store operators, and using precedence rules to determine the order of operations as characters are read from left to right. An example is provided of evaluating the expression 2 * (5 * (3 + 6)) / 5 – 2 step-by-step using the algorithm. The document concludes by stating a C++ program should be written to implement the described infix expression evaluation algorithm using stacks.

Uploaded by

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

Infix Expression Evaluation Using Stack

The document describes an algorithm for evaluating infix expressions using stacks, which involves using two stacks - an operand stack to store operands and an operator stack to store operators, and using precedence rules to determine the order of operations as characters are read from left to right. An example is provided of evaluating the expression 2 * (5 * (3 + 6)) / 5 – 2 step-by-step using the algorithm. The document concludes by stating a C++ program should be written to implement the described infix expression evaluation algorithm using stacks.

Uploaded by

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

Infix Expression Evaluation Using Stack

To begin with, let us see how infix expression evaluation using stack. 

Algorithm
Step 1: Create two stacks - the operand stack and the character stack.
Step 2: Push the character to the operand stack if it is an operand.
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 precedence 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.

Example

2 * (5 * (3 + 6)) / 5 – 2

Character Action Operand Stack Operator Stack


2 Push to the operand stack 2
* Push to the operator stack 2 *
( Push to the operator stack 2 (*
5 Push to the operand stack 5 2  (*
* Push to the operator stack 5 2  *(*
( Push to the operator stack 52 (*(*
3 Push to the operand stack 3 5 2  (*(*
+ Push to the operator stack 321 +(*(*
6 Push to the operand stack 6 3 5 2  +(*(*
) Pop 6 and 3 5 2  +(*(*
Pop + 5 2  (*(*
6 + 3 = 9, push to operand stack 9 5 2  (*(*
Pop ( 9 5 2  *(*
) Pop 9 and 5 2 *(*
Pop * 2 (*
9 * 5 = 45, push to operand stack 45 2 (*
Pop ( 45 2 *
/ Push to the operator stack 45 2 /*
5 Push to the operand stack 5 45 2 /*
– Pop 5 and 45 2 /*
Pop / 2 *
45/5 = 9, push to the operand stack 92 *
Pop 9 and 2 *
Pop *
9 * 2 = 18, push to operand stack 18
Push – to the operator stack 18 –
2 Push to the operand stack 2 18 –
Pop 2 and 18 –
Pop –
18 – 2 = 16, push to operand stack 16

Write a program in C++ to implement the algorithm described above.

You might also like