Professional Documents
Culture Documents
Briana B. Morrison
Adapted from Alan Eugenio
Topics
Define Stack
APIs
Applications
Implementation
Array based
Linked list based
Stacks
Stacks
Stacks
Pushing/Popping a Stack
Because
Stacks
Stacks
METHOD INTERFACES
FOR THE
stack CLASS
Stacks
CLASS stack
Constructor
<stack>
Operations
<stack>
stack();
Create an empty stack
CLASS stack
Stacks
CLASS stack
Operations
<stack>
void pop();
Remove the item from the top of the stack.
Precondition:
The stack is not empty.
Postcondition: Either the stack is empty or
the stack has a new topmost
item from a previous push.
void push(const T& item);
Insert the argument item at the top of the stack.
Postcondition: The stack has a new item at
the top.
Stacks
CLASS stack
Operations
<stack>
Stacks
10
Stacks
11
12
Stacks
13
14
Stacks
15
Implementing Stack as
Extension of Vector
Stacks
16
17
stack Code
template<typename Item_Type>
void stack<Item_Type>::push(const Item_Type& item) {
container.push_back(item);
}
template<typename Item_Type>
Item_Type& stack<Item_Type>::top() {
return container.back();
}
template<typename Item_Type>
const Item_Type& stack<Item_Type>::top() const {
return container.back();
}
Stacks
18
19
STACK APPLICATIONS
Stacks
20
Applications of Stacks
Direct applications
Indirect applications
Stacks
21
'1 '
'A '
'A '
'A '
'F '
'F '
'F '
26 % 16 = 10
26 / 16 = 1
1 % 16 = 1
1 / 16 = 0
'A '
'F '
P o p '1 '
n u m Str = "1 "
P u s h D ig it C h a r a c t e r s
'F '
P o p 'A '
P o p 'F '
n u m Str = "1 A " n u m Str = "1 A F "
P o p D ig it C h a r a c t e r s
Stacks
22
STACK APPLICATION
23
24
25
26
27
main() {
The C++ run-time system keeps
int i = 5;
track of the chain of active functions
with a stack
foo(i);
When a function is called, the run}
time system pushes on the stack a
frame containing
foo(int j) {
Local variables and return value
int k;
Program counter, keeping track of
k = j+1;
the statement being executed
bar(k);
When a function returns, its frame is
}
popped from the stack and control
is passed to the method on top of
bar(int m) {
the stack
Stacks
bar
PC = 1
m=6
foo
PC = 3
j=5
k=6
main
PC = 2
i=5
28
A rg u m e n ts
in t n
R e tu rn A d d re ss
R e tL o c o r R e tL o c 2
A c tivatio n R e c o r d
S y s te m S ta ck
I n m a in ( ) :
c a ll f a c t ( 4 )
In fact(4 ):
c a ll f a c t ( 3 )
A rg u m e n t
R e tu rn
R etL o c1
A rg u m e n t
R e tu r n
R etL o c2
A rg u m e n t
R e tu r n
R etL o c1
Stacks
29
Stacks
30
Stacks
31
STACK APPLICATION
CONVERTING FROM
INFIX TO POSTFIX
Stacks
32
Stacks
33
OLD COMPILERS:
INFIX
MACHINE LANGUAGE
BECAUSE
OF
NEWER COMPILERS:
INFIX
POSTFIX
Stacks
MACHINE LANG.
34
POSTFIX
a
Stacks
35
Stacks
36
Stacks
37
Stacks
38
INFIX
POSTFIX
xy*z
Stacks
39
INFIX
POSTFIX
xy*z
40
INFIX
POSTFIX
xy*z
xy
Stacks
41
INFIX
POSTFIX
xy*z
xy
42
INFIX
POSTFIX
xy*z
xyz
Stacks
43
INFIX
POSTFIX
xy*z
xyz*
Stacks
44
POSTFIX
x*y z
xy
Stacks
45
Stacks
46
Stacks
47
Analysis:
Strategy:
Stacks
48
49
Stacks
50
INFIX
POSTFIX
a+b*c/d-e
Stacks
51
INFIX
POSTFIX
a+b*c/de
abc*d/+e
/
*
+
OPERATOR STACK
Stacks
52
53
CONVERT TO POSTFIX:
x * (y + z)
Stacks
54
INFIX
x * (y + z)
POSTFIX
xyz+*
+
(
*
OPERATOR STACK
Stacks
55
Stacks
56
Exercise
a+b-(c+d*e/(fg))*(h+i)
Stacks
57
Evaluating a Postfix
Expression.
Stacks
58
593+42**7
Stack Operations
Output
+*
push(5);
push(9);
push(3);
push(pop() + pop())
push(4);
push(2);
push(pop() * pop())
push(pop() * pop()) 5 96
push(pop() + pop())
push(pop() * pop())
Stacks
5
59
593
5 12
5 12 4
5 12 4 2
5 12 8
push(7) 5 96 7
5 103
515
59
Stacks
60
Stacks
61
I
N
F
I
X
C
identifier
)
(
+,-
H
A
*,/
empty
+,-
*,/
empty
APPEND
TO
POSTFIX
POP;
POP TO POP TO
PITCH ( POSTFIX POSTFIX
PUSH
PUSH
PUSH
ERROR
PUSH
PUSH
POP TO
POSTFIX
POP TO
POSTFIX
PUSH
PUSH
PUSH
PUSH
ERROR
POP TO
POSTFIX
POP TO
POSTFIX
POP TO
POSTFIX
Stacks
DONE
62
Stacks
63
STACK APPLICATION
EVALUATING A
Stacks
64
Stacks
65
3.
4.
5.
6.
7.
8.
9.
10.
11.
66
R P N (R e v e r s e P o lis h N o ta tio n ) e x p r e s s io n
2 . I d e n t if y 3 a s a n o p e r a n d .
P u s h in t e g e r 3 o n t h e s t a c k .
3 . I d e n t if y + a s a n o p e r a t o r
B e g in t h e p r o c e s s o f e v a lu a t in g + .
4 . g e tO p e r a n d s ( ) p o p s s ta c k
t w ic e a n d a s s ig n s 3 t o
r ig h t a n d 2 t o le f t .
o p e ran d S tac k e m p ty
5 . c o m p u t e ( ) e v a lu a t e s le f t + r ig h t
a n d r e t u r n s t h e v a lu e 5 . R e t u r n
v a lu e is p u s h e d o n t h e s t a c k .
Stacks
67
Another Example
Expression:
5 3 - 6 + 8 2 / 1 2 + - *
3
5
6
2
Execute 5 - 3
1
4
8
4
8
Execute 8 / 2
Execute 2 + 6
Execute Execute
1+2
4-3
Stacks
8
Execute
8*1
68
Stacks
69
INFIX
a+b
PREFIX
+ab
a * (b + c)
*a+bc
a*b+c
+*abc
70
Stacks
71
Stacks
72
Stacks
73
INFIX
PREFIX
a + (b * c d) / e
+a/ *bcde
+a/ *bcde
/ *bcde
e
*bcd
d
*bc
c
b
a
OPERAND
STACK
*
(
+
Stacks
OPERATOR
STACK
74
a b + c * (d / e (f + g))
Stacks
75
Exercise
a+b-(c+d*e/(fg))*(h+i)
Stacks
76
Self-Test
Stacks
77
Stack Implementations
Array based
Where is top?
How are elements added, removed?
Where is top?
How are elements added, removed?
Efficiency of operations
Stacks
78
[0]
array
3
4
stack
s.pop( )
Stacks
79
Stacks
Algorithm push(o)
if t = S.length 1 then
A new array of
size
for i 0 to t do
A[i] S[i]
SA
tt+1
S[t] o
80
Stacks
81
Incremental Strategy
Analysis
Stacks
82
Stacks
83
fro nt
C
B
L in k e d L is t
S ta c k
s.push (F)
s.pop( )
Stacks
84
elements
Stacks
85
Comparison of stack
Implementations
The code for the vector version is very similar to the
Using a single linked list allocates space for the links, but
these are necessary.
Using a single linked list also provides O(1) performance.
Stacks
86
Summary Slide 1
- Stack
Stacks
87
87
Summary Slide 2
- Recursion
Stacks
88
88
Summary Slide 3
3)
Stacks
89
89
Summary Slide 4
- Infix notation
Stacks
90
90