Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
10 views

Stack Assignment

Uploaded by

shivamp071y
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Stack Assignment

Uploaded by

shivamp071y
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Algorithm to Convert Infix to Postfix

1. Initialize an empty stack for holding operators and an empty list (or string) for the output postfix
expression.
2. Iterate through each character in the infix expression:
 If the character is an operand, add it directly to the postfix expression.
 If the character is an opening parenthesis ‘(‘, push it onto the stack.
 If the character is a closing parenthesis ‘)’, repeatedly pop from the stack and add to the postfix
expression until an opening parenthesis ‘(‘ is encountered on the stack. Pop and discard the
opening parenthesis from the stack.
 If the character is an operator (e.g., +, -, *, /):
 While the stack is not empty, and the top of the stack has an operator with greater or equal
precedence than the current operator, pop the stack and add the operator to the postfix
expression.
 Push the current operator onto the stack.
3. After reading the infix expression (if reach end of expression), pop any remaining operators from the
stack and add them to the postfix expression.
4. Return the postfix expression.
Operator Precedence and Associativity
Operator precedence determines how operators are prioritized in expressions. For example, multiplication (*)
and division (/) have higher precedence than addition (+) and subtraction (-).
Associativity (usually left-to-right for most operators) determines the order of operations when two operators
share the same precedence.
Example of Infix to Postfix Conversion
Example 1:
Infix Expression : A+(B*C+D)/E
Input Postfix
Stack Action
Token Expression

A A Add A into expression string

+ + A Push ‘+’ into stack

( +( A Push ( into stack

B +( AB Add B into expression string

* +(* AB Push ‘*’ into stack

C +(* ABC Add C into expression string

‘+’ operator has less precedence than ‘*’, so pop * and add
+ +(+ ABC* to
expression string

D +(+ ABC*D Add D into expression string

) + ABC*D+ ) has come so pop + and add it to expression string

/ +/ ABC*D+ / has higher precedence than + so push / into stack

Add E into expression string and pop all operators one by


E +/ ABC*D+E/+ one from
stack and add it to expression string
Postfix expression is ABC*D+E/+

Q:What is the main reason that computers can directly execute instructions represented in
postfix notation?
Ans: Postfix notation is the standard format for all programming languages
Postfix notation is easier to understand
Postfix notation does not require interpreting nested brackets or determining precedence rules
Postfix notation is more efficient to store in memory

Infix to Prefix Conversion Algorithm


Iterate the given expression from left to right, one character at a time
Step 1: First reverse the given expression and convert “( “ to “)” and vice versa
Step 2: If the scanned character is an operand, put it into prefix expression.
Step 3: If the scanned character is an operator and operator's stack is empty, push operator into operators'
stack.
Step 4: If the operator's stack is not empty, there may be following possibilities.
 If the precedence of scanned operator is greater than the top most operator of operator's
stack, push this operator into operator 's stack.
 If the precedence of scanned operator is less than the top most operator of operator's stack,
pop the operators from operator's stack untill we find a low precedence operator than the
scanned character.
 If the precedence of scanned operator is equal then check the associativity of the operator. If
associativity left to right then simply put into stack. If associativity right to left then pop the
operators from stack until we find a low precedence operator.
 If the scanned character is opening round bracket ‘(‘, push it into operator's stack.
 If the scanned character is closing round bracket ‘)', pop out operators from operator's stack
until we find an opening bracket '('.
Repeat Step 2, 3 and 4 till expression has character or till you reach end of expression.
Step 5: Now pop out all the remaining operators from the operator's stack and push into postfix expression.
Step 6: Reverse the expression to get prefix notation and Exit

Infix to Prefix conversion example


Infix Expression: (A+B)+C-(D-E)^F
First reverse the given infix expression
After Reversing: F^(E-D)-C+(B+A)
Input Stack Postfix Action
expression Expression

F F Add F into expression string

^ ^ F Push ‘^’ into stack

( ^( FE Push ‘(’ into stack

E ^( FE Add E into expression string

– ^(- FE Push ‘-‘ into stack

D ^(- FED Add D into expression string

) ^(- FED- ‘)‘ Pair matched, so pop operator ‘-‘

– – FED-^ ‘-‘ operator has less precedence than ‘^’, so pop ^ and add to expression
string

C – FED-^C Add C into expression string


+ + FED-^C- Pop – because – and + have left associativity

( +( FED-^C- Push ‘(’ into stack

B +( FED-^C-B Add B into expression string

+ +(+ FED-^C-B Push ‘+’ into stack

A +(+ FED-^C-BA Add A into expression string

) + FED-^C-BA+ ‘(‘ Pair matched, so pop operator ‘+’

FED-^C-BA++ Pop all operators one by one as we have reached end of the expression

Now reverse the expression to get prefix expression ++AB-C^-DEF

Practice Questions Infix to Postfix, Infix to Prefix


1. ((A + B) – C * (D / E)) + F
2. A + B * C / D – E
3. A/B^C-D
4. a+(b*c(d/e^f)*g)*h)
5. a+b*(c^d-e)^(f+g*h)-i

You might also like