/* Author : Michael Robinson Program : simpleStack.java Purpose : Stack is a data structure that allows us to place data items on top of each other. It uses the LIFO (last in, first out) method of processing. The last item placed in the stack is the first item to be extracted. To create an object of the Stack class we use: Stack stackName = new Stack(); To place items in the stack we use the command stackName.push( item ); To extract items in the stack we use the command stackName.pop(); To find the location of an item in the stack we use: stackName.search( item ); To see the item that is on top of the stack we use: stackName.peek(); The Stack class inherits from several classes, e.i: Iterator iter = stack.iterator(); and many others. For more details see : http://docs.oracle.com/javase/6/docs/api/java/util/Stack.html Updated : October 26, 2099 */ import java.util.*; public class simpleStack { public static void printTheStack( Stack stack, String message ) { if( !stack.empty() ) { System.out.printf( " %s size = %d : %s\n", message, stack.size(), stack ); } }//end public static void printTheStack( Stack stack, String message ) public static void stackPop( Stack stack ) { String message = "Poping stack item ="; while ( !stack.empty() ) { //lets see what is on top System.out.printf( "\n Lets see what is on top : %s", (String)stack.peek() ); //find and remove the last element from the stack System.out.printf( "\n %s %s\n", message, (String)stack.pop() ); printTheStack( stack, "Items in the stack " ); } System.out.printf( "\nNow we have an empty stack," + " size = %d, items = %s\n", stack.size(), stack ); }//end public static void stackPop( Stack stack ) public static void fullStack( Stack stack ) { System.out.printf( "\nNow that we are playing with a " + "full stack let's see what we can do:\n\n" ); System.out.printf( " Full stack = %s of size %d\n\n", stack, stack.size() ); String item = ""; Iterator iter = stack.iterator(); while(iter.hasNext()) { item = (String)iter.next(); System.out.printf( " while loop at location %d the" + " stack contains : %s.\n", stack.search( item ), item ); } System.out.println(); //enhanced for loop processes the stack object for( String temp : stack) { System.out.printf( " enhanced for at location %d the" + " stack contains : %s.\n", + stack.search( temp ), temp ); } System.out.println(); //inline iterator new for loop in Java NO STEP section for( Iterator it = stack.iterator(); it.hasNext(); ) { item = (String)it.next(); System.out.printf( " inline iterator at location %d " + "the stack contains : %s.\n", + stack.search( item ), item ); } }//end public static void fullStack( Stack stack ) public static void stackPush( Stack stack ) { System.out.printf( "We start with an empty stack : %s\n\n", stack ); String message = "Pushing items is the stack,"; stack.add( "First" ); printTheStack( stack, message ); stack.add( "Second" ); printTheStack( stack, message ); stack.add( "Third" ); printTheStack( stack, message ); stack.add( "Four" ); printTheStack( stack, message ); }//end public static void stackPush( Stack stack ) public static void main( String arg[] ) { //creating an object called stack Stack stack = new Stack(); //call the method stackPush( stack ) stackPush( stack ); //call the method fullStack( stack ) fullStack( stack ); //call the method stackPop( stack ) stackPop( stack ); }//end public static void main( String arg[] ) }//end public class simpleStack