3-11 The Linked List

We have used an array to implement static stack and queue ADTs.  Arrays have both advantages and disadvantages.  The advantages of arrays are that they are simple to work with and provide very efficient access to data as long as we know the index.

The primary disadvantage is that arrays have a fixed size.  This means if we need to increase the size of an array a new larger one must be created, and every element must be copied from the old array into the new one – you experienced this first hand in 3-2 Q3.  Similarly, if you need to insert or delete an element in the middle of an array this will also be very inefficient because you will need to shift all of the elements after the insertion point by 1 position in the array.

Today we’ll investigate a dynamic data structure called a linked list, and later we’ll use this to build dynamic stack and queue ADTs.

The Concept of a Linked List

A linked list is made up of a chain of nodes.  Each node is an object that contains a data element and a reference to the next node in the chain (or null if it’s the last node).

Interestingly, the strengths of a linked list are the weaknesses of arrays and vice versa.  Linked lists are dynamic data structures so we don’t need to worry when writing our code that the size may be too small or too large (wasting memory).  Inserting or deleting nodes is relatively efficient compared to working with a large array.

All of this convenience comes with some downsides.  Linked lists are more complex data structures than arrays.  As you’ll learn, building your own linked list data structure requires a solid understanding of object references.  Furthermore, nodes are not accessed by index. Unless the data you want is at front of the linked list you may need to traverse the entire chain to find what you are looking for (i.e., linear search).

The bottom line is that linked lists are a good choice for storing data if you are often adding or removing elements, and only occasionally accessing the data, or mainly accessing the data in the first (or last) node.  As we’ll see later, this makes linked lists ideal for stack and queue implementations!

The ListNode Class

Let’s start by defining a generic <T> class to represent a single node in a linked list:

As you’d expect, I’ve made this a generic class (just like the ArrayList class) such that we can store any kind of object.  Notice that each ListNode object will encapsulate a reference to a data object and a reference to another ListNode object that may be linked after the current one.  Here’s some demo code to show how one might create and link two ListNode objects containing String data:

Peggy points to Allen, and Allen points to null.

This is what happens in memory:

First we instantiate a ListNode<String> called node2 with the data “Allen” and null (by default) set as its reference to the next node since this will be the last node in our chain.  Next we instantiate another ListNode<String> with the data “Peggy” and pass in a reference to node2 so that our new node1 will link to node2.  By linking nodes together in this way we are creating a simple linked list.

It’s important to realize that both the data and next instance variables of the ListNode class are references to objects.  In the example above, the data instance variable refers to a String object, and next refers to another ListNode object (or null).

Linked List Operations

As with any abstract data type (ADT) there are certain operations that linked lists are expected to have:

  1. To check if the linked list is empty (“isEmpty“).
  1. To delete all of the nodes in the linked list (“clear“).
  1. To check how many elements are in the linked list (“size“).
  1. To add a node to the front of the linked list (“addFirst“).
  1. To look at the data in the front node, but not remove it (“peekFirst“).
  1. To remove a node from the front of the linked list (“removeFirst“).
  1. To determine if the linked list contains a certain element (“contains“).

The LinkedListADT Interface

Based on the expected operations above, we can write a Java interface for a Linked List ADT:

The LinkedList Class

Ok, now let’s define a LinkedList class that implements the interface above.  We’ll do this in a step-by-step fashion.  Let’s start by defining our LinkedList class and its encapsulated data:

Because a LinkedList “has-a” series of ListNode objects associated with it, we encapsulate a ListNode reference variable called front to point to the first ListNode in the list (or null if the list is empty).  We also define a numberOfNodes variable to internally keep track of the number of nodes in our list.  There is no constructor required since front is set to null and numberOfNodes to 0 when they are defined, and when instantiating LinkedList there are no parameters (e.g., the size) that need to be passed in.

Method: isEmpty()

By definition, a linked list is empty when it has no nodes. In other words, when front is null.

Method: clear()

If a linked list contains a chain of nodes, then front will refer to the first node in the chain.  If we simply set front to null any ListNode objects will be unlinked and automatically garbage collected by the JVM – elegant!

Method: size()

The numberOfNodes variable will be used to keep track of the number of nodes in the LinkedList.  This method acts like an accessor method, but I’ve chosen not to call it getNumberOfNodes() because size() is the traditional name for this method when dealing with linked lists.

Method: addFirst()

Adding an element to the front of the list is very easy.  First, we create the new ListNode object containing the element data, and set its next reference to whatever front is currently pointing to.  Front may be referring to null if the list is empty, or the current first ListNode in the list.  Second, we make front refer to the new ListNode object we’ve just created and add 1 to the numberOfNodes counter.

Method: peekFirst()

To return a reference to the data in the first node, we must first check that there actually is a first node.  Otherwise, if we try to call the getData() method on a null reference we will get a NullPointerException.

Method: removeFirst()

Removing the first ListNode is almost as easy.  Usually when a node is removed from a LinkedList the caller would like to know what the data in that node was – kind of like a stack pop().  To avoid a run-time error, we again start by ensuring that the list is not empty.  If it is empty, we simply return null since there is no first ListNode (or data attribute) to be removed.

Assuming that the list has at least one ListNode, we first make a temporary reference (tempData) to the node’s data object (so that we can return it), then set our front reference to the second ListNode in the chain, and finally reduce the numberOfNodes counter.

Note that once there are no reference variables referring to the first node in the linked list, it will automatically be garbage collected by the JVM.  However, the data object that was associated with the first node is not garbage collected because tempData refers to it, keeping it active in memory.  It is the tempData reference that gets returned to the calling code.

Method: contains()

To check if a LinkedList contains a specific data element we must traverse (i.e., visit every ListNode) and compare its data with the search key that we are looking for – essentially a linear search.  If the search key is found, we return true , otherwise false.  Here’s the code:

Method: toString()

To aid with debugging, here is a toString() method to return a String representation of our LinkedList.  We are basically traversing the list like in the contains() method, except we aren’t looking for a particular search key.

Testing the LinkedList

Finally, let’s create a test class to demonstrate how our LinkedList works:

You Try!

  1. A linked list is a very versatile data structure. If you think about it, our LinkedList class provides all of the methods required to simulate a dynamic stack, they just have different names!  For example, the addFirst() linked list method is equivalent to the push() stack method.  Similarly, our LinkedList class also includes all of the methods needed to simulate a dynamic queue, except one!  Enhance the LinkedList class by completing an addLast() method:

    Hint: If the LinkedList is empty, then you can simply call addFirst() as usual.  If it is not empty, you will need to traverse the linked list until you reach the last node, then link a new ListNode to the end of the list.  Once you have this working, enhance the test program by adding an “append” command to demonstrate your addLast() method.
  2. Enhance the LinkedList class again by adding a removeLast() method.  The method should remove the last node from the linked list and return a reference to the data in the removed node.  Modify the test program by adding a “del” command to demonstrate our removeLast() method.  Hint: here you will have 3 situations to deal with: (1) if the list is empty, (2) if the list only has 1 node, or (3) if there is more than one node and you need to traverse the list.