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.

Lab on Queues with Circular Lists

Summary

This laboratory provides practice in using a circular list to implement queues. Specifically, class Queue261Implementation will implement the Queue261.java interface, using a QueueNode class to store specific queue items.

Acknowledgement

Most of this reading 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 5.6, with programming examples translated from Pascal to Java. This material is used with permission from the copyright holder.

Introduction

In the original pointer implementation of queues in the lab on Queues with Singly-linked Lists, a queue was implemented as a singly linked list, and elements of the list were objects of class QueueNode . Insertion into the queue took place at the head of the list, whereas deletion took place at the tail of the list.

One alternative approach to this implementation simplifies this structure so that the head element of the list follows the tail element, as shown in the following figure.

A Circular List Implementation of a Queue

This structure illustrates another commonly used list structure, called a circular list, in which the last list item points back to the first one.

In this list, each item points to the next, and this chain of pointers eventually forms a loop back to the first one. With this structure, we need only one pointer to the tail of the queue, and, from this last element, the next field specifies the location of the head of the queue. This requirement of only one pointer allows the declarations to be simplified considerably:

The following notes outline some additional implementation details.

Steps for this Lab

  1. Write class Queue261Implementation2 that provides a circular-list implementation of the Queue261 interface, including a default constructor and methods add, remove, element, and size.

Testing

Java's Queue interface specifies several common operations. According to Java's Queue description:

Besides basic Collection operations, queues provide additional insertion, extraction, and inspection operations. Each of these methods exists in two forms: one throws an exception if the operation fails, the other returns a special value (either null or false, depending on the operation). The latter form of the insert operation is designed specifically for use with capacity-restricted Queue implementations; in most implementations, insert operations cannot fail.

  Throws exception Returns special value
Insert add(e) offer(e)
Remove remove() poll()
Examine element() peek()

Java's Java's ArrayDeque provides an expandable-array implementation of this data structure.

For simplicity in this and some subsequent labs, we focus on just a few methods, as prescribed in the Queue261 interface.

Testing

In this and later labs, we will be considering several implementations of queues. For this lab, we need to consider when an implementation of Queue261 is correct.

  1. Identify what circumstances should be tested in order to be confident that a Queue261 implementation is correct. That is, specify (in English) a list of cases that should be tested when reviewing a Queue261 implementation; if an implementation passes these tests, we want to be able to conclude that the implementation is completely correct.

  2. Write out a list of specific tests (e.g., with specific data) that will cover all situations that you identified in step 1. For example, you might fill in an extended table of the following format:

    Operation Parameter, if any Expected Result
    create/constructor of String (String data type) Queue object created
    add "one" returns true, no exception, string "one" added to object
    add null NullPointerException thrown
    ...    
    ...    
  3. Translate your list of test cases into a sequence of tests implemented with JUnit.

  4. Test your implementation with the test suite that you developed in steps 2-4.


created 1 April 2012
revised 4 April 2012
revised 1 February 2020
link fixed 14 April 2020
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu.