01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package algs13;
import stdlib.*;

public class XResizingArrayStackOfStrings {
  private String[] a;   // array of items
  private int N = 0;    // number of elements on stack
  // Invariant (after the constructor):
  //   a[0]..a[N-1] are non null
  //   a[N]..a[a.length-1] are null
  
  // create an empty stack
  public XResizingArrayStackOfStrings() {
    a = new String[N];
  }

  public boolean isEmpty() { return N == 0; }
  public int size()        { return N;      }

  // resize the underlying array holding the elements
  private void resize(int capacity) {
    String[] temp = new String[capacity];
    for (int i = 0; i < N; i++)
      temp[i] = a[i];
    a = temp;
  }

  // push a new item onto the stack
  public void push(String item) {
    if (N == a.length) resize(2*a.length);
    a[N] = item;
    N++;
  }

  // delete and return the item most recently added
  public String pop() {
    if (isEmpty()) { throw new Error("Stack underflow error"); }
    N--;
    String item = a[N];
    a[N] = null;  // to avoid loitering
    if (N > 0 && N == a.length/4) resize(a.length/2); // shrink size of array if necessary
    return item;
  }



  /* *********************************************************************
   * Test routine.
   **********************************************************************/
  public static void main(String[] args) {
    Trace.drawStepsOfMethod ("resize");
    Trace.run ();
    StdIn.fromString ("to be or not to - be - - that - - - is");

    XResizingArrayStackOfStrings s = new XResizingArrayStackOfStrings();
    while (!StdIn.isEmpty()) {
      String item = StdIn.readString();
      if (!item.equals("-")) s.push(item);
      else if (!s.isEmpty()) StdOut.print(s.pop() + " ");
    }
    StdOut.println("(" + s.size() + " left on stack)");
  }
}