CSCE 3110 Data Structures & Algorithm Analysis: Rada Mihalcea
CSCE 3110 Data Structures & Algorithm Analysis: Rada Mihalcea
Rada Mihalcea
http://www.cs.unt.edu/~rada/CSCE3110
Stack Applications
Reading: Chap. 3 Weiss
Applications
a = 4, b = c = 2, d = e = 3
Interpretation 1:
((4/2)-2)+(3*3)-(4*2)=0 + 8+9=1
Interpretation 2:
(4/(2-2+3))*(3-4)*2=(4/3)*(-1)*2=-2.66666…
bitwise or 6 left-to-right
logical or 4 left-to-right
?: conditional 3 right-to-left
= += -= assignment 2 right-to-left
/= *= %=
<<= >>=
&= ^= =
, comma 1 left-to-right
user compiler
Infix Postfix
2+3*4 234*+
a*b+5 ab*5+
(1+2)*7 12+7*
a*b/c ab*c/
(a/(b-c+d))*(e-a)*c abc-d+/ea-*c*
a/b-c+d*e-a*c ab/c-de*ac*-
*symbol =expr[(*n)++];
switch (*symbol) {
case ‘(‘ : return lparen;
case ’)’ : return rparen;
case ‘+’: return plus;
case ‘-’ : return minus;
case ‘/’ : return divide;
case ‘*’ : return times;
case ‘%’ : return mod;
case ‘\0‘ : return eos;
default : return operand;
/* no error checking, default is operand */
}
}
Infix to Postfix Conversion
(Intuitive Algorithm)
(1) Fully parenthesized expression
a / b - c + d * e - a * c -->
((((a / b) - c) + (d * e)) – (a * c))
/ - +* -*
(3) Delete all parentheses.
ab/c-de*+ac*-
two passes
The orders of operands in infix and postfix are the same.
a + b * c, * > +
( ) + - * / % eos
isp 0 19 12 12 13 13 13 0
icp 20 19 12 12 13 13 13 0
precedence stack[MAX_STACK_SIZE];
/* isp and icp arrays -- index is value of precedence
lparen, rparen, plus, minus, times, divide, mod, eos */
static int isp [ ] = {0, 19, 12, 12, 13, 13, 13, 0};
static int icp [ ] = {20, 19, 12, 12, 13, 13, 13, 0};