Project 1 Hints
Below are a few points to hopefully save you some time:
- The removeFront and removeBack methods need to both remove the front/back of the list, and return what the data was that was previously stored there. This will entail first finding the correct node (it should be just to the "right" of the head for the "front", or just to the "left" of the tail for the "back"); saving the data reference in a local variable; and then removing the corresponding node from the list. After all that is done, you can return the "saved" data reference.
- The List12 interface requires that the list be able to accept null data elements from the user. You might want to leave the implementation of this detail towards the end -- though it will be part of your grade, it will only be a minor point. "Adding on" this bit of functionality to your code will not require a huge amount of effort, but you have to be careful. Basically, whenever you compare a particular Object o passed in by the user to the _data field of one of your list's Node objects, you need to use both the == operator (to check for null), and the equals method (if the _data field in your Node is not null). The order in which you use == and equals will matter -- if you use the wrong order, you will get a NullPointerException :-).
-
The List12 interface contains methods addToFront, addToBack, and add. The description of the add method says that it should be "equivalent to addToBack". This means that, in your method body for add, you literally just call addToBack. You might wonder why add was required in this assignment at all. It turns out it will be useful in later assignments so that the DoublyLinkedList12 class you're writing now, as well as the other data structures you will write later in this course, will all have a common interface. "Adding to the front" and "adding to the back" is in some sense specific to lists (because "front" and "back" assume a linear data structure), but "adding" in general is general to both linear and non-linear data structures. In any case, you don't need to worry about this explanation too much at this point -- just make sure add just "forwards the request" to addToBack.
-
To implement clear, you are encouraged to take full advantage of the JVM's garbage collection facilities. This means that, instead of removing each node individually (in a loop) by calling, say, removeFront, you can instead just start a completely new linked list, i.e., create new dummy head and tail nodes and link them together properly -- just as you (hopefully) did in your DoublyLinkedList12 constructor. As long as nothing points to any of the old nodes, the JVM will erase them from memory.
- There will be a lot of symmetry in the method bodies in P1 -- if you successfully implement addToFront, then implementing addToBack shouldn't be too difficult -- you're basically going to end up switching the word _head to _tail and _next to _prev (and vice-versa). This symmetry arises because adding to the front is the "mirror image" of adding to the back. A similar principle applies to removeFront and removeBack.