Project 2: Doubly-Linked Lists
Due Date: Thu, May 22th (extended)
Lists are vital in computer science and are used frequently in programming.
Because of this, it is important that you fully understand how lists work.
In this project, you will implement a doubly-linked list.
Goals of the Project
The primary goals of the project are to:
- Help you understand inheritance.
- Help you understand how generics work with Java.
- Help you understand how linked lists work, and how to implement them.
- Help you understand how to make a class Iterable.
- Help you learn how to program and debug with Eclipse better.
- Help you further understand how to analyze the performance of
algorithms.
What to Do
You will need at least one interfaces and two classes to build this project with
the following names:
- List<Item>: an interface, which describes how we access things in a list.
- DLLIterator<Item>: a class which implements Java's Iterator<Item> interface from java.util.Iterator.
- DLList<Item>: a doubly-linked list that implements the List<Item> interface.
Iterator<Item> has the following methods:
- public boolean hasNext(): returns true if
there's another item to advance to, false otherwise.
- public Item next(): returns the item and
advances (internally) to the next item.
- public void remove(): removes the item
we just iterated over. In other words, you must call next() before
a call to remove().
Note that you will need to create a concrete version
of this Iterator class from DLList that can walk through the DLList. This
is done by creating a private, inner class inside DLList and returning it
through the call from iterator().
List<Item> extends java.util.Iterable<Item>. List has the following methods:
- public void add(Item i): Adds an item
to the list. Note that the type 'Item' is a generic type.
- public void clear(): Removes all items
from the list.
- public void contains(Item i): Returns
true if the list has the item, false otherwise.
- public Item get(int index): Returns
the element at the given index in the list.
- public int indexOf(Item i): Returns the
index of the given item, or -1 if it's not found.
- public boolean isEmpty(): Returns
true if the list is empty, false otherwise.
- public Iterator<Item> iterator(): Returns
an Iterator<Item> object for this list.
- public Item remove(int index): Removes the
element at the given index, and returns it
- public int size(): Returns the size of the list.
- public void sort(): Sorts the underlying list. Hint: Use java.util.Arrays
- public List<Item> subList(int fromIndex, int toIndex): Returns a list taken from the items starting at fromIndex and ending at toIndex. Note that we don't include the item at toIndex, but only those before it.
DLList<Item> implements List<Item>. You must therefore define all the methods
in the List<Item> interface as specified above. Note that the List<Item> interface
is slightly different than Java's, so you have to create it yourself.
Things to Note
You may not use Java's LinkedList class in any way in this project. You
must name your classes with the given names. Your Doubly-Linked List
class must be called DLList. We will be creating a main method in a
testing class that will expect these names. If you do not use these names
correctly, you will loose points!
If the user performs any illegal operation (for example, more than one
call to remove) then you must throw an exception.
Your specific Iterator class and the Node class should most likely be
internal classes for DLList. Internal classes are accessible
by the parent class and are defined just like regular classes, but inside
another class. Note that List<Item> must extend Iterable<Item>
so that you can use Java's foreach call.
Turn-in
You will need to use subversion with your group or solo (remember, groups
can be up to 3 people). We will simply check-out your repository at the
end of the day that the project is due, so check-in your project frequently
and of course when you're done.
You must include comments in your code to explain what you're doing. In
your comments, you need to state what the expected running time, in
terms of n, will be to call that method.
Grading
Your project will be graded on the correctness of your implementation
and your understanding of its performance. In particular:
- Did you name all the classes as specified in the project (List, Iterator, DLList)?
- Did you correctly implement all the classes and interfaces as specified in the project description?
- Does it correctly add and remove items from the list?
- Does it correctly create an interator? Can we use Java's foreach call with it?
- Does your iterator work and allow you to walk through the list?
- Does your iterator allow you to delete items?
- Does your list sort correctly?
- Do you handle errors correctly (ie, throw an exception)?
- Did you get the correct performance for each operation?
Extra Credit
For extra credit, add the following two methods to the Iterator class:
- public Item previous(): Returns the previous item from the Iterator, moving the iterator backwards.
- public boolean hasPrevious(): Returns
true if there is a previous element to return from the iterator,
or false otherwise.
Note that if you remove an item, it must be the item you just returned
from previous or next. If you add an item, it
will be returned
by a subsequent call to previous.