| CS 261 | University of Puget Sound | Spring, 2020 |
|
Computer Science II
|
||
|
Abstract Data Types and their Implementations,
Some Basic Algorithms,
Object-oriented Problem Solving, and Efficiency |
||
Warning:
This course is under development.
Although the basic structure of this course is largely established,
nothing on this Web site should be considered official or even possibly
correct.
DO NOT MAKE PLANS BASED ON THE CONTENTS OF THIS SITE UNTIL JANUARY, 2020.
This laboratory provides practice in using a singly-linked list to implement queues. Specifically, class Queue261Implementation1 will implement the Queue261.java interface, using a QueueNode class to store specific queue items.
Much of this material is an edited version of Henry M. Walker, Computer Science 2: Principles of Software Engineering, Data Types, and Algorithms, Little, Brown, and Company, 1989, Section 7.2, with programming examples translated from Pascal to Java. This material is used with permission from the copyright holder.
One common implementation of a Queue involves a singly-linked list of QueueNodes, for which a QueueNode class has the following basic structure:
package queues;
public class QueueNode <E>
{
/**
* Each QueueNode has a data field of type E
* and a next field to support a singly-linked list
*/
private E data;
private QueueNodee<E> next;
/**
* The default construtor sets the data and next fields to null
*/
public QueueNode<E> ()
{
data = null;
next = null;
}
/**
* An alternative constructor has both data and next parameters
*/
public QueueNode (E origData, QueueNodee<E> origNext)
{
data = origData;
next = origNext;
}
// other methods include the usual getters and setters
}
With this QueueNode structure, a Queue261Implementation1 of the Queue261.java class maintains three private fields numberItems, head, and tail. In this design, numberItems is an integer that specifies the number of items in the queue. Also, head and tail are pointers, as shown in the following diagram:
In this structure, head and tail mark the two ends of the queue, inserting items at the tail and deleting them from the head. Here, we view the queue as ordering items from the head to the tail; the head is the first item we will remove, and the tail is the last item. Also, the data field of a QueueNode contains the reference for one item on the queue, and the next field is a reference to the next QueueNode on the list.
The following notes outline some additional implementation details.
exceptions:
constructor:
insert:
remove:
Write class Queue261Implementation that provides a singly-linked-list implementation of the Queue261 interface, including a default constructor and methods add, remove, element, and size.
Be sure to test your implementation carefully!
|
created 29 March 2012 revised 4 April 2012 revised 6 November 2018 |
|
| For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. |