Stack

Stack
9 January 2015
OSU CSE
1
Stack
• The Stack component family allows you
to manipulate strings of entries of any
(arbitrary) type in LIFO (last-in-first-out)
order
– A kind of “dual” to Queue
– Remember, "first" and "last" here refer to the
temporal order in which entries are put into
the string and taken out of it, not about the
order in the string when it is written down
9 January 2015
OSU CSE
2
Interfaces and Classes
Standard
extends
StackKernel
extends
Stack
implements
implements
Stack1L
9 January 2015
Stack2
OSU CSE
3
Interfaces and Classes
Standard
extends
Standard has contracts
for three methods: StackKernel
clear
newInstance
extends
transferFrom
Stack
implements
Stack1L
9 January 2015
implements
Stack2
OSU CSE
4
Interfaces and Classes
Standard
extends
StackKernel
StackKernel has
contracts for three
methods:
implements
push
pop
Stack1L
length
9 January 2015
extends
Stack
implements
Stack2
OSU CSE
5
Interfaces and Classes
Stack
has a contract for threeStandard
other methods:
top
extends
replaceTop
StackKernel
flip
extends
Stack
implements
implements
Stack1L
9 January 2015
Stack2
OSU CSE
6
Mathematical Model
• The value of a Stack variable is modeled
as a string of entries of type T
• Formally:
type Stack is modeled by
string of T
9 January 2015
OSU CSE
7
No-argument Constructor
• Ensures:
this = < >
9 January 2015
OSU CSE
8
Example
Code
State
Stack<Integer> si =
new Stack1L<>();
9 January 2015
OSU CSE
9
Example
Code
State
Stack<Integer> si =
new Stack1L<>();
si = < >
9 January 2015
OSU CSE
10
push
void push(T x)
• Adds x at the top (left end) of this.
• Aliases: reference x
• Updates: this
• Ensures:
this = <x> * #this
9 January 2015
OSU CSE
11
Example
Code
State
si = < 3, 70 >
k = 49
si.push(k);
9 January 2015
OSU CSE
12
Example
Code
State
si = < 3, 70 >
k = 49
si.push(k);
si = < 49, 3, 70 >
k = 49
9 January 2015
OSU CSE
13
Example
Note the alias
created
Code
here, which you cannot
see in the tracing table;
you should be able to
draw the appropriate
diagram showing it.
si.push(k);
State
si = < 3, 70 >
k = 49
si = < 49, 3, 70 >
k = 49
9 January 2015
OSU CSE
14
pop
T pop()
• Removes and returns the entry at the top
(left end) of this.
• Updates: this
• Requires:
this /= < >
• Ensures:
#this = <pop> * this
9 January 2015
OSU CSE
15
Example
Code
State
si = < 49, 3, 70 >
z = –584
z = si.pop();
9 January 2015
OSU CSE
16
Example
Code
State
si = < 49, 3, 70 >
z = –584
z = si.pop();
si = < 3, 70 >
z = 49
9 January 2015
OSU CSE
17
length
int length()
• Reports the length of this.
• Ensures:
length = |this|
9 January 2015
OSU CSE
18
top
T top()
• Returns the entry at the the top (left end)
of this.
• Aliases: reference returned by top
• Requires:
this /= < >
• Ensures:
<top> is prefix of this
9 January 2015
OSU CSE
19
Example
Code
State
si = < 49, 3, 70 >
k = –58
k = si.top();
9 January 2015
OSU CSE
20
Example
Code
State
si = < 49, 3, 70 >
k = –58
k = si.top();
si = < 49, 3, 70 >
k = 49
9 January 2015
OSU CSE
21
Example
Note the alias
created
Code
here, which you cannot
see in the tracing table;
you should be able to
draw the appropriate
diagram showing it.
k = si.top();
State
si = < 49, 3, 70 >
k = –58
si = < 49, 3, 70 >
k = 49
9 January 2015
OSU CSE
22
replaceTop
T
•
•
•
replaceTop(T x)
Replaces the top of this with x, and returns the old top.
Aliases: reference x
Updates: this
• Requires:
this /= < >
• Ensures:
<replaceTop> is prefix of #this
this = <x> * #this[1, |#this|)
9 January 2015
OSU CSE
and
23
Example
Code
State
si = < 49, 70 >
k = –58
j = 16
k = si.replaceTop(j);
9 January 2015
OSU CSE
24
Example
Code
State
si = < 49, 70 >
k = –58
j = 16
k = si.replaceTop(j);
si = < 16, 70 >
k = 49
j = 16
9 January 2015
OSU CSE
25
Example
Note theCode
alias created
here, which you cannot
see in the tracing table;
you should be able to
draw the appropriate
diagram showing it.
k = si.replaceTop(j);
State
si = < 49, 70 >
k = –58
j = 16
si = < 16, 70 >
k = 49
j = 16
9 January 2015
OSU CSE
26
Another Example
Code
State
si = < 49, 70 >
j = 16
j = si.replaceTop(j);
9 January 2015
OSU CSE
27
Another Example
Code
State
si = < 49, 70 >
j = 16
j = si.replaceTop(j);
si = < 16, 70 >
j = 49
9 January 2015
OSU CSE
28
Another Example
This use of
the method
Code
avoids creating an alias: it
swaps j with the entry
previously at the top.
State
si = < 49, 70 >
j = 16
j = si.replaceTop(j);
si = < 16, 70 >
j = 49
9 January 2015
OSU CSE
29
flip
void flip()
• Reverses (“flips”) this.
• Updates: this
• Ensures:
this = rev(#this)
9 January 2015
OSU CSE
30
Example
Code
State
s1 = < 18, 6, 74 >
s1.flip();
9 January 2015
OSU CSE
31
Example
Code
State
s1 = < 18, 6, 74 >
s1.flip();
s1 = < 74, 6, 18 >
9 January 2015
OSU CSE
32
Resources
• OSU CSE Components API: Stack
– http://cse.osu.edu/software/common/doc/
9 January 2015
OSU CSE
33