Tags

 

/*************************************************************************
 *  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 Reverse {
    public static void main(String[] args) {
        Stack<Integer> stack = new Stack<Integer>();
        while (!StdIn.isEmpty()) {
            stack.push(StdIn.readInt());
        }
        for (int i : stack) {
            StdOut.println(i);
        }
    }
}

 

Copyright © 2002–2010, Robert Sedgewick and Kevin Wayne. 
Last updated: Sat Feb 5 14:07:08 EST 2011.
Advertisements