Return to lecture notes index
February 10, 2003 (Lecture 11)

Overview

Today we talked about linked lists. As you know, there is a competency exam at the end of the semester, which is worth 30% of your final grade. This exam has three questions, and two of them are on linked lists. So, not only is this material about something fundamental to the discipline, it is critical to your grade. It will form much of the base of the material for the next exam -- and account for up to two letters of your final grade through the end-of-semester exam (2/3*30% = 20% of your final grade).

List Basics

So let's back up a little bit. What is a list? A list is a collection of things, possibly ordered, depending on your usage. For instance, a shopping list can be in any order, but if you want to make a scheduled list of things to do, you will probably want them to be in some sort of chronological order. Now, what is a link? A connection between two things.

So what is a linked list? A collection of things that are connected one by one. Let's think of it this way: Imagine you have a bunch of buckets. What's missing in this picture? The links!

So we not only need something to hold an object, but a reference to the next bucket. We call this combination of a bucket and it's link a Node.

The Node Class

Each Node contains two things: A reference to the Object the "bucket" is containing and a reference to the next Node in the list. Here's a loose skeleton of a Node:
	private class Node {

	   private Object data;
	   private Node next;

	   public Node(Object o, Node next) { };

	   public Node getNext() {};
	   public Object getData() { };

	   public void setNext(Node Next) { };

	}
  

You'll notice that the class is private. Why is this? Consider the Java API for a Linked List. Do you ever see anything to do with any sort of Node? No, so that's exactly why it's private. No one outside of the LinkedList, itself, needs to know about what goes on at the Node level, so the class is private.

The LinkedList class

We've talked about the Nodes that make up a linked list, but what do we actually need in the list? Well, we definitely need the first Node, right? Once you have the first Node, you can access the rest of the list. But now what? You can look at the first Object, but how useful is that? You need to be able to look at all the other Objects as well, without losing your handle on the first Node.

Imagine you are holding onto a box of index cards with one hand. You can access the cards, since they're all in your possession, but if you try to reach into the box with the same hand you're holding the box with, you'll drop all of them. So what you need is another hand to sift through the stack, right? So we need a reference to a helper node that we'll use to traverse your Linked List. We'll call this one an index.

Another thing we want to know in the list is where the tail is, so we can add to the end of the list just as efficiently as we can add to the beginning, instead of having to send the index all the way down to the end.

So here's a very basic (and meager) structure of a LinkedList class:


	private class LinkedList {

	   private class Node{
	      //see above
	   }

	   private Node head;
	   private Node tail;
	   private Node index;

	   public LinkedList( ) { };

	   public void addHead(Object o) { };
	   public void addTail(Object o) { };
	   public boolean find(Object o) { };
	   public Object remove(Object o) { };

	}