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
63
64
65
66
67
68
69
70
71
72
package algs13;
import stdlib.*;
/* ***********************************************************************
 *  Compilation:  javac ZLinkedStackOfStrings.java
 *  Execution:    java ZLinkedStackOfStrings
 *
 *  A stack of strings, implemented using a linked list.
 *
 *  % more tobe.txt
 *  to be or not to - be - - that - - - is
 *
 *  % java ZLinkedStackOfStrings < tobe.txt
 *  to be not that or be
 *
 *************************************************************************/

public class XStackOfStrings {
  private int N;          // size of the stack
  private Node first;     // top of stack

  // helper Node class
  private static class Node {
    public Node() { }
    public String item;
    public Node next;
  }

  // is the stack empty?
  public boolean isEmpty() { return first == null; }

  // number of elements on the stack
  public int size() { return N; }


  // add an element to the stack
  public void push(String item) {
    Node oldfirst = first;
    first = new Node();
    first.item = item;
    first.next = oldfirst;
    N++;
  }

  // delete and return the most recently added element
  public String pop() {
    if (isEmpty()) throw new Error("Stack underflow");
    String item = first.item;      // save item to return
    Node oldfirst = first;
    first = first.next;            // delete first node
    N--;
    return item;                   // return the saved item
  }

  // test client
  public static void main(String[] args) {
//    Trace.run ();
//    Trace.drawStepsOfMethod ("push");
//    Trace.drawStepsOfMethod ("pop");
    StdIn.fromString ("to be or not to - be - - that - - - is");

    XStackOfStrings s = new XStackOfStrings();
    while (!StdIn.isEmpty()) {
      String item = StdIn.readString();
      Trace.draw ();
      if (!item.equals("-")) s.push(item);
      else if (s.isEmpty())  StdOut.println("BAD INPUT");
      else                   StdOut.print(s.pop() + " ");
    }
  }


}