MINI-LAB #1
Linked List
The goal of this lab is to create a singly linked list that adds new nodes at the front and can delete any node by index. You must also insure that your methods correctly update the property size
for the list. You will be modifying the class List
which stores objects (in this case String
) as the data portion of the nodes.
You need to write the code in class List
for two methods addElement(T element)
which accepts any valid object as the data for each node of the list and adds a new Node
at the front of the list, updates the pointer first
and the property size
. You also need to write a method removeElement(int index)
which correctly removes the node at index
, returns the data (element
), updates size
, and fixes up the list pointers.
You might find it useful to write a toString()
method to print the linked list. This is optional.
- Begin by downloading the file ListMiniLab.zip. Unzip that file, and import the project into BlueJ (or your preferred GUI). From the "Project" menu select "Open Non BlueJ..." and browse to locate your unzipped folder.
- Add code to
List.java
to complete the exercise. - Test your code using the
ListTester.java
class which contains themain
method
Bonus Portion
This mini-lab has an optional portion. If you complete this portion you'll have 5 bonus points:
Implement the following methods inside List.java
and write the appropiate tests in ListTester.java
.
- Implement the method
public void addElementAfter (T element, int index) throws NoSuchElementException
which will receive anelement
and it will add it after the specifiedindex
. For example,addElement (“A”, 1)
will add“A”
after the element at index1
, that is at index 2. Take care of indexes that are out of bounds. - Implement the method
public void clear()
which will clear the linked list deleting all references and setting thesize
to zero. - Implement the method
public String toString()
which, when invoked, will return a String containing all the elements of the linked list. For example[Z, Y, X]
for a list with 3 elements Z, Y, and X or[]
for an empty list.
Submit your completed program through Kit, under Mini-Lab 1.
EVALUATION
Mini-lab #1 is worth 15 points with an extra 5 bonus points. This project will be submitted individually. The following items describe the criteria we will use to evaluate your mini-lab:
- Correct implementation of
addElement
method 6 pts. - Correct implementation of
removeElement
method 6 pts. - Adding appropiate amount of comments to the code (your name and description of the class, method description, and so on) 1.5 pts.
- Proper organization of your code (indentation, clarity, cleverness) 1.5 pts.
- Bonus portion:
- Correct implementation of
addElementAfter
method 3 pts. - Correct implementation of
clear
method 1 pts. - Correct implementation of
toString
method 1 pts.
- Correct implementation of
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