CS 23001 CS II Data Structures and Abstraction
CS 23001 CS II: Data Structures and Abstraction Stacks, Prefix, Postfix, Infix (1) Xiang Lian Department of Computer Science Kent State University Email: xlian@kent. edu Homepage: http: //www. cs. kent. edu/~xlian/ 1
Objectives • Stacks • Prefix, postfix, and infix notations • Conversion from infix to pre/postfix 2
Stack 3
Property of the Stack • First In Last Out (FILO) 4
Illustration of Method-Call Stack • Stack double Maximum(double x, double y, double z) { double rlt; // Maximum() code return rlt; } stack void main () { double a, b, c; Maximum() // … std: : cout<<Maximum(a, b, c)<<std: : endl; } main() z y rlt x c b a 5
Function double Maximum(double x, double y, double z) { double rlt; // Maximum() code return rlt; } void main () { double a, b, c; // … std: : cout<<Maximum(a, b, c)<<std: : endl; } 6
Equivalent Function Call Using a Stack void main () { stack s; double a, b, c; // … s. push(a); s. push(b); s. push(c); double z = s. pop(), y = s. pop(), x = s. pop(); double rlt; // Maximum() code s. push(rlt); std: : cout<<s. pop()<<std: : endl; } 7
Example • Template version of stacks • … 8
Infix Notation • Operators operands are written in-between their –X+Y –X-Y –X*Y –X/Y 9
Prefix Notation • Operators are written before their operands –+XY –-XY –*XY –/XY 10
Postfix Notation • Operators are written after their operands –XY+ –XY–XY* –XY/ 11
Examples 12
How to Obtain Infix, Prefix, and Postfix? • Infix: A*B + C/D ((A*B) + (C/D)) • Postfix: A B * C D / + • Prefix: + * A B / C D 13
14
- Slides: 14