


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



CSC220: Programming II (Data Structures) - Fall 2003 Dr. Christian A. Duncan [email protected]
Until now, we have been focusing our data structure development using arrays. It is time to learn the next generic building block, the linked list. Linked lists are as versatile to use as an array. They are a bit harder to work with initially. But with practice they will become, maybe not your best friend in programming, but a nice buddy to have around. I prefer a slightly different structure to the linked list class presented in the book but to avoid any confusion we shall use the book’s presentation, as the key ideas are still there. Remember there are always different ways to organize your structure. This could mean different names for the methods (though some names are very popular) and different layouts to the class entirely. For example, C is not even object-oriented yet you can still create linked lists using structures. A linked list is a sequence of elements connected to each other by “links”. You can imagine it as forming a chain of elements. The general organization of a linked list is to group the element and link together into one structure (or class) called a node. Since the book deals with integers, we shall first look at a linked list of integers. In this case, you would define an IntNode which would look something like:
public class IntNode { private int data; private IntNode link; // methods go here... }
The biggest thing to notice here is that the link variable is of type IntNode, which is the current class being defined! How is this possible? Well, Java doesn’t care too much because it is really only interested in the fact that the link stores a reference to some other object. When you use it, it will then care what type of object that really is. Here is a very simple graphical depiction of the linked list structure.
data: ??? link
data: ??? link
data: ??? link
data: ??? link
head
null
That simple class along with many methods gives you a lot of power. There are several approaches to creating a linked list. We shall now look at one. First, we need a reference to the start of our sequence. The head of the sequence is usually identified by a variable called: IntNode head Remember head is not the actual node. It is a reference to the first node! It is also often useful to know the end of the sequence. In this case, we have the IntNode tail. Since every node has a link to another node, how do you identify the last node? What does it store a link to? For a standard linked list, that link would be simply null, meaning it does not point to anything.
We have seen nulls before. But just to stress some uses remember the following:
Just a friendly reminder as the book also points it out. What happens with the following bit of code?
String text; text = null; System.out.println(text.length());
Always be careful when dereferencing (a.k.a. accessing) a variable of value null. If you try to do that, a NullPointerException will be thrown and if not caught (as is most likely) the program will crash. Another warning, although I haven’t seen it with newer versions, one old java implementation actually core dumped when this happened. Java should always exit cleanly either by displaying the exception that was thrown or by ending normally at the end of the program. If it core dumps, there is a bug in the java implementation but it probably means you also have an error in your program as well. I don’t think this will happen for us but in case it does be aware of it.
There are several different methods you should write for a node. Mainly the following standard ones:
Once the basics for a Node are established the next thing to do is actually manipulate the linked list so it will have some use. For notational sake, I will often use either head or h to refer to the head of a node and tail or t to refer to the tail. In particular, we shall cover (first graphically then via implementation details), how to:
In nearly every case, always think about the special cases, these are typically when one of the affected nodes is referred to by the head or the tail or when the list was or becomes empty. All of these situations should be thought of before coding the method. In particular, ask questions like, “Could this method cause the list to become empty? And if so, what must I do special?” Also, think about how long each algorithm takes and compare that with the array versions of the Bag and Sequence classes. How long does it take to add a node to the list? In the worst case? How long does it take to delete a node? In the worst case? How about getting the length of a list?