Upload
others
View
3
Download
0
Embed Size (px)
Citation preview
Stack
7 January 2019 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
7 January 2019 OSU CSE 2
Interfaces and Classes
7 January 2019 OSU CSE 3
Stack
Stack1L Stack2
implements implements
StackKernel
extends
Standard
extends
Interfaces and Classes
7 January 2019 OSU CSE 4
Stack
Stack1L Stack2
implements implements
StackKernel
extends
Standard
extendsStandard has contracts
for three methods:clear
newInstancetransferFrom
Interfaces and Classes
7 January 2019 OSU CSE 5
Stack
Stack1L Stack2
implements implements
StackKernel
extends
Standard
extends
StackKernel has contracts for three
methods:pushpop
length
Interfaces and Classes
7 January 2019 OSU CSE 6
Stack
Stack1L Stack2
implements implements
StackKernel
extends
Standard
extends
Stackhas a contract for three
other methods:top
replaceTopflip
Mathematical Model
• The value of a Stack variable is modeled as a string of entries of type T
• Formally:type Stack is modeled bystring of T
7 January 2019 OSU CSE 7
No-argument Constructor
• Ensures:this = < >
7 January 2019 OSU CSE 8
Example
7 January 2019 OSU CSE 9
Code State
Stack<Integer> si =new Stack1L<>();
Example
7 January 2019 OSU CSE 10
Code State
Stack<Integer> si =new Stack1L<>();
si = < >
push
void push(T x)
• Adds x at the top (left end) of this.• Aliases: reference x• Updates: this• Ensures:this = <x> * #this
7 January 2019 OSU CSE 11
Example
7 January 2019 OSU CSE 12
Code Statesi = < 3, 70 >k = 49
si.push(k);
Example
7 January 2019 OSU CSE 13
Code Statesi = < 3, 70 >k = 49
si.push(k);
si = < 49, 3, 70 >k = 49
Example
7 January 2019 OSU CSE 14
Code Statesi = < 3, 70 >k = 49
si.push(k);
si = < 49, 3, 70 >k = 49
Note the alias created here, which you cannot see in the tracing table; you should be able to draw the appropriate diagram showing it.
pop
T pop()
• Removes and returns the entry at the top (left end) of this.
• Updates: this• Requires:this /= < >
• Ensures:#this = <pop> * this
7 January 2019 OSU CSE 15
Example
7 January 2019 OSU CSE 16
Code Statesi = < 49, 3, 70 >z = –584
z = si.pop();
Example
7 January 2019 OSU CSE 17
Code Statesi = < 49, 3, 70 >z = –584
z = si.pop();
si = < 3, 70 >z = 49
length
int length()
• Reports the length of this.• Ensures:length = |this|
7 January 2019 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
7 January 2019 OSU CSE 19
Example
7 January 2019 OSU CSE 20
Code Statesi = < 49, 3, 70 >k = –58
k = si.top();
Example
7 January 2019 OSU CSE 21
Code Statesi = < 49, 3, 70 >k = –58
k = si.top();
si = < 49, 3, 70 >k = 49
Example
7 January 2019 OSU CSE 22
Code Statesi = < 49, 3, 70 >k = –58
k = si.top();
si = < 49, 3, 70 >k = 49
Note the alias created here, which you cannot see in the tracing table; you should be able to draw the appropriate diagram showing it.
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 andthis = <x> * #this[1, |#this|)
7 January 2019 OSU CSE 23
Example
7 January 2019 OSU CSE 24
Code Statesi = < 49, 70 >k = –58j = 16
k = si.replaceTop(j);
Example
7 January 2019 OSU CSE 25
Code Statesi = < 49, 70 >k = –58j = 16
k = si.replaceTop(j);
si = < 16, 70 >k = 49j = 16
Example
7 January 2019 OSU CSE 26
Code Statesi = < 49, 70 >k = –58j = 16
k = si.replaceTop(j);
si = < 16, 70 >k = 49j = 16
Note the alias created here, which you cannot see in the tracing table; you should be able to draw the appropriate diagram showing it.
Another Example
7 January 2019 OSU CSE 27
Code Statesi = < 49, 70 >j = 16
j = si.replaceTop(j);
Another Example
7 January 2019 OSU CSE 28
Code Statesi = < 49, 70 >j = 16
j = si.replaceTop(j);
si = < 16, 70 >j = 49
Another Example
7 January 2019 OSU CSE 29
Code Statesi = < 49, 70 >j = 16
j = si.replaceTop(j);
si = < 16, 70 >j = 49
This use of the method avoids creating an alias: it
swaps j with the entry previously at the top.
flip
void flip()
• Reverses (“flips”) this.• Updates: this• Ensures:this = rev(#this)
7 January 2019 OSU CSE 30
Example
7 January 2019 OSU CSE 31
Code State
s1 = < 18, 6, 74 >
s1.flip();
Example
7 January 2019 OSU CSE 32
Code State
s1 = < 18, 6, 74 >
s1.flip();
s1 = < 74, 6, 18 >
Resources
• OSU CSE Components API: Stack– http://cse.osu.edu/software/common/doc/
7 January 2019 OSU CSE 33