Linked List MINI-LAB
The goal of this mini-lab is to complete a singly linked list that adds new
nodes to the front or end of a list, can remove nodes from either end, and
can delete any node by index. You must also ensure that your methods
correctly update the size property for the list. You will
primarily be modifying and completing the class K_SimpleLL,
which consists of a linked list of K_LLNode nodes.
There are two common patterns for stepping through a linked list:
for (K_LLNode curNode = first; curNode.getNext() != null;
curNode = curNode.getNext())
{
// Do something with curNode
}
or
K_LLNode curNode = first
while ( curNode.getNext() != null )
{
// Do something with curNode.
// Advance curNode.
curNode = curNode.getNext())
}
The get, remove and toString methods
in K_SimpleLL provide examples of variations on the first
pattern, either because they have to keep track of the index number or they
have to handle the last node in the list a little differently from the
previous nodes (or both).
Completing a Linked List Class
- Download three files into a new folder: Import the folder into BlueJ (or your preferred GUI). For example, in BlueJ, select "Open Non BlueJ..." in the "Project" menu and browse to locate your folder.
- Agile software development: Read the (incomplete) classes provided to you and try running the program before making any modifications to be sure that you understand its initial functionality.
- Add an extra constructor to the
K_LLNodeclass that takes 2 parameters, a data element and a reference to anotherK_LLNodeclass. - Add code to
K_SimpleLL.javato complete theaddFirstmethod. TheaddFirstaccepts any valid object as the data for a new node in the list, constructs a newK_LLNodeat the front of the list, and updates thefirstpointer and thesizeproperty of the list. (Hint: your newK_LLNodeconstructor is appropriate here.) Test your code using theK_SimpleLLTester.javaclass, which contains themainmethod - Complete the
isEmptymethod in theK_SimpleLLclass. It returnstrueif the list is empty, orfalseotherwise. - Complete the
addLastmethod. It takes the same parameter asaddFirst, but adds the element at the end of the list. This method can use either theforpattern orwhilepattern illustrated above. Note that adding a "last" element to an empty list is a special case (and is actually the same as adding a "first" element to an empty list). Add code to an existing method or a new method inK_SimpleLLTesterto test your new method. - Implement
removeFirstandremoveLastmethods to remove the first and last elements from the list. Likeremove, they should return the element that they are removing. (Hint: you can use theremovemethod!) Write code inK_SimpleLLTesterto test your new methods. - Read and compare the
getandremovemethods. Make sure you understand why thegetmethod can be simpler thanremove. Add code to an existing method or a new method inK_SimpleLLTesterto test thegetmethod. (The test driver already has code to test theremovemethod.) - Implement the
set(int index, T element)method. This method is similar to theget, but replaces the value at the specified location.
Analysis Question:
- What is the time complexity of each method in the
K_SimpleLLclass?
- What is the time complexity of each method in the
Adding a Pointer to the Last Element
- Add a new instance variable called
lastto theK_SimpleLLclass. This new instance variable should always point to the last element in the list.
Analysis Question:
- What methods will need to change as a result of this new instance variable?
- Modify the constructor and the
addFirst,addLast, andremovemethods, as well as any others that need to change. Can any methods be done more simply or more efficiently now that you have this new variable? Test your changes using your existing tests. (This is called regression testing.)
Analysis Question:
- Did your modifications change the time complexity of any methods in the class?
Optional Improvements
This mini-lab has an optional portion to add extra interest.
Implement the following methods inside K_SimpleLL.java and
write the appropiate tests in K_SimpleLLTester.java.
- Implement the method
public void addAfter (int index, T element) throws IndexOutOfBoundsExceptionwhich will receive anelementand add it after the specifiedindex. For example,addAfter (1, “A”)will add“A”after the element at index1, so the“A”is at index 2. Take care of indices that are out of bounds. (Question: how would anadd(int index, T element)method be implemented differently if it were to add the new element at the specified index, inserting it in front of the element that used to be at that index, instead of after?) - Implement the method
public void clear()which will clear the linked list, setting thesizeto zero. Question: What references need to be cleared for this method? What is the time complexity of this new method?
Submit your completed program through Kit, under LinkedList Mini-Lab.
Have fun! And if you have any questions remember my email, my office hours, and the Collaboration Center are here for you!
← back to the schedule