Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the 'head' of a linked list?

I'm working in linked lists in Java, so I'm trying to grasp the concept of a single linked list.

head -> 12 -> 34 -> 56 -> null

head.next would be 12 (also the same as node1). However, what is head then?

Update: What is the difference between a reference and a pointer?

Update2: So if head is 12 and head.next is 34, then doesn't mean this following function skips the first node to see if it's null?

public void add(Object data, int index)
    // post: inserts the specified element at the specified position in this list.
    {
        Node temp = new Node(data);
        Node current = head;
        // crawl to the requested index or the last element in the list,
        // whichever comes first
        for(int i = 1; i < index && current.getNext() != null; i++)
        {
            current = current.getNext();
        }
        // set the new node's next-node reference to this node's next-node reference
        temp.setNext(current.getNext());
        // now set this node's next-node reference to the new node
        current.setNext(temp);
        listCount++;// increment the number of elements variable
    }

Source: http://www.mycstutorials.com/articles/data_structures/linkedlists

like image 652
Strawberry Avatar asked Feb 11 '11 20:02

Strawberry


People also ask

What is the value of head in linked list?

Unlike arrays, the entry point into any linked list is the head of the list. Head of the list is not a node but a reference to the first node in the list. In other words, head can be considered a lvalue. In an empty list the value of head is equal to null.

What is the head of a node?

A head node is setup to be the launching point for jobs running on the cluster. When you are told or asked to login or access a cluster system, invariably you are being directed to log into the head node.

Is linked list head null?

The entry point into a linked list is called the head of the list. It should be noted that head is not a separate node, but the reference to the first node. If the list is empty then the head is a null reference. A linked list is a dynamic data structure.

What does head node contain?

Conventionally the head node is the very first node of the linked list but from a "type" point of view it is no different than any other node. It does contain data as well as a pointer to the next node (the second node of your linked list, if it exists).


1 Answers

The head of the list refers to the first node of the list. It would make a good name for the variable storing the reference of that node, and I would expect it to contain the null-reference if the list was empty

someLinkedList.head
         |
         |
         v
        ______        ______        ______            
       |    |n|      |    |n|      |    |n|           
       |    |e|      |    |e|      |    |e|           
       | 12 |x| -->  | 34 |x| -->  | 56 |x| --> null
       |    |t|      |    |t|      |    |t|           
       |____|_|      |____|_|      |____|_|           

Depending on context, the tail can refer to different things. The terminology I'm used to says that the tail corresponds to 34 -> 56 -> null in this example, that is, the list that follows the head.

In other contexts, it may be a reference to the last node. In such interpretation, the tail would refer to the 56 node in your example.


Regarding your first edit, which happens to be a completely different question:

A pointer is a value corresponding to a memory address. A reference is value referring to some object (or null). You can't do pointer arithmetic on Java references, but otherwise I'd say they are fairly similar.

What may confuse you, is that variables in Java can never contain objects. Objects always live on the heap, and variables contain primitive data types, or references to objects on the heap.


Regarding your second edit:

In the example you provided, it looks like the add method skips the first element, and in a sense it does. This is because the implementation has a "dummy"-element as the head. Look at the initialization of the head-variable in the constructor:

head = new Node(null);

I can't understand why they've decided to do that. To me it looks plain stupid.

like image 146
aioobe Avatar answered Oct 29 '22 21:10

aioobe