University of North Carolina at Asheville

CSCI 202: Introduction to Data Structures


Lab 07: Linked-List Implementation of a Queue

[Introduction] [Instructions]


Introduction

In this lab you will again finish a partially written Java GUI application that demonstrates the basic functions of a Queue. This project is similar to Lab04 but it implements a Queue class as a linked list rather than a circular array. Many features of the linked list itself are identical to those you encountered in Lab06, where you performed a similar revision of the Stack class. But this time, since you have now acquired some sophistication in these matters, you will be asked to write the complete source file that defines your new Queue class.

Now just in case you have forgotten what a queue does, recall that its rules for insertion and removal of items are as follows:

Again, these rules are sometimes summarized by saying that a queue is a FIFO ("First In, First Out") list. In contrast, a stack is called a LIFO ("Last In, First Out") list.

As usual, the GUI test framework has already been created and tested, and source files for the relevant application classes are provided to you. Your main task is to write a complete source file named Queue.java, which defines both the member data and methods for a Queue that stores its items in a linked list. You will have to make the Queue API (Application Programmer Interface, namely the set of public Queue methods that applications can use) conform exactly to specifications given below. Otherwise, you only need to make sure that your Queue does indeed store its data in a linked list (no arrays allowed this time).

However, for this lab you will be given a complete source file Node.java that represents the individual nodes in the linked list. This is to spare you the labor of implementing the same old Node methods that you needed for Lab06. Incidentally, this source file also includes a fully working paint() method which allows applications to render nodes graphically.

Now this last point just naturally leads up to your final challenge in this lab. This time you will be asked to implement a paint() method for your Queue, which the GUI application can use to render its Queue object in the main window. However, this task should not be too difficult if you make proper use of the paint() method that is already defined for the Node class.

After you have completed the Queue class implementation as described below, you will be able to compile all the files and run the project. For this lab, the paint() method that you will implement for the Queue class must explicitly display it as a linked list. You should recall that this is the way Lab06 rendered a linked-list Stack. Thus for an empty Queue (with no nodes present), you will see only two null links labeled Front and Rear respectively:

After inserting the names "Patricia", "Howard", "Martin", and "Jennifer" in that order, the queue should appear as shown below:

If you now clicked the Remove button to remove the front node ("Patricia"), you should see

The Queue will also be provided with a peek() method that allows an application to determine the frontmost data value without removing it. You should recall that a similar method was introduced for a Stack in Lab06. If you now clicked the Peek button, you should see the following:

Finally, clicking the Print All button generates a console display of the complete queue contents in the NetBeans IDE output window, as shown below:

To enable this last feature, the Queue class will be provided with a printAll() method similar to the one you implemented for the Stack in Lab06.

When you have built this project, you should test it using similar examples to make sure that all your Queue methods are working correctly.


Instructions:

To complete this project, follow the steps listed below:

  1. Start the NetBeans IDE and mount a new filesystem named Lab07 (you will have to create this folder as part of the process). Then create a new project, also named Lab07.
  2. Download the following files and import them into your Lab07 project folder:

    QueueDisplayFrame.java DisplayPanel.java ControlPanel.java StatusPanel.java Node.java

    You have been provided with a complete set of javadoc pages fpr the classes defined in these files. For a quick overview, QueueDisplayFrame is the class that contains the main() method for this project. The only task of main() is to create a toplevel application window, which is itself defined by this class. One special feature of this class is that it includes a single Queue object as part of its member data. The DisplayPanel class represents the central portion of the application window, which will graphically display the current state of the Queue object.

    Of course the Node class defines a single node, the basic building block for a linked list. This class is fully implemented and ready to use in this project.

  3. As noted in the introduction, this time you need to write your own complete version of a file Queue.java that implements a Queue as a linked list. This class must include a default constructor (that is, a constructor with no arguments) that creates an initially empty Queue object. It must also contain the following methods, all of which are essential for the application to work correctly:

    1. public void add (String value)
    2. public String remove ()
    3. public String peek ()
    4. public boolean isEmpty ()
    5. public int getItemCount ()
    6. public void reset ()
    7. public void printAll()
    8. public void paint (java.awt.Graphics g, java.awt.Dimension winSize)

    You should now proceed to implement the bodies of the methods listed above, so that they perform the following tasks:

    1. add() must create a new Node object and insert its value parameter as the data for this node. Since this node will be the new rear node of the linked list, its link must be set to null. Then the node must actually be attached at the rear of the list, and the relevant Queue data (in particular, its reference to the rear node) must be updated. Note also that the item counter must be incremented before the method returns.

      Note: The special case of adding to an empty queue requires care, since in that case both the rear and front pointers will be affected...

    2. remove() should first make a local reference to the data in the front node, since eventually it must return this item. Then it must detach the front node from the list, and update all the relevant Queue member data. Finally, remove() must decrement the item counter and return the item it just removed.

      Note: The special case of removing the last item from a Queue is also tricky, since both the rear and front pointers must be updated.

    3. peek() simply returns a reference to the front node in the queue. If the queue is currently empty, the return value is null.
    4. isEmpty() must determine whether the Queue is currently empty and return the result.
    5. getItemCount() must return the current number of String items stored on the Queue.
    6. reset() must restore the Queue to its original state, just as it was first constructed. This means that all items should be removed so that the Queue is again empty.
    7. printAll() sets up a loop that traverses the queue from front to rear, using System.out.println() statements to print the data value stored in each node. As described in the Introduction, the values are displayed in the output window of the NetBeans IDE. Note that this method is quite similar to the one you implemented for a Stack in Lab06.
    8. paint() must render the complete Queue in the application window as shown in the Introduction, using the specified Graphics object and the specified size of the display area. The size parameter is specified as a java.awt.Dimension object, which contains two public integer fields named width and height.

      You should be able to ge a lot of help implementing this method if you start from the paint() method in the source file Stack.java, which was provided in Lab06 for the linked-list Stack. Basically, this method just sets up a loop that makes each node render itself at the appropriate starting point. (If you consult the Node class documentation or directly examine the paint() method in Node.java, you will find that it returns a Point object that represents the "tip" of its arrow to the next node. This technique makes it easy to render the entire linked list using just a few lines of code.)

      If you get bogged down anywhere, it is likely to be in the details of the Front and Rear labels and their associated lines. For this task especially,it will help if you have one branch in your paint() method handle an empty Queue (in which case there will be no chain of nodes at all), and one to handle a nonempty queue. Of course there will be some tasks common to both branches, such as drawing the Front and Rear labels themselves, but most of the code will be different in the two branches. You should develop your code in stages, compiling and testing frequently as you go along. And of course, whenever you think you are at an impasse, get help from your lab instructor.

  4. Once you have completed the implementation of the new Queue class, try to compile your Lab07 project. Of course if you encounter any compiler errors, you will have to fix them all before you can proceed...
  5. Run the project. You should now verify the application behavior as described in the Introduction. You should experiment with several sequences of inserting and removing data. Verify especially that the graphical representation of the Queue changes in the way you would expect.