CS 314 - Specification 5 - Linked Lists
Programming Assignment 5: Individual Assignment. You must complete this assignment on your own. You may not acquire from any source (e.g. another student, a web site, a LLM or chatbot such as chatGPT) a partial or complete solution to a problem or project that has been assigned. You may not show another student your solution to an assignment. You may not have another person (current student, former student, tutor, friend, anyone, LLM) “walk you through” how to solve an assignment. You may get help from the instructional staff. You may discuss general ideas and approaches with other students but you may not develop code together. Review the class policy on collaboration from the syllabus
The purpose of this assignment is to implement a linked list class that uses doubly linked nodes. Doubly linked nodes have references to the next node in the list and back to the previous node in the list. This makes some things harder because there are more references to set up so more chances for logic errors. But, it makes a lot of things easier because it is possible to move backwards in the linked structure of nodes without having to start over at the beginning, use the look ahead technique, or use a trailer node.
Files:
File | Responsibility | |
Source Code | IList.java Interface your LinkedList class implements. | Provided by me. Do not alter |
Source Code | Stopwatch.java | Provided by me. Do not alter. |
Implementation | LL314.java. Implementation of a doubly linked list. This class has the DoubleListNode.java class nested inside it. | Provided by you and me. (Okay, mostly you.) |
Test code | LinkedListTester.java. Test harness. correct any tests that have errors. Ensure your LinkedList class passes the tests included and add more tests. | Provided by you and me. |
Optional Visual Tester | LinkedListView.java A tool [its use is optional] created by one of our TAs [Andrew] for viewing the LinkedList. Download the class and refer to the documentation on the GitHub page. | Provided by TA Andrew |
Documentation | Javadoc for IList. |
Draw pictures!! When completing methods and figuring out all the references that have to be updated and what the special cases are DRAW PICTURES of the linked structure. This is much easier than just looking at code and trying to determine (guess?) what must be done or why something does not work. Don't program by permutation or try cargo cult programming.
Please realize, when you get an error in a test, especially toString, the cause is typically in another method. It is often the case that students make errors when adding or removing elements or changing the structure of the internal storage container (the linked structure of nodes), but the error does not manifest itself until later on in a test of toString.
Implementation details:
Complete the LL314
class. For this assignment you may
not use the Java LinkedList
class or any other data structures from the Java standard library. You must
implement the underlying storage container as a doubly linked linear structure
using the DoubleListNode
class. The DoubleListNode
s
and LL314
are generic based on Java's generic syntax.
You may approach the implementation in many ways. (References to first and last node, circular list with only a link to the first node, use of header nodes that don't contain any data) The approach you take is up to you. Each has certain advantages and disadvantages.
In addition to implementing the methods
specified in the IList
interface you must add the
following methods to
the LL314
class. E is the data type variable. It will hold
what the data type is for the elements (thus the E) of a particular
LL314
object.
void addFirst(E item) // add an item to the front of this
LL314
void addLast(E item) // add an item to the end of this
LL314
E removeFirst() // remove the first item in this LL314
E removeLast() // remove the first item in this LL314
toString
method. The data in the list should
be listed between square brackets with a comma between each item and a
single space after each comma. e.g. [A,
B, C]public String toString()
equals
method. Two IList
s are equal if they
have the same number of elements in the same order.public boolean equals(Object other).
Two empty lists are
equal regardless of the kinds of elements they store. (This is true in the
Java standard library.) Also note, we
are implementing the IList interface. To meet the requirement IList puts
on classes that implement it, your LinkedList is considered equal to any
other IList (not just a LL314) if it has the same elements in the
same order. LinkedListTester
provides some tests for the LL314
class. In your final submission delete the provided
tests. Add at least 2 tests of your own per method in
the LL314
class to the LinkedListTester
class.
Delete the provided tests. You may post the tests you write to the class class discussion group,
but in the end you must implement your own tests.
Note, most of my tests rely on your iterator method. If that is not working you will fail those tests. I strongly recommend you test your methods as you complete them by writing your own tests! Write a method, then test it. Write a method, then test it. DON'T do this: write a method, write a method, write a method, ... then test ...then debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug.
For every method in the LL314
class state the Big O
of the method if there are already N items in the list.
The methods for the iterator for your linked list shall all be O(1). Implement the hasNext and next methods for your iterator. You shall replace the default remove method from the Iterator interface with a remove method that actually removes the last element returned by next. Your iterator class does NOT need to detect co-modification errors.
If a method has preconditions you must check those preconditions and throw an Exception if they are not met.
You can, of course, implement methods using other methods from the class. You should only do this if the version of a method that relies on other methods in the class is as efficient as not using other methods.
As always, use good style: comments for non obvious algorithms and operations, good variable names, use helper methods if necessary, make operations as efficient as possible given the constraints.
Experiments: It is interesting and important to know the difference in
behavior between linked lists and array based lists. When your LL314
class is finished, run the comparison method from the LinkedListTester
class which performs a number
of tests with your LL314
and the Java ArrayList
class.
To run the method simply uncomment the line at the end of the main method in
LinkedListTester that calls the comparison
method. You are free to
change the initial value of N for the various tests in the comparison
method.
In a comment
at the top of your LinkedListTester
class indicate which operations are
faster when using your LL314, which are faster when using the Java
ArrayList
class, and which ones are about the same.
(Realize the experiments use different starting values of N.)
For the operations tested via the experiment what do you think the Big O of each operation / method is based on the timing data? State your reasoning.
Submission: Fill in the header in the LinkedListTester.java and LL314.java files.. Replace <NAME> with your name. Note, you are stating, on your honor, that you did the assignment on your own.
Submit your files to assignment 5 on Gradescope.
Checklist. Did you remember to:
LL314
class states the Big O of the method
in the method comment?LL314
passes the tests in
LinkedListTester.java and add your own tests?