// adapted from Waite, Lafore "Data Structures and // Algorithms" import java.io.*; class Link { public double dData; // data item public Link next; // next link in list // ------------------------------------------------------------- public Link(double d) // constructor { dData = d; } // ------------------------------------------------------------- public void displayLink() // display this link { System.out.print(dData + " "); } // ------------------------------------------------------------- } // end class Link class FirstLastList{ private Link first; // ref to first item private Link last; // ref to last item public FirstLastList(){ // constructor first = null; // no items on list yet last = null; } public boolean isEmpty(){ // true if no links return first==null; } public void insertLast(double dd){ // insert at end of list Link newLink = new Link(dd); // make new link if( isEmpty() ) // if empty list, first = newLink; // first --> newLink else last.next = newLink; // old last --> newLink last = newLink; // newLink <-- last } // PRECONDITION: nonempty list public double deleteFirst(){ // delete first link double temp = first.dData; if(first.next == null) // if only one item last = null; // null <-- last first = first.next; // first --> old next return temp; } public void displayList() { Link current = first; // start at beginning while(current != null) // until end of list, { current.displayLink(); // print data current = current.next; // move to next link } System.out.println(""); } } // end class FirstLastList class LinkQueue{ private FirstLastList theList; public LinkQueue() // constructor { theList = new FirstLastList(); } // make a 2-ended list public boolean isEmpty() { return theList.isEmpty(); } public void insert(double j){ theList.insertLast(j); } public double remove(){ return theList.deleteFirst(); } public void displayQueue(){ System.out.print("Queue (front-->rear): "); theList.displayList(); } } // end class LinkQueue class LinkQueueApp{ public static void main(String[] args) throws IOException { LinkQueue theQueue = new LinkQueue(); theQueue.insert(20); // insert items theQueue.insert(40); theQueue.insert(60); // insert items theQueue.insert(80); theQueue.displayQueue(); // display queue theQueue.remove(); // remove items theQueue.remove(); theQueue.displayQueue(); // display queue } // end main() } // end class LinkQueueApp