0% found this document useful (0 votes)
33 views18 pages

Stack Postfixconversion 3-4

Here are the steps to evaluate the given postfix expression using a stack: 6 - Push 6 onto the stack 2 - Push 2 onto the stack 3 - Push 3 onto the stack +- - Pop 3 and 2 from the stack, compute 2-3 and push the result (-1) onto the stack 3 - Push 3 onto the stack 8 - Push 8 onto the stack 2 - Push 2 onto the stack /+ - Pop 2 and 8 from the stack, compute 8/2 and push the result (4) onto the stack * - Pop 4 and -1 from the stack, compute 4*-1 and push the result (-4) onto the stack 2 - Push 2 onto the stack

Uploaded by

saadinves04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views18 pages

Stack Postfixconversion 3-4

Here are the steps to evaluate the given postfix expression using a stack: 6 - Push 6 onto the stack 2 - Push 2 onto the stack 3 - Push 3 onto the stack +- - Pop 3 and 2 from the stack, compute 2-3 and push the result (-1) onto the stack 3 - Push 3 onto the stack 8 - Push 8 onto the stack 2 - Push 2 onto the stack /+ - Pop 2 and 8 from the stack, compute 8/2 and push the result (4) onto the stack * - Pop 4 and -1 from the stack, compute 4*-1 and push the result (-4) onto the stack 2 - Push 2 onto the stack

Uploaded by

saadinves04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Stack

• A variable sized data structure in which insertion or deletion


is made just from one end
or
• A LIFO data structure is called Stack

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)

Polish Suffix notations


• It is named after Polish mathematician Jan Lukasiewiez.
The operator symbol is placed before its two operands in
this notations
• Examples: +XY, AB+, CD-, EF*
Application of Stack

• 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

• Read Symbol Stack


A 2 PUSH(A=2)
B 23 PUSH(B=3)
C 234 PUSH(C=4)
* 2 12 POP(4),POP(3),PUSH(R)
D 2 12 6 PUSH(D=6)
/ 22 POP(6),POP(12),PUSH(R)
+ 4 POP(2),POP(2),PUSH(R)
Evaluation of PSN
Evaluate the following postfix expression using stack.

6 2 3+-3 8 2 /+* 2 -3+

You might also like