cse 2221 - stackweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/32.stack.pdfstack • the...

33
Stack 7 January 2019 OSU CSE 1

Upload: others

Post on 14-Jul-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSE 2221 - Stackweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/32.Stack.pdfStack • The . Stack. component family allows you to manipulate strings of entries of any (arbitrary)

Stack

7 January 2019 OSU CSE 1

Page 2: CSE 2221 - Stackweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/32.Stack.pdfStack • The . Stack. component family allows you to manipulate strings of entries of any (arbitrary)

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

Page 3: CSE 2221 - Stackweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/32.Stack.pdfStack • The . Stack. component family allows you to manipulate strings of entries of any (arbitrary)

Interfaces and Classes

7 January 2019 OSU CSE 3

Stack

Stack1L Stack2

implements implements

StackKernel

extends

Standard

extends

Page 4: CSE 2221 - Stackweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/32.Stack.pdfStack • The . Stack. component family allows you to manipulate strings of entries of any (arbitrary)

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

Page 5: CSE 2221 - Stackweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/32.Stack.pdfStack • The . Stack. component family allows you to manipulate strings of entries of any (arbitrary)

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

Page 6: CSE 2221 - Stackweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/32.Stack.pdfStack • The . Stack. component family allows you to manipulate strings of entries of any (arbitrary)

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

Page 7: CSE 2221 - Stackweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/32.Stack.pdfStack • The . Stack. component family allows you to manipulate strings of entries of any (arbitrary)

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

Page 8: CSE 2221 - Stackweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/32.Stack.pdfStack • The . Stack. component family allows you to manipulate strings of entries of any (arbitrary)

No-argument Constructor

• Ensures:this = < >

7 January 2019 OSU CSE 8

Page 9: CSE 2221 - Stackweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/32.Stack.pdfStack • The . Stack. component family allows you to manipulate strings of entries of any (arbitrary)

Example

7 January 2019 OSU CSE 9

Code State

Stack<Integer> si =new Stack1L<>();

Page 10: CSE 2221 - Stackweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/32.Stack.pdfStack • The . Stack. component family allows you to manipulate strings of entries of any (arbitrary)

Example

7 January 2019 OSU CSE 10

Code State

Stack<Integer> si =new Stack1L<>();

si = < >

Page 11: CSE 2221 - Stackweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/32.Stack.pdfStack • The . Stack. component family allows you to manipulate strings of entries of any (arbitrary)

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

Page 12: CSE 2221 - Stackweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/32.Stack.pdfStack • The . Stack. component family allows you to manipulate strings of entries of any (arbitrary)

Example

7 January 2019 OSU CSE 12

Code Statesi = < 3, 70 >k = 49

si.push(k);

Page 13: CSE 2221 - Stackweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/32.Stack.pdfStack • The . Stack. component family allows you to manipulate strings of entries of any (arbitrary)

Example

7 January 2019 OSU CSE 13

Code Statesi = < 3, 70 >k = 49

si.push(k);

si = < 49, 3, 70 >k = 49

Page 14: CSE 2221 - Stackweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/32.Stack.pdfStack • The . Stack. component family allows you to manipulate strings of entries of any (arbitrary)

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.

Page 15: CSE 2221 - Stackweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/32.Stack.pdfStack • The . Stack. component family allows you to manipulate strings of entries of any (arbitrary)

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

Page 16: CSE 2221 - Stackweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/32.Stack.pdfStack • The . Stack. component family allows you to manipulate strings of entries of any (arbitrary)

Example

7 January 2019 OSU CSE 16

Code Statesi = < 49, 3, 70 >z = –584

z = si.pop();

Page 17: CSE 2221 - Stackweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/32.Stack.pdfStack • The . Stack. component family allows you to manipulate strings of entries of any (arbitrary)

Example

7 January 2019 OSU CSE 17

Code Statesi = < 49, 3, 70 >z = –584

z = si.pop();

si = < 3, 70 >z = 49

Page 18: CSE 2221 - Stackweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/32.Stack.pdfStack • The . Stack. component family allows you to manipulate strings of entries of any (arbitrary)

length

int length()

• Reports the length of this.• Ensures:length = |this|

7 January 2019 OSU CSE 18

Page 19: CSE 2221 - Stackweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/32.Stack.pdfStack • The . Stack. component family allows you to manipulate strings of entries of any (arbitrary)

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

Page 20: CSE 2221 - Stackweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/32.Stack.pdfStack • The . Stack. component family allows you to manipulate strings of entries of any (arbitrary)

Example

7 January 2019 OSU CSE 20

Code Statesi = < 49, 3, 70 >k = –58

k = si.top();

Page 21: CSE 2221 - Stackweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/32.Stack.pdfStack • The . Stack. component family allows you to manipulate strings of entries of any (arbitrary)

Example

7 January 2019 OSU CSE 21

Code Statesi = < 49, 3, 70 >k = –58

k = si.top();

si = < 49, 3, 70 >k = 49

Page 22: CSE 2221 - Stackweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/32.Stack.pdfStack • The . Stack. component family allows you to manipulate strings of entries of any (arbitrary)

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.

Page 23: CSE 2221 - Stackweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/32.Stack.pdfStack • The . Stack. component family allows you to manipulate strings of entries of any (arbitrary)

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

Page 24: CSE 2221 - Stackweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/32.Stack.pdfStack • The . Stack. component family allows you to manipulate strings of entries of any (arbitrary)

Example

7 January 2019 OSU CSE 24

Code Statesi = < 49, 70 >k = –58j = 16

k = si.replaceTop(j);

Page 25: CSE 2221 - Stackweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/32.Stack.pdfStack • The . Stack. component family allows you to manipulate strings of entries of any (arbitrary)

Example

7 January 2019 OSU CSE 25

Code Statesi = < 49, 70 >k = –58j = 16

k = si.replaceTop(j);

si = < 16, 70 >k = 49j = 16

Page 26: CSE 2221 - Stackweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/32.Stack.pdfStack • The . Stack. component family allows you to manipulate strings of entries of any (arbitrary)

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.

Page 27: CSE 2221 - Stackweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/32.Stack.pdfStack • The . Stack. component family allows you to manipulate strings of entries of any (arbitrary)

Another Example

7 January 2019 OSU CSE 27

Code Statesi = < 49, 70 >j = 16

j = si.replaceTop(j);

Page 28: CSE 2221 - Stackweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/32.Stack.pdfStack • The . Stack. component family allows you to manipulate strings of entries of any (arbitrary)

Another Example

7 January 2019 OSU CSE 28

Code Statesi = < 49, 70 >j = 16

j = si.replaceTop(j);

si = < 16, 70 >j = 49

Page 29: CSE 2221 - Stackweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/32.Stack.pdfStack • The . Stack. component family allows you to manipulate strings of entries of any (arbitrary)

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.

Page 30: CSE 2221 - Stackweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/32.Stack.pdfStack • The . Stack. component family allows you to manipulate strings of entries of any (arbitrary)

flip

void flip()

• Reverses (“flips”) this.• Updates: this• Ensures:this = rev(#this)

7 January 2019 OSU CSE 30

Page 31: CSE 2221 - Stackweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/32.Stack.pdfStack • The . Stack. component family allows you to manipulate strings of entries of any (arbitrary)

Example

7 January 2019 OSU CSE 31

Code State

s1 = < 18, 6, 74 >

s1.flip();

Page 32: CSE 2221 - Stackweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/32.Stack.pdfStack • The . Stack. component family allows you to manipulate strings of entries of any (arbitrary)

Example

7 January 2019 OSU CSE 32

Code State

s1 = < 18, 6, 74 >

s1.flip();

s1 = < 74, 6, 18 >

Page 33: CSE 2221 - Stackweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/32.Stack.pdfStack • The . Stack. component family allows you to manipulate strings of entries of any (arbitrary)

Resources

• OSU CSE Components API: Stack– http://cse.osu.edu/software/common/doc/

7 January 2019 OSU CSE 33