0% found this document useful (0 votes)
14 views5 pages

Exp 3

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)
14 views5 pages

Exp 3

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

Government College of Engineering, Karad Programming for Problem Solving Lab

Experiment No. 3

Title: Implement stack as an ADT to perform expression conversion and evaluation for infix to
Postfix.

Outcome: Students can perform expression conversion and evaluation for infix to postfix and
its related applications.

Theory:

One of the applications of Stack is in the conversion of arithmetic expressions in high-level


programming languages into machine readable form. As our computer system can only
understand and work on a binary language, it assumes that an arithmetic operation can take
place in two operands only e.g., A+B, C*D,D/A etc. But in our usual form an arithmetic
expression may consist of more than one operator and two operands e.g. (A+B)*C(D/(J+D)).

These complex arithmetic operations can be converted into polish notation using stacks which
then can be executed in two operands and an operator form.

Infix Expression

It follows the scheme of <operand><operator><operand> i.e. an <operator> is preceded and


succeeded by an <operand>. Such an expression is termed infix expression. E.g., A+B

Postfix Expression

It follows the scheme of <operand><operand><operator> i.e. an <operator> is succeeded by


both the <operand>. E.g., AB+

Algorithm to convert Infix to Postfix


Let, X is an arithmetic expression written in infix notation. This algorithm finds the equivalent
postfix expression Y.

1. Push “(“onto Stack, and add “)” to the end of X.


2. Scan X from left to right and repeat Step 3 to 6 for each element of X until the Stack is
empty.
3. If an operand is encountered, add it to Y.
4. If a left parenthesis is encountered, push it onto Stack.
5. If an operator is encountered ,then:
1. 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.
2. Add operator to Stack.
[End of If]
6. If a right parenthesis is encountered ,then:
1. Repeatedly pop from Stack and add to Y each operator (on the top of Stack) until
a left parenthesis is encountered.
2. Remove the left Parenthesis.
[End of If]
[End of If]

Department of Information Technology


Let’s take an example to better understand the algorithm

Infix Expression: A+ (B*C-(D/E^F)*G)*H, where ^ is an exponential operator.

Resultant Postfix Expression: ABC*DEF^/G*-H*+

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.

Following is an algorithm for evaluation postfix expressions.


1) Create a stack to store operands (or values).
2) Scan the given expression and do the 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 the 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

Department of Information Technology


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. The 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. The stack now becomes ‘5’.
6) Scan ‘9’, it’s a number, we push it to the stack. The 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 the stack. The stack now becomes ‘-4’.
8) There are no more elements to scan, we return the top element from the stack (which is the
only element left in a stack).

Advantage of Postfix Expression over Infix Expression

An infix expression is difficult for the machine to know and keep track of precedence of
operators. On the other hand, a postfix expression itself determines the precedence of operators
(as the placement of operators in a postfix expression depends upon its precedence).Therefore,
for the machine it is easier to carry out a postfix expression than an infix expression.

Analysis:

1.

2.

3.

List of similar programs: Solve any one.


1. Write a program for implementation of Infix to Postfix using different Precedence
Values for In-Stack and Out-Stack.
2. Write a program to convert Infix notation to Expression Tree.
3. Write a program to check for Balanced Brackets in an expression (well-formedness)
using Stack

Source code of Implemented Programs:

Title Program:

Practice Program:

Screenshots of Output:

List of sample questions for oral examination:


1. What are infix, prefix, and postfix notations?
2. What is the need for circular array to implement queue?
3. What is queue full condition if it is implemented with an array?
4. What are the applications of circular queue?
Department of Information Technology
5. How a circular queue can be implemented using array?

Conclusion:

Department of Information Technology

You might also like