|
CMPU 102 - Assignment 3 List ADT, Circular-Doubly-Linked-list |
Assigned: Monday, Feb. 18 Due
|
In this assignment you will:
Summary: You will be given a starter project that contains source files:
The data structure "behind the wall" will be a circular, reference-based implementation of a sorted linked list. The ADT you will implement is listed below. The classes you will implement are GNode and CircDoublyLL.
public interface SortedListInterface<E> {
/** Determines whether this sorted list is empty. */
public boolean sortedIsEmpty();
/** Returns the number of items that are in a sorted list. */
public int sortedSize();
/**
* Inserts item into its proper sorted position in a
* sorted list.
*/
public void sortedAdd(E item);
/**
* Deletes item from a sorted list.
* Throws an exception if the item is not found.
*/
public void sortedRemove(E item);
/**
* Returns the item at position index of a sorted list,
* if 1 <= index <= sortedSize().
* The list is left unchanged by this operation.
* Throws an exception if the index is out of range.
*/
public E sortedGet(int index);
/**
* Returns the position where item belongs or exists
* in this sorted list; item and the list are unchanged.
*/
public int locateIndex(E item);
}
You should begin by implementing this class, in file GNode.java. Objects of this class have three instance variables: prev and next, which are references to other GNode<E> objects, and item, which will contain a reference to a generic item of type E.
Type E is a placeholder for the actual type that will be supplied by the client in the application class Main. Since GNode<E> extends Comparable<E>, the actual type that replaces this placeholder must implement Java's Comparable interface. The SortedLinkedList interface is generic, which means that the class CircDoublyLL that implements it and the class GNode that CircDoublyLL uses to hold the sequence of items must also both be generic.
Implementation class GNode is very similar to the implementation of class Node for singly-linked lists given in Carrano & Prichard on pages 231-232 except that now there are two references to other GNode objects and the placeholder E is used to indicate the type of item that is contained.
Next implement this class, in file CircDoublyLL.java. In this class we've provided the fields you must use, and the stubs (headers and empty bodies) for all the methods in the SortedListInterface that must be implemented. You must implement the ADT methods using only the fields provided, although you may implement private helper methods, if you wish.
You are given two instance variables: head and size. The variable head is a reference to a dummy head node.
When you implement the constructor for this class you will create a GNode<E> object whose two references both reference itself. The size of the list will initially be 0 (the dummy head node doesn't count).
When you implement the sortedInsert and sortedRemove methods in this class, be sure to adjust the list size accordingly. With a dummy head node, there are no special cases for performing inserts or removes. Just be sure you cannot delete the head node. When the first node is added to the list, it will have the same predecessor and successor (the dummy node head refers to). After the first insert occurs dummy.prev and dummy.next will refer to the newNode; newNode.prev and newNode.next will both refer to the dummy. This is not a special case. The sortedInsert method is implemented the same way for all inserts. You will need to declare a local GNode reference to traverse the sorted list to find the location where the new item is to be placed. You must also be able to recognize when you reach the end of the list (if that is where the new item should go). You may refer to page 262 in Carrano & Prichard for additional information on implementing this method.
The same situation exists when you are performing a remove or a get. You must move a node reference to the node containing the item to be retrieved or deleted and be sure that you do not continue to circulate around the linked list. If the item to be deleted is not present in the list, do nothing. Pay close attention to the comments in the stubs and in the interface listed above to help you implement each of the methods of the class.
There is nothing for you to implement in Main, but you should read through it and understand how the code tests your CircDoublyLL class. We have created two CircDoublyLL objects holding items of types Integer and String, respectively. Items for both of these lists are produced from a list of primitives of type int that are converted into Integer and String objects. Initially, 15 values will be added to each list. Notice the use of the for-each loop to iterate through the array of values and add them to the lists. Pretty cool, huh?
Since each list contains items of a different respective type, the resulting sorted lists will contain the same items in different sequences. Next, each list will be printed out in forward and reverse order. (In case you were wondering when we were going to exploit the doubly-linked lists.) Once the lists are printed, we check for the position of a particular value in both lists.
Then, some elements will be removed from each of the lists, and the two lists will be printed in forward and reverse order again. Finally, we'll check the position of the same item we checked before (the new positions should reflect the removals). You should refer to the code in Main to observe how these lists are declared and constructed, and you should note and be able to explain the difference in the ordering between the Integer and String items in the two lists.
:From a terminal window, type the following commands:
cdcd cs102submit102 assign3