Stack Postfixconversion 3-4
Stack Postfixconversion 3-4
Operations on Stack
1) Push ( to insert an element into stack)
2) Pop( to take an element out of stack)
The last element inserted is deleted or taken out first and
first element at last
The stack can be implemented using Array as well as Link
List
Stack using Arrays
• The no of elements that can be inserted in a stack is the
dimension of array. The current position of stack(i.e its
size is known by a pointer called its top.
Algorithm to insert data in stack Push(S,Top, X, N)
1 If Top >= N then
write (‘Stack full’) and exit
2 Top=Top+1
3 S[Top]= X
4 Exit
Stack Operations
Algorithm to delete an element from Stack Pop(S, Top)
1 If Top= 0 then
write(‘ stack is empty’) and exit
2 Pop = S[Top]
3 Top= Top-1
4 Exit
Applications of Stack
i) Recursion
ii) Evaluation of Arithmetic expressions
a) Infix notation
b) prefix notations
c) postfix notations
Recursion
• The calling of a subprogram to itself is called recursions
Examples: Factorial of a number
Fact(N)
1) If N= 0 then
Fact =1
2) Fact= N* Fact(N-1)
• Fact(int f) {
• if (f == 1) return 1;
return (f * Fact(f - 1));
}
int main() {
int fact;
fact = Fact(4);
printf("Factorial is %d", fact);
return 0;
}
Application of Stack
PSN notations
PSN notations
Polish Suffix Notation
Infix Postfix
A+B AB+
12 + 60 – 23 12 60 + 23 –
(A + B)*(C – D ) AB+CD–*
A * (B + C / D) ABCD/+*
A * (B + C) / D ABC+*D/
PSN notations
• The expressions in which operands are preceded by the
operators are called PSN notations or postfix notations.
• Example: Following are PSN notations
AB+
DE*
ABC*+
• Convert A+B*C into PSN
Symbol scanned operator Stack PSN
A A
+ + A
B + AB
* +* AB
C +* ABC
$ ABC *+
RPN(Q, P)
• Suppose Q is expression in infix form. This pseudocode
covert this expression into equivalent postfix expression P
1) Scan Q from left to right and repeat Steps 2 to 5 for each
element of Q until the end of Expression
2) If an operand is encountered, add it to P
3) If a left parenthesis is encountered, push it onto stack
4) If an operator is encountered then:
a) Repeatedly pop from stack and add to P each operator which
has the same precedence as or higher precedence than (?)
b) Add (?) to stack
5) If a right parenthesis is encountered then:
a) Repeatedly pop from stack and add to P each operator until a
left parenthesis is encountered
b) remove left parenthesis
6) Exit
Algorithm for Infix to postfix Conversion
Stack s; //declare s stack
While(not end of input){
c= next input character
if (c is an operand)
add c to postfix string;
else{
while(!s.empty() && prcd(s.top(),c)) {
op= s.pop();
add op to the postfix string;
} s.push(c); } //end of else
} end of while
Algorithm for Infix to postfix Conversion
While (!s.empty()) {
op=s.pop();
add op to postfix string;
}
//prcd(op1,op2)
Where op1 and op2 are two operators
It returns true if op1 has precedence over op2
e.g. prcd(*,+) will return true
PSN Exercise
1 A + (B + C * D ^2 + 3)
2 ((A+B) * C –(D-E)) *(F+G)
3 A – B / (C* D^E)
Assignments
4 Write an algorithm that should evaluate a postfix expression
5 Write a program to convert
a) A prefix expression to postfix
b) A postfix expression to prefix
c) A prefix expression to infix
d) A postfix expression to infix
Pseudocode for evaluation of PSN
1 Scan the Postfix string from left to right.
2 Initialize an empty stack.
3 Repeat step 4 until the end of string
4 If the scanned character is an operand, Push it to
the stack.
Else
a) If the scanned character is an Operator, pop top two
elements and apply operator.
b) Push result back on top of stack
5 Pop last value from stack
5 Exit.
Algorithm for evaluation of PSN
Stack s; //declare s stack
While(not end of input){
e=get next element of input
if (e is an operand)
s.push(e);
else{
op2=s.pop();
op1=s.pop();
value=result of op1 and op2 by applying operator e
s.push(value); }
} finalresult=s.pop();
Example
• infix expression: 1+ 2 *3
• Postfix :1 2 3 * +
Symbol Stack
1 1
2 1, 2
3 1, 2, 3
* 1, 6
+ 7
Example Infix: A+(B*C)/D Postfix:ABC*D/+
with A=2, B=3, C=4 and D=6