/* Author : Michael Robinson Program : queue.java Purpose : Queue is a data structure that allows us to place data items one after another. It uses the FIFO (First In, First Out) processing method. The first item placed in the queue is the first item to be extracted/processed. To create an object of the Queue class we use: Queue queue = new LinkedList(); To place items in the queue we use the command queue.add( "FirstQ" ); To extract items in the queue we use the command queue.poll(); if we are using an iterator such as: Iterator itq = queue.iterator(); we first need to check if the queue has more items: itq.hasNext() then we remove the data using: itqProcess.remove(); we can see the first item in the queue using: queue.peek(); For more details see : http://docs.oracle.com/javase/6/docs/api/java/util/Queue.html There is another type of queue called priority queue. Updated : October 26, 2099 */ import java.util.Iterator; import java.util.LinkedList; import java.util.Queue; public class queue { public static void printTheQueue( Queue queue, String message ) { System.out.printf( " Adding... %d = Size of Queue : %s\n", queue.size(), queue ); } public static void reqularQueue() { String message = "Adding items is the queue,"; Queue queue = new LinkedList(); System.out.printf( "We now start with an empty queue :" + " %s\n\n", queue ); queue.add( "FirstQ" ); printTheQueue( queue, message ); queue.add( "SecondQ" ); printTheQueue( queue, message ); queue.add( "ThirdQ" ); printTheQueue( queue, message ); queue.add( "FourQ" ); printTheQueue( queue, message ); System.out.println("\nFIFO order means, First In First Out"); System.out.println( "\nLet's see the top of the Queue in" + " FIFO order : " + queue.peek() ); Iterator itq = queue.iterator(); System.out.printf( "\nLet's see what is in the Queue in " + "FIFO order: %s\n", queue ); System.out.printf("\n Let's see again \t .. left in queue"); while( itq.hasNext() ) //start at the top of the queue { System.out.printf( " %s ", ( String )itq.next() ); } System.out.printf( "\n processing %s", queue.peek() ); queue.poll(); System.out.printf( "\t .. left in queue %s \n", queue ); Iterator itqProcess = queue.iterator(); //start at the top of the queue while( itqProcess.hasNext() ) { System.out.printf( " processing %s ", ( String ) itqProcess.next() ); itqProcess.remove(); System.out.printf("\t .. left in queue %s \n", queue ); } System.out.println( "\nFinal Size of Queue : " + queue.size() ); System.out.printf( "We end with an empty queue : %s\n\n", queue ); }//end public static void reqularQueue() public static void main(String[] args) { reqularQueue(); }//end public static void main(String[] args) }//end public class queue