A Queue Implemented by a Linked List
A Queue Implemented by a Linked List
Here’s a similar example of an ADT implemented with a linked list. Listing 5.5 shows
Abstract Data Types 207
LISTING 5.5 The linkQueue.java Program // linkQueue.java
// demonstrates queue implemented as double-ended list // to run this program: C>java LinkQueueApp //////////////////////////////////////////////////////////////// class Link
{ public long dData; // data item public Link next; // next link in list
// ------------------------------------------------------------- public Link(long 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(long 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 }
// -------------------------------------------------------------
208 CHAPTER 5 Linked Lists
LISTING 5.5 Continued public long deleteFirst() // delete first link
{ // (assumes non-empty list) long 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() // true if queue is empty { return theList.isEmpty(); } //-------------------------------------------------------------- public void insert(long j) // insert, rear of queue { theList.insertLast(j); } //-------------------------------------------------------------- public long remove() // remove, front of queue { return theList.deleteFirst(); } //-------------------------------------------------------------- public void displayQueue() { System.out.print(“Queue (front-->rear): “);
Abstract Data Types 209
LISTING 5.5 Continued theList.displayList();
} //-------------------------------------------------------------- } // end class LinkQueue //////////////////////////////////////////////////////////////// class LinkQueueApp
{ public static void main(String[] args)
{ LinkQueue theQueue = new LinkQueue(); theQueue.insert(20); // insert items theQueue.insert(40);
theQueue.displayQueue(); // display queue theQueue.insert(60); // insert items
theQueue.insert(80); theQueue.displayQueue(); // display queue theQueue.remove(); // remove items
theQueue.remove(); theQueue.displayQueue(); // display queue
} // end main() ////////////////////////////////////////////////////////////////
The program creates a queue, inserts two items, inserts two more items, and removes two items; following each of these operations the queue is displayed. Here’s the output:
Queue (front-->rear): 20 40 Queue (front-->rear): 20 40 60 80 Queue (front-->rear): 60 80
Here the methods insert() and remove() in the LinkQueue class are implemented by the insertLast() and deleteFirst() methods of the FirstLastList class. We’ve substi- tuted a linked list for the array used to implement the queue in the queue.java program (Listing 4.4) of Chapter 4.
The linkStack.java and linkQueue.java programs emphasize that stacks and queues
210 CHAPTER 5 Linked Lists
the push() and pop() operations and how they’re used; it’s not the underlying mechanism used to implement these operations.
When would you use a linked list as opposed to an array as the implementation of a stack or queue? One consideration is how accurately you can predict the amount of data the stack or queue will need to hold. If this isn’t clear, the linked list gives you more flexibility than an array. Both are fast, so speed is probably not a major consideration.