University of North Carolina at Asheville

CSCI 202: Introduction to Data Structures


Lab 08: Linked-List Implementation of a Queue

[Introduction] [Instructions] [What To Submit]


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 Lab07, where you performed a similar revision of the Stack 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 complete a partially written source file Queue.java, which defines the instance variables and methods for a Queue class that stores its data values in a singly linked list.

For this lab you will again be given a complete source file Node.java that represents the individual nodes in the linked list. The Node class definition includes a fully working paint() method, which allows applications to render nodes graphically.

After you have completed the Queue class implementation as described below, you will be able to compile all the files and run the project. As in Lab07, the GUI framework will explicitly display the Queue class as a linked list. For an empty Queue, with no nodes present, you will see only two null links labeled Front and Rear respectively:

If you now inserted the names "Patricia", "Howard", "Martin", and "Jennifer" in that order, the queue would 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 Lab07. If you now clicked the Peek button, you should see the following:

Finally, clicking the Print All button raises a dialog box that lists the complete queue contents, from front to rear, 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 Lab07.

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. Login and enter the directory csci/202/labs. If this directory does not already exist, create it now.
  2. Launch the NetBeans IDE and create a new project named Lab08. Define the project to be a General Java Application, and accept all the default settings so that NetBeans will automatically write a class lab08.Main for you. You should find the source for this class in Lab08/src/lab08.
  3. Download the following files and import them into your Lab08/src/lab08 project folder, which will automatically include them all in your Lab08 project:

    DisplayFrame.java DisplayPanel.java InfoDialog.java ErrorDialog.java Queue.java Node.java Paintable.java

  4. Open the file lab08.Main and add the following lines of code into its main() method:
    	DisplayFrame frame = new DisplayFrame();
    	frame.setVisible(true);
    	frame.setInitialFocus();
    
  5. Once you have assembled all these files, you should immediately be able to generate a set of Javadoc pages for the classes they define. But for a quick overview, the only task of the main() method in lab08.Main is to create a DisplayFrame, which is the toplevel application window for this project. The DisplayFrame 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.
  6. As noted in the introduction, you will need to complete the partially written file Queue.java by implementing its two principal methods, namely

    1. public void add (Object value)
    2. public Object remove ()

    You should now proceed to implement these methods, 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 added to the rear of the linked list, its link field must be set to null. Next the method must attach the node to the rear of the list (unless the list is currently empty; see the note below). Then it must update the Queue instance variable rear to reference the new node. Finally, it must increment the Queue item counter and return.

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

    2. remove() must first make a local reference to the data in the front node, since eventually it must return this value. Then it must effectively detach the front node from the list by updating the Queue instance variable front so that it references the next node in the list. Finally, remove() must decrement the Queue item counter and return the value it just removed.

      Note: Again, removing the last value from the Queue requires special care, since in this case both the rear and front pointers must be updated.

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


What To Submit

When your project is complete and working correctly, demonstrate it to your lab instructor. Then, before you exit NetBeans, clean your Lab08 project. Finally, before you logout, open a terminal window and use the cd command to enter your csci/202/labs directory. Then create a JAR file of your Lab08 project folder, using the command

jar cf Lab08.jar Lab08
Please leave Lab08.jar in your csci/202/labs directory for the remainder of the semester.