Title: CSci 270: Data Structures Fall 2004
1CSci 270 Data StructuresFall 2004
2Application of Stacks Run-time Stack
Read 4.3
Whenever a function begins execution (i.e., is
activated), an activation record (or stack
frame) is created to store the current
environment for that function. Its contents
include
What kind of data structure should be used to
store these so that they can be recovered and the
system reset when the function resumes execution?
3Problem FunctionA can call FunctionB FunctionB
can call FunctionC . . . When a function calls
another function, it interrupts its own execution
and needs to be able to resume its execution in
the same state it was in when it was
interrupted. When FunctionC finishes, control
should return to FunctionB. When FunctionB
finishes, control should return to FunctionA.
Exampleon slide 5
So, the order of returns from a function is the
reverse offunction invocations that is, LIFO
behavior.? Use a stack to store the activation
records. Since it is manipulated at run-time, it
is called the run-time stack.
4What happens when a function is called? 1. Push
a copy of its activation record onto the run-time
stack 2. Copy its arguments into the parameter
spaces 3. Transfer control to the address of the
function's body The top activation record in the
run-time stack is always that of the function
currently executing. What happens when a
function terminates? 1. Pop activation record of
terminated function from the run-time
stack 2. Use new top activation record to
restore the environment of the interrupted
function and resume execution of the
interrupted function.
5Examples
Trace run-time stack for the following . . .int
main() . . . f2(...) f3(...)void
f1(...) . . .void f2(...) ... f1(...)
...void f3(...) ... f2(...) ...
int factorial(int n) if (n lt 2) return
1 else return n factorial(n-1) What
happens to the run-time stack when the following
statement executes? int answer factorial(4)
This pushing and popping of the run-time stack is
the real overhead associated with function calls
that inlining avoids by replacing the function
call with the body of the function.
6Application of Stacks RPN
1. What is RPN (Reverse Polish Notation)?A
notation for arithmetic expressions in which
operators are written after the operands.
Expressions can be written without
usingparentheses.Developed by Polish logician,
Jan Lukasiewics, in 1950's Infix notation
operators written between the operands Postfix
" (RPN) operators written after the
operands Prefix " operators
written before the operands
Examples INFIX RPN (POSTFIX) PREFIX A
B A B C A (B C) A - (B - (C - D)) A -
B - C - D
A B
A B
A B C
A B C
A B C
A B C
A B C D - - -
- A - B - C D
A B - C - D -
- - - A B C D
7Evaluating RPN Expressions
"By hand" (Underlining technique) 1. Scan the
expression from left to right to find an
operator. 2. Locate ("underline") the last two
preceding operands and combine them using this
operator. 3. Repeat until the end of the
expression is reached. Example 2 3 4 5
6 - -
2 3 4 5 6 - -
2 7 5 6 - - 2 7 5 6 - -
2 7 -1 - 2 7 -1 -
2 8
16
2 8
8Stack Algorithm (p.195) Receive An RPN
expression. Return A stack whose top element is
the value of RPN expression (unless an error
occurred). 1. Initialize an empty stack. 2.
Repeat the following until the end of the
expression is encountered a. Get next token
(constant, variable, arithmetic operator) in
the RPN expression. b. If token is an
operand, push it onto the stack. If it is an
operator, then (i) Pop top two values
from the stack. If stack does not contain
two items, signal error due to a malformed
RPN and terminate evaluation. (ii) Apply the
operator to these two values. (iii) Push the
resulting value back onto the stack. 3. When the
end of expression encountered, its value is on
top of the stack (and, in fact, must be the
only value in the stack).
Generate codeLOAD operand1op
operand2STORE TEMP
Push TEMP
A B C D E - -
9Sample RPN Evaluation (p. 196)
Example 2 3 4 5 6 - - Push 2 Push 3 Push
4 Read Pop 4, Pop 3, Push 7 Push 5 Push
6 Read - Pop 6, Pop 5, Push -1 Read - Pop -1,
Pop 7, Push 8 Read Pop 8, Pop 2, Push 16
3 4 7
5 - 6 -1
7 - -1 8
2 8 16
10 Unary minus causes problems Example 5 3 -
- 5 3 - - 5 -3 - 8
5 3 - - 2 - -2
OR
Use a different symbol 5 3 -
5 3 -
11Converting Infix to RPN
By hand Represent infix expression as an
expression tree
A B C
A (B C)
((A B) C) / (D - E)
12Traverse the tree in Left-Right-Parent order
(postorder) to get RPN
A
B
C
D
E
-
/
Traverse tree in Parent-Left-Right order
(preorder) to get prefix
/ A B
- D E
C
Traverse tree in Left-Parent-Right order
(inorder) to get infix must insert ()'s
(A B)
( C)
/
( )
(D - E)
13By hand "Fully parenthesize-move-erase"
method 1. Fully parenthesize the expression. 2.
Replace each right parenthesis by the
corresponding operator. 3. Erase all left
parentheses. Examples
((A B) C)
A B C ?
(A (B C) )
A (B C) ?
? ((A B C ? A B C
? (A (B C ? A B C
Exercise
((A B) C) / (D - E)
14Stack Algorithm (p.199) 1. Initialize an
empty stack of operators. 2. While no error has
occurred and end of infix expression not
reached a. Get next token in the infix
expression. b. If token is (i) a left
parenthesis Push it onto the stack. (ii) a
right parenthesis Pop and display stack elements
until a left parenthesis is encountered, but
do not display it. (Error if stack empty
with no left parenthesis found.) (iii) an
operator If stack empty or token has higher
priority than top stack element, push token
onto stack. (Left parenthesis in stack has
lower priority than operators) Otherwis
e, pop and display the top stack element
then repeat the comparison of token with new
top stack item. (iv) an operand Display
it. 3. When end of infix expression reached, pop
and display stack items until stack is empty.
15Example Push ( Output Display A Push
Display B Push Display C Read ) Pop ,
Display , Pop , Display , Pop ( Push
/ Push ( Display D Push - Push ( Display
E Push - Display F Read ) Pop -, Display -,
Pop ( Read ) Pop -, Display -, Pop ( Pop /,
Display /
(ABC)/(D-(E-F))
(ABC)/(D-(E-F))
(ABC)/(D-(E-F))
(ABC)/(D-(E-F))
(ABC)/(D-(E-F))
(ABC)/(D-(E-F))
(ABC)/(D-(E-F))
(ABC)/(D-(E-F))
(ABC)/(D-(E-F))
(ABC)/(D-(E-F))
(ABC)/(D-(E-F))
(ABC)/(D-(E-F))
(ABC)/(D-(E-F))
(ABC)/(D-(E-F))
(ABC)/(D-(E-F))
(ABC)/(D-(E-F))
(ABC)/(D-(E-F))
(ABC)/(D-(E-F))
A
AB
ABC
ABC
ABC
ABCD
ABCDE
ABCDEF
ABCDEF-
ABCDEF--
ABCDEF--/