0% found this document useful (0 votes)
22 views

Exp 4

Uploaded by

shaikhtamim8209
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)
22 views

Exp 4

Uploaded by

shaikhtamim8209
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/ 6

Government College of Engineering, Karad Programming for Problem Solving Lab

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

Input: (A - B/C) * (A/K-L)


Output: *-A/BC-/AKL

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.

Algorithm to Convert Infix To Prefix:

 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

Reverse the infix expression :


H*)G*)F^E/D(-C*B(+A

Make Every “ ( ” as “ ) ” and every “ ) ” as “ ( ”


H*(G*(F^E/D)-C*B)+A

Convert expression to postfix form:


H*(G*(F^E/D)-C*B)+A

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 ' / '

Department of Information Technology


D (*(*(/ HGFE^D
) (*(* HGFE^D/
- (*(- HGFE^D/∗
C (*(- HGFE^D/∗C
* (*(-* HGFE^D/∗C
B (*(-* HGFE^D/∗CB
POP from top on Stack, that's why ' * '
) (* HGFE^D/∗CB∗-
come first
+ (+ HGFE^D/∗CB∗-∗ ' * ' is at highest precedence then ' + '
HGFE^D/∗CB∗-
A (+
∗A
HGFE^D/∗CB∗-
) Empty END
∗A+

Reverse the postfix expression:


+A*-*BC*/D^EFGH

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)

Evaluation of Prefix Expressions

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

Character | Stack | Explanation


Scanned | (Front to |
| Back) |
-------------------------------------------
6 6 6 is an operand,
push to Stack
2 6 2 2 is an operand,
push to Stack
* 12 (6*2) * is an operator,
pop 6 and 2, multiply
them and push result
to Stack
9 12 9 9 is an operand, push
to Stack
+ 21 (12+9) + is an operator, pop
12 and 9 add them and
push result to Stack

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.

List of similar programs: Solve any one.


1. Write a program for implementation of Infix to prefix conversion using two stacks.
2. Write a program to print all words matching a pattern in CamelCase Notation
Dictionary.

Source code of Implemented Programs:

Title Program:

Practice Program:

Screenshots of Output:

List of sample questions for oral examination:


1. Which will you use to convert infix to postfix prefix operators?
2. How do I convert infix to prefix manually?
3. Which is required to convert the infix to prefix notation?
4. How do you solve an infix expression?
5. What is infix prefix and postfix?

Conclusion:

Department of Information Technology

You might also like