Stack 1
Stack 1
Introduction to Stacks
Introduction to Stacks
2
What is a Stack?
Applications of Stacks.
3
Applications of Stacks
Some direct applications:
Conversion of tail-recursive algorithms to iterative ones
Evaluation of arithmetic expressions by compilers [infix
to postfix conversion, infix to prefix conversion,
evaluation of postfix expressions]
(5+9)*2+6*5
An ordinary arithmetical expression like the above
is called infix-expression - binary operators appear
in between their operands.
The order of operations evaluation is determined
by the precedence rules and parentheses.
When an evaluation order is desired that is
different from that provided by the precedence,
parentheses are used to override precedence rules.
Application of Stacks - Evaluating Postfix Expressions (Cont’d)
5
Examples:
3+4*5 (3 + (4 * 5) ) 345*+
a/b^c–d*e–a*c^3^4 abc^/de*ac34^^*--
( (a / (b ^ c)) – ( (d * e) – (a * (c ^ (3 ^ 4) ) ) ) )
The following
Application algorithmPostfix
of Stacks - Evaluating usesExpression
a stack to evaluate
(Cont’d) a
postfix expressions.
10
postfixString = “”;
while(infixString has tokens){
Get next token x;
if(x is operand)
Append x to postfixString;
else if(x is ‘(’ )
stack.push(x);
else if(x is ‘)’ ){
y = stack.pop();
while(y is not ‘(’ ){
Append y to postfixString;
y = stack.pop();
}
discard both ‘(‘ and ‘)’;
}
//Application of Stacks – Infix to Postfix Conversion
11
else if(x is operator){
while(stack is not empty){
y = stack.getTop(); // top value is not removed from stack
if(y is ‘(‘ ) break;
if(y has low precedence than x) break;
if(y is right associative with equal precedence to x) break;
y = stack.pop();
Append y to postfixString;
}
push x;
}
while(stack is not empty){
y = stack.pop( );
Append y to postfixString;
}
Application of Stacks – Infix to Postfix Conversion (cont’d)
12
step1: step2:
step3: step4:
Application of Stacks – Infix to Postfix Conversion (cont’d)
13
step5: step6:
step7: step8:
Application of Stacks – Infix to Postfix Conversion (cont’d)
14
step9: step10:
step11: step12:
Application of Stacks – Infix to Postfix Conversion (cont’d)
15
step13: step14:
step15: step16:
Application of Stacks – Infix to Postfix Conversion (cont’d)
16
step17: step18:
Application of Stacks – Infix to Prefix Conversion
17
An infix to prefix conversion algorithm:
1. Reverse the infix string
2. Perform the infix to postfix algorithm on the reversed string
3. Reverse the output postfix string
Example: (A + B) * (B – C)
reverse
(C – B) * (B + A)
C B - B A + *
reverse
* + A B - B C
Exercise:-
18
1– Convert the following infix expressions into postfix notations using two
stacks:
I. a+b^2/4-(c*5/8-f) ^3
II. m^3 or n-b/2 and (m+n)
III. a+b+c*(-d/3^4)and not f
IV. a^(b/2)or(x+y/3-w)and (c^2)^3
V. x^n+(m-p*4)^f and (-b)^2/c+6
VI. a+b-c or (x*2^n)-m/p and f^2
VII. a^(b/2)and (x-y/3+w)or(c^3)^2
VIII. n^x-(p*4+m)/f or –c/b^2
IX. b-a+c and n^x-(p/m or f^2)
Homework:-
1- write an algorithm to execute infix notation (Number consists of
more than one rank) using the stack.
Hint:- For example (12+28=40).