Exercise 10.1.1

Using figure 10.1 as a model, illustrate the result of each operation in the sequence PUSH(S, 4), PUSH(S, 1), PUSH(S, 3), POP(S), PUSH(S, 8), and POP(S) on an initially empty stack S stored in array S[1..6].

Well, ASCII art to the rescue.

  +---+---+---+---+---+---+
  |   |   |   |   |   |   |
  +---+---+---+---+---+---+
^
S.top = 0

PUSH(S, 4)
  +---+---+---+---+---+---+
  | 4 |   |   |   |   |   |
  +---+---+---+---+---+---+
    ^
    S.top = 1

PUSH(S, 1)
  +---+---+---+---+---+---+
  | 4 | 1 |   |   |   |   |
  +---+---+---+---+---+---+
        ^
        S.top = 2

PUSH(S, 3)
  +---+---+---+---+---+---+
  | 4 | 1 | 3 |   |   |   |
  +---+---+---+---+---+---+
            ^
            S.top = 3

POP(S)
  +---+---+---+---+---+---+
  | 4 | 1 | 3 |   |   |   |
  +---+---+---+---+---+---+
        ^
        S.top = 2

PUSH(S, 8)
  +---+---+---+---+---+---+
  | 4 | 1 | 8 |   |   |   |
  +---+---+---+---+---+---+
            ^
            S.top = 3

POP(S)
  +---+---+---+---+---+---+
  | 4 | 1 | 8 |   |   |   |
  +---+---+---+---+---+---+
        ^
        S.top = 2