SinglyLinkedList
class to get some practice with linked structures, add some testing code to measure how many computational steps it takes when working with the list, then add a feature to the list code that should improve its performance in some typical use cases.
SinglyLinkedList.java
and starting a project in either BlueJ or Eclipse with the file. Take some time to look over the code that's in the class to understand how it works.
SinglyLinkedList
when discussing linked structures, but there are two left for you to do: The remove
method that takes an index and removes the item at that position from the list, and the remove
method that takes an item and removes the first occurrence of that item from the list (if it's found). Implement these two methods and test them before proceeding.
node = node.next
in the code.) Implement the following methods:
getHopCount()
: An accessor ("getter") that returns the current value of the counter.
resetHopCount()
: A void method that sets the count back to zero.
ListTester
class with the following methods:
static int sumUp(SinglyLinkedList<Integer> list)
: A method that takes a linked list of integers and returns the list's sum. It should work from the front of the list to the back (low index values to high).
static int sumDown(SinglyLinkedList<Integer> list)
: A method that takes a linked list of integers and returns the list's sum, working from the back of the list to the front (high index values to low).
main
method that creates a list containing 10,000 integers, passes that list to sumUp
, then passes it to sumDown
. For full credit, main should print how many "hops" were required for each of those three steps individually (creating the list, summing up, and summing down). Don't forget to reset the hop counter between the steps to get an accurate count.
sumUp
, it's really annoying that for each item in the list we have to start all over at the head again to find it. (When adding the item at index 99 into the total, we have to start at the front of the list again and find index 99 even though we were just at index 98 on the previous iteration of the loop!) One simple performance improvement would be to add some internal state to our linked list class so that it "remembers" the most recently accessed position in the list.
Add two new fields to SinglyLinkedList
: An integer to record the most recently accessed position (index) in the list, and a Node
reference that points to the corresponding list node. Modify the code so that all calls to add, get, and remove (the version of remove that takes an index) update these fields. Those methods should take advantage of the new information when possible as well. For example, if we do a get(107)
and the previous access to the list was at index 98, the list traversal should start at 98 and work its way to 107 rather than starting at 0. Think carefully about where to add this new code before you make any changes — if you put it in the right places it can be done without a lot of effort.
remove(int index)
organized and implemented properly
remove(E item)
organized and implemented properly
ListTester
class organized and implemented properly
SinglyLinkedList
to implement the List
interface. If it did, we could use it interchangeably with ArrayLists
and other built-in Java list structures!
@param
and @return
directives) above each method. When you're convinced it's ready to go, zip up your project folder and submit it via the Canvas submission tool for Assignment #3.