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
package algs13;
import stdlib.*;
/* ***********************************************************************
 *  Compilation:  javac Reverse.java
 *  Execution:    java Reverse
 *  Dependencies: Stack.java StdOut.java StdIn.java
 *
 *  Read a sequence of integers from standard input and print them
 *  in reverse order.
 *
 *  % java  java Reverse
 *  1 2 3 4 5
 *  5
 *  4
 *  3
 *  2
 *  1

 *************************************************************************/

public class XReverseStdIn {
  public static void main(String[] args) {
    //StdIn.fromString ("1 2 3 4 5");
    StdIn.fromString ("10 20 30 40 50");

    Stack<Integer> stack = new Stack<>();
    while (!StdIn.isEmpty()) {
      int i = StdIn.readInt();
      stack.push(i);
    }
    while (! stack.isEmpty()) {
      int i = stack.pop ();
      StdOut.println(i);
    }
  }
}