Stack 7 January 2019 OSU CSE 1 Stack The Stack component family - - PowerPoint PPT Presentation

stack
SMART_READER_LITE
LIVE PREVIEW

Stack 7 January 2019 OSU CSE 1 Stack The Stack component family - - PowerPoint PPT Presentation

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


slide-1
SLIDE 1

Stack

7 January 2019 OSU CSE 1

slide-2
SLIDE 2

Stack

  • The Stack component family allows you

to manipulate strings of entries of any (arbitrary) type in LIFO (last-in-first-out)

  • rder

– 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

  • rder in the string when it is written down

7 January 2019 OSU CSE 2

slide-3
SLIDE 3

Interfaces and Classes

7 January 2019 OSU CSE 3

Stack Stack1L Stack2 implements implements StackKernel extends Standard extends

slide-4
SLIDE 4

Interfaces and Classes

7 January 2019 OSU CSE 4

Stack Stack1L Stack2 implements implements StackKernel extends Standard extends Standard has contracts for three methods: clear newInstance transferFrom

slide-5
SLIDE 5

Interfaces and Classes

7 January 2019 OSU CSE 5

Stack Stack1L Stack2 implements implements StackKernel extends Standard extends StackKernel has contracts for three methods: push pop length

slide-6
SLIDE 6

Interfaces and Classes

7 January 2019 OSU CSE 6

Stack Stack1L Stack2 implements implements StackKernel extends Standard extends Stack has a contract for three

  • ther methods:

top replaceTop flip

slide-7
SLIDE 7

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

7 January 2019 OSU CSE 7

slide-8
SLIDE 8

No-argument Constructor

  • Ensures:

this = < >

7 January 2019 OSU CSE 8

slide-9
SLIDE 9

Example

7 January 2019 OSU CSE 9

Code State

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

slide-10
SLIDE 10

Example

7 January 2019 OSU CSE 10

Code State

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

slide-11
SLIDE 11

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

slide-12
SLIDE 12

Example

7 January 2019 OSU CSE 12

Code State

si = < 3, 70 > k = 49 si.push(k);

slide-13
SLIDE 13

Example

7 January 2019 OSU CSE 13

Code State

si = < 3, 70 > k = 49 si.push(k); si = < 49, 3, 70 > k = 49

slide-14
SLIDE 14

Example

7 January 2019 OSU CSE 14

Code State

si = < 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.

slide-15
SLIDE 15

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

slide-16
SLIDE 16

Example

7 January 2019 OSU CSE 16

Code State

si = < 49, 3, 70 > z = –584 z = si.pop();

slide-17
SLIDE 17

Example

7 January 2019 OSU CSE 17

Code State

si = < 49, 3, 70 > z = –584 z = si.pop(); si = < 3, 70 > z = 49

slide-18
SLIDE 18

length

int length()

  • Reports the length of this.
  • Ensures:

length = |this|

7 January 2019 OSU CSE 18

slide-19
SLIDE 19

top

T top()

  • Returns the entry at the the top (left end)
  • f this.
  • Aliases: reference returned by top
  • Requires:

this /= < >

  • Ensures:

<top> is prefix of this

7 January 2019 OSU CSE 19

slide-20
SLIDE 20

Example

7 January 2019 OSU CSE 20

Code State

si = < 49, 3, 70 > k = –58 k = si.top();

slide-21
SLIDE 21

Example

7 January 2019 OSU CSE 21

Code State

si = < 49, 3, 70 > k = –58 k = si.top(); si = < 49, 3, 70 > k = 49

slide-22
SLIDE 22

Example

7 January 2019 OSU CSE 22

Code State

si = < 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.

slide-23
SLIDE 23

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 and this = <x> * #this[1, |#this|)

7 January 2019 OSU CSE 23

slide-24
SLIDE 24

Example

7 January 2019 OSU CSE 24

Code State

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

k = si.replaceTop(j);

slide-25
SLIDE 25

Example

7 January 2019 OSU CSE 25

Code State

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

k = si.replaceTop(j);

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

slide-26
SLIDE 26

Example

7 January 2019 OSU CSE 26

Code State

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

k = si.replaceTop(j);

si = < 16, 70 > k = 49 j = 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.

slide-27
SLIDE 27

Another Example

7 January 2019 OSU CSE 27

Code State

si = < 49, 70 > j = 16

j = si.replaceTop(j);

slide-28
SLIDE 28

Another Example

7 January 2019 OSU CSE 28

Code State

si = < 49, 70 > j = 16

j = si.replaceTop(j);

si = < 16, 70 > j = 49

slide-29
SLIDE 29

Another Example

7 January 2019 OSU CSE 29

Code State

si = < 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.

slide-30
SLIDE 30

flip

void flip()

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

this = rev(#this)

7 January 2019 OSU CSE 30

slide-31
SLIDE 31

Example

7 January 2019 OSU CSE 31

Code State

s1 = < 18, 6, 74 > s1.flip();

slide-32
SLIDE 32

Example

7 January 2019 OSU CSE 32

Code State

s1 = < 18, 6, 74 > s1.flip(); s1 = < 74, 6, 18 >

slide-33
SLIDE 33

Resources

  • OSU CSE Components API: Stack

– http://cse.osu.edu/software/common/doc/

7 January 2019 OSU CSE 33