public static void main(String[] args) {
    ResizingArrayStack<String> strStack = new ResizingArrayStack<String>();
    while (!StdIn.isEmpty()) {
      String item = StdIn.readString();
      if (!item.equals("-")) strStack.push(item);
      else if (!strStack.isEmpty()) StdOut.print(strStack.pop() + " ");
    }

    for (String s : strStack) StdOut.println(s);

    StdOut.println("(" + strStack.size() + " left on stack)");
  }
Example #2
0
  public static void main(String[] args) throws EmptyStackException {
    Scanner input = new Scanner(new BufferedInputStream(System.in));
    PrintWriter output = new PrintWriter(new OutputStreamWriter(System.out), true);

    ResizingArrayStack<String> stack = new ResizingArrayStack<String>();
    while (input.hasNext()) {
      String s = input.next();
      if (!s.equals("-")) stack.push(s);
      else if (!stack.isEmpty()) output.println(stack.pop());
    }
    output.println("stack contains " + stack.size() + " items");
    for (String s : stack) output.println(s);
  }