Lab 4: Linked Lists
Due: Thursday, Feb. 18, 11:59pm
The goal of this lab is to give you experience with implementing data structures from scratch. If you were frustrated that there were parts of graphics that seemed mysterious, in this lab we'll be building everything from the ground up. There is not a lot of code, but make sure you understand everything you're writing, as well as the starter code (which we did in class).
You are welcome to start from the files from Class 7. Below are the same files, except CourseManager.java now contains all the code you'll add to main. So you can start with that version and comment out most of it to start. Then gradually add in the functionality as you write the methods (either way is fine).
To start, make a new lab4 package and add the following files:
Change the author field to be your name on each file. I would recommend having a piece of paper and pencil when you do this lab, to diagram what is happening with each of the functions.
Number of courses: 2
public Object get(int index) { // TODO }This method will have a similar structure to the add method, but there will be some differences. Now add the following code to your main (at the end of main) to test your method.
// get a course at a specific index System.out.println("Course 0: " + courses.get(0)); System.out.println("Course 1: " + courses.get(1)); System.out.println("Course 2: " + courses.get(2));What happens? If the printout looks strange, make sure you're not returning a Node object, but the actual data instead. You should write a method to accomplish this in the Node class.
public void add(int index, Object newData) { // TODO }For this method, there are a few cases. First, add the code below to your main to test your method. Before you write the method, what should happen?
// add a course at a specific index courses.add(1, "aerobics"); // feel free to change this System.out.println("Course 0: " + courses.get(0)); System.out.println("Course 1: " + courses.get(1)); System.out.println("Course 2: " + courses.get(2)); System.out.println("Number of courses: " + courses.size());Think about how to update the references (you might also need a for loop to get to the right place). After that works, change the "1" index to "0", so that you're adding the element at the beginning of the list. What happens? If you get an error, how can you account for this special case?
Test out your method in a range of scenarios by changing the index variable. Make sure to account for indices that are out of range. In that case, it might be nice to print out an error message or throw an exception. (Instead of receiving error messages all the time, you can give them out!)
Here is some starter code to add to main to make sure this part is working:
// add courses until you reach your schedule, // in an order that makes sense for you courses.add(3, "CSC 103"); courses.add(0, "yoga"); System.out.println("Number of courses: " + courses.size()); for (int i=0; i < courses.size(); i++) { System.out.println("Course " + i + ": " + courses.get(i)); }But now there might be courses in your list that you are not actually taking, we need to remove them!
public void remove(int index) { // TODO }For this method, make sure to diagram what is happening first on paper. Think about any special cases you'll have to consider, similar to the add method. You can add the code below to main to start:
// remove courses you're not taking courses.remove(3); System.out.println("Number of courses: " + courses.size()); for (int i=0; i < courses.size(); i++) { System.out.println("Course " + i + ": " + courses.get(i)); }Try removing each course in turn to make sure it works (and out-of-range indices to make sure those don't do anything except print an error).
import java.util.LinkedList; ... LinkedListcourses = new LinkedList ();
If you do this part, make sure to change it back afterwards (and remove the import statement) so that you code is based on your own linked list class.
If you have more time, you may begin work on Homework 4.