Exp 4
Exp 4
Experiment No. 4
Title: Implement stack as an ADT to perform expression conversion and evaluation for infix to
prefix.
Outcome: Students can perform expression conversion and evaluation for infix to prefix and its
related applications.
Theory:
While we use infix expressions in our day to day lives. Computers have trouble understanding
this format because they need to keep in mind rules of operator precedence and also brackets.
Prefix and Postfix expressions are easier for a computer to understand and evaluate.
Given two operands a and b and an operator , the infix notation implies that O will be placed in
between a and b i.e a b . When the operator is placed after both operands i.e ab, it is called
postfix notation. And when the operator is placed before the operands i.e a b, the expression in
prefix notation.
Given any infix expression we can obtain the equivalent prefix and postfix format.
Input: A * B + C / D
Output: + * A B/ C D
To convert an infix to postfix expression refer to this article Stack | Set 2 (Infix to Postfix). We
use the same to convert Infix to Prefix.
Step 1: Reverse the infix expression i.e A+B*C will become C*B+A. Note while
reversing each ‘(‘ will become ‘)’ and each ‘)’ becomes ‘(‘.
Step 2: Obtain the postfix expression of the modified expression i.e CB*A+.
Step 3: Reverse the postfix expression. Hence in our example prefix is +A*BC.
Let, X is an arithmetic expression written in infix notation. This algorithm finds the
equivalent prefix expression Y.
Reverse the infix expression.
Make Every “ ( ” as “ ) ” and every “ ) ” as “ ( ”
Push “ ( ” onto Stack, and add “ ) ” to the end of X.
Scan X from left to right and repeat Step 3 to 6 for each element of X until the Stack is
empty.
If an operand is encountered, add it to Y.
If a left parenthesis is encountered, push it onto Stack.
If an operator is encountered ,then :
Department of Information Technology
Repeatedly pop from Stack and add to Y each operator (on the top of Stack) which has
the same precedence as or higher precedence than operator.
Add operator to Stack.
[ End of If ]
If a right parenthesis is encountered ,then :
Repeatedly pop from Stack and add to Y each operator (on the top of Stack) until a left
parenthesis is encountered.
Remove the left Parenthesis.
[ End of If ]
[ End of If ]
Reverse the prefix expression.
END.
Example
Infix Expression : A + ( B * C - ( D / E ^ F ) * G ) * H
Postfix
Scanned Stack Description
Expression
( Start
H ( H
* (* H
( (*( H
G (*( HG
* (*(* HG
( (*(*( HG
F (*(*( HGF
^ (*(*(^ HGF
E (*(*(^ HGFE
/ (*(*(/ HGFE^ ' ^ ' is at highest precedence then ' / '
Complexity:
Stack operations like push() and pop() are performed in constant time. Since we scan all the
characters in the expression once the complexity is linear in time i.e .o(n)
Prefix and Postfix expressions can be evaluated faster than an infix expression. This is because
we don’t need to process any brackets or follow operator precedence rule. In postfix and prefix
expressions which ever operator comes before will be evaluated first, irrespective of its priority.
Also, there are no brackets in these expressions. As long as we can guarantee that a valid prefix
or postfix expression is used, it can be evaluated with correctness.
Algorithm
EVALUATE_PREFIX (STRING)
Step 1: Put a pointer P at the end of the end
Step 2: If character at P is an operand push it to Stack
Step 3: If the character at P is an operator pop two
elements from the Stack. Operate on these elements
according to the operator, and push the result
back to the Stack
Department of Information Technology
Step 4: Decrement P by 1 and go to Step 2 as long as there
are characters left to be scanned in the expression.
Step 5: The Result is stored at the top of the Stack,
return it
Step 6: End
Expression: +9*26
Result: 21
Examples:
Input : -+8/632
Output :
Input : -+7*45+20
Output :
Complexity: The algorithm has linear complexity since we scan the expression once and
perform at most O (N) push and pop operations which take constant time.
Department of Information Technology
Analysis:
1.
2.
3.
Title Program:
Practice Program:
Screenshots of Output:
Conclusion: