// Reversing a sequence of words by using a Stack // Printing the stack import java.io.*; import java.util.*; import mystack; class reverse2 { public static void main (String[] args) throws IOException { BufferedReader stdin; stdin = new BufferedReader (new InputStreamReader (System.in)); System.out.print ("Type a sequence of words: "); StringTokenizer str = new StringTokenizer(stdin.readLine()); Stack s = new Stack(); mystack z = new mystack(); String t; while (str.hasMoreTokens()) { t = str.nextToken(); System.out.print ("Push: " + t + "\t"); s.push(t); z.print_stack(s); } while (! s.empty()) { System.out.print ("Pop: " + s.pop() + "\t"); z.print_stack(s); } } }