CS 261 Assignment #3

Due Sunday, February 26th by 11:59pm
Not accepted after 2/28

Introduction

On this assignment you'll work individually to complete the book's 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.

The Assignment

  1. Start by downloading a copy of 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.
  2. The book writes most of the methods in 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.
  3. When inserting, removing, or retrieving a value from a singly linked list, the list must first be traversed to get to an appropriate position in the list. That seems like it could lead to an awful lot of traversing. So that we can quantify the work done during traversals, modify the class so that it maintains a counter variable, and increments it each time a "hop" from one node to the next occurs. (Look for things like node = node.next in the code.) Implement the following methods:
  4. Now write a ListTester class with the following methods:
  5. When calling 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.

Grading

This assignment will be graded out of a total of 100 points.

Extending the Assignment

The trick you implemented in the final step is a simplified version of an iterator. Consider adding full iterator functionality to your list class. You might also investigate what would be required for SinglyLinkedList to implement the List interface. If it did, we could use it interchangeably with ArrayLists and other built-in Java list structures!

Submitting

Before submitting, test each of your methods thoroughly and double check for comments (including @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.


Brad Richards, 2023