stack
play

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


  1. Stack 7 January 2019 OSU CSE 1

  2. 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

  3. Interfaces and Classes Standard extends StackKernel extends Stack implements implements Stack1L Stack2 7 January 2019 OSU CSE 3

  4. Interfaces and Classes Standard extends Standard has contracts StackKernel for three methods: clear newInstance extends transferFrom Stack implements implements Stack1L Stack2 7 January 2019 OSU CSE 4

  5. Interfaces and Classes Standard extends StackKernel extends StackKernel has contracts for three Stack methods: implements implements push pop Stack1L Stack2 length 7 January 2019 OSU CSE 5

  6. Interfaces and Classes Stack Standard has a contract for three other methods: top extends replaceTop StackKernel flip extends Stack implements implements Stack1L Stack2 7 January 2019 OSU CSE 6

  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

  8. No-argument Constructor • Ensures: this = < > 7 January 2019 OSU CSE 8

  9. Example Code State Stack<Integer> si = new Stack1L<>(); 7 January 2019 OSU CSE 9

  10. Example Code State Stack<Integer> si = new Stack1L<>(); si = < > 7 January 2019 OSU CSE 10

  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

  12. Example Code State si = < 3, 70 > k = 49 si.push(k); 7 January 2019 OSU CSE 12

  13. Example Code State si = < 3, 70 > k = 49 si.push(k); si = < 49, 3, 70 > k = 49 7 January 2019 OSU CSE 13

  14. Example Note the alias created Code State here, which you cannot see in the tracing table; si = < 3, 70 > you should be able to k = 49 draw the appropriate diagram showing it. si.push(k); si = < 49, 3, 70 > k = 49 7 January 2019 OSU CSE 14

  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

  16. Example Code State si = < 49, 3, 70 > z = –584 z = si.pop(); 7 January 2019 OSU CSE 16

  17. Example Code State si = < 49, 3, 70 > z = –584 z = si.pop(); si = < 3, 70 > z = 49 7 January 2019 OSU CSE 17

  18. length int length() • Reports the length of this . • Ensures: length = | this | 7 January 2019 OSU CSE 18

  19. 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

  20. Example Code State si = < 49, 3, 70 > k = –58 k = si.top(); 7 January 2019 OSU CSE 20

  21. Example Code State si = < 49, 3, 70 > k = –58 k = si.top(); si = < 49, 3, 70 > k = 49 7 January 2019 OSU CSE 21

  22. Example Note the alias created Code State here, which you cannot see in the tracing table; si = < 49, 3, 70 > you should be able to k = –58 draw the appropriate diagram showing it. k = si.top(); si = < 49, 3, 70 > k = 49 7 January 2019 OSU CSE 22

  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

  24. Example Code State si = < 49, 70 > k = –58 j = 16 k = si.replaceTop(j); 7 January 2019 OSU CSE 24

  25. Example Code State si = < 49, 70 > k = –58 j = 16 k = si.replaceTop(j); si = < 16, 70 > k = 49 j = 16 7 January 2019 OSU CSE 25

  26. Example Note the alias created Code State here, which you cannot see in the tracing table; si = < 49, 70 > you should be able to k = –58 draw the appropriate j = 16 diagram showing it. k = si.replaceTop(j); si = < 16, 70 > k = 49 j = 16 7 January 2019 OSU CSE 26

  27. Another Example Code State si = < 49, 70 > j = 16 j = si.replaceTop(j); 7 January 2019 OSU CSE 27

  28. Another Example Code State si = < 49, 70 > j = 16 j = si.replaceTop(j); si = < 16, 70 > j = 49 7 January 2019 OSU CSE 28

  29. Another Example This use of the method Code State avoids creating an alias: it swaps j with the entry si = < 49, 70 > previously at the top. j = 16 j = si.replaceTop(j); si = < 16, 70 > j = 49 7 January 2019 OSU CSE 29

  30. flip void flip() • Reverses (“flips”) this . • Updates: this • Ensures: this = rev (# this ) 7 January 2019 OSU CSE 30

  31. Example Code State s1 = < 18, 6, 74 > s1.flip(); 7 January 2019 OSU CSE 31

  32. Example Code State s1 = < 18, 6, 74 > s1.flip(); s1 = < 74, 6, 18 > 7 January 2019 OSU CSE 32

  33. Resources • OSU CSE Components API: Stack – http://cse.osu.edu/software/common/doc/ 7 January 2019 OSU CSE 33

Recommend


More recommend