The Linked List Data Type#

The linked list data type handles a linked list in memory, including its instance fields and associated methods. Minimally a linked list must have a way of accessing the front of it, called the head. From the front, we can access any node by following the next links. Often we also provide a way to access the end, called the tail, and keep track of how big the list is with size. Here is the Java code for the data fields and constructor:

public class LinkedList<V> {
   //Instance Fields
   private Node<V> head;
   private Node<V> tail;
   private int size;
    
   //Constructor - makes an empty linked list
   public LinkedList(){
      head = null;
      tail = null;
      size = 0;
   }
}//class

Now we need to add in operations for linked lists, as we will see in the next section.

Practice Questions#

  1. Write a getter to return the head node of a linked list.

  2. Write a getter to return the tail node of a linked list.

  3. Write a getter to return the size of a linked list.

To Solutions