Carnegie Mellon University Website Home Page
 
Fall 2014

15-121 Homework 4: Linked List Operations - Due 10/22 at midnight

Download and unzip hw4-code.zip, which contains all the files you will need for this assignment. Open the file MyLinkedList.java. All of the methods that you will be writing should be added to this file. ListTest.java is where you will find a main method that you will use to test the new linked list methods that you are writing.

BONUS for early submission: students who submit their work by Tuesday, 10/21 at midnight will earn a 5-point bonus on the assignment.

Note: You will be graded in part on your coding style. Your code should be easy to read, well organized, and concise. You should avoid duplicate code.


Background: The Linked List

In this assignment, you'll be adding more methods to the MyLinkedList class we've been developing recently. When you're done, you'll have a pretty good start on a fully-fledged generic linked list class!


The Methods

There are 10 new methods that you will be writing for this homework. Just like in class, some are easy, some are hard. I suggest that you write them in the order I have specified them (hint, hint). I have provided code that tests (almost) all the methods by calling them from the main method in the file ListTest.java. I have also added stubs for the methods that you have to write so the code in ListTest.java compiles.

The 10 methods are as follows:
Anything getFirst()
Anything getLast()
void add(Anything value)
void addAfter(int index, Anything value)
Anything set(int index, Anything newValue)
int lastIndex(Anything value)
MyLinkedList<Anything> clone()
void removeAll(Anything value)
boolean equals(Object o)
MyLinkedList<Anything> split()

Method Specifications

You need to complete all of the following methods in the MyLinkedList class. You can write the methods in any order you wish, but I suggest you do them in the order they are listed. And, remember, no working on the last bonus until the 10 methods are done and correct! In particular, make sure all your methods work for empty lists, lists of different sizes, etc. as you can see from the sample test cases provided in ListTest.java. Also, make sure that your solutions do not modify the original lists (unless you are specifically instructed to do so).

void main(String[] args)
You can use this method in the ListTest class to create lists as needed and test each of the 10 methods listed above. I have provided a number of test cases, but you may want to add more to some of the method tests, and I deliberately left one out. :-) Until you get add working, you will need to use addFirst to create lists to test your other methods. Ultimately, you should augment the test suite in ListTest.java to more exhaustively test your code.

Anything getFirst()
This method returns the value stored in the first node in the list. It should print an error message and return null if the list is empty.

Anything getLast()
This method returns the value stored in the last node in the list. It should print an error message and return null if the list is empty.

void add(Anything value)
This method adds a node containing newValue to the end of the list, updating the length appropriately.

void addAfter(int index, Anything value)
This method adds a node containing value immediately after the position specified by index, if such a position is in the list. Prints an error message if index is out of range.

Anything set(int index, Anything newValue)
This method replaces the data in the node at position index with newValue, if such a position is in the list and returns the previous (old) value that was at that location. Prints an error message and returns null if index is out of range.

int lastIndex(Anything value)
Returns the index of the node that contains the last occurrence of value in the list. Consistent with Java indexing, the first node is at position 0. If value is not in the list, returns -1.

MyLinkedList<Anything> clone()
Returns a new list that is a (shallow) copy of this list. This method must run in linear time or you will lose points!

void removeAll(Anything value)
This method removes every node that contains value in the list. Realize that the value can occur multiple times and anywhere in the list! This method does nothing if value is not in the list.

boolean equals(Object o)
This method overrides the equals method (found in the Object class). Since you are overriding the equals method, it must have the same signature as the one found in the Object class! As a result, you will need to cast the Object parameter to a variable of appropriate type (MyLinkedList<Anything>). I suggest that you use the following line of code as the first line in your solution to accomplish the cast (it will generate an unchecked cast warning, but that's OK):

MyLinkedList<Anything> list = (MyLinkedList<Anything>)o;
Once you've made the cast, the method should then compare list with this list for equality. The method returns true if and only if both lists have the same size and all corresponding pairs of elements in the two lists are equal. You must not create any other list in this method, and your solution must not modify the original lists.

MyLinkedList<Anything> split()
This method splits the original list in half. The original list will continue to reference the front half of the original list and the method returns a reference to a new list that stores the back half of the original list. If the number of elements is odd, the extra element should remain with the front half of the list.

Other methods
It is OK to add other methods (helper methods) to complete the 10 methods described above.


Efficiency and ADDITIONAL BONUS POINTS

You don't have to worry (too much) about efficiency on this assignment (the lone exception to this is the clone() method. However, I will award 10 points bonus if you add a new instance variable to the MyLinkedList class called last and use it (and update it appropriately and consistently) to quickly access the last element in the list in any and all methods that need to access that element. Note: you should only attempt this bonus once you have all the methods working correctly!


Submitting Your Work

When you have completed this assignment and tested your code thoroughly, create a .zip file with your work (including both MyLinkedList.java and ListTest.java). Name the zip file "your-andrew-id".zip and email it to me mjs @ cs.cmu.edu.

Make sure to keep a copy of your work just in case!