user42155
user42155

Reputation: 49665

Head node in linked lists

I have problems with understanding what is the nature of the first node, or the so called head, in a linked list data structure. A linked list consists of nodes, and each node contains some data and a link to another node in the list. But is the very first node a node which contains data and a link to the second node? Or is it contain only a link (and no data) to a node? I thought that the very first node in a linked list has both data and a link to another node, but in one introductory book it is explained that the head is a node but a link that gets you to the first node. At the same time head is a variable of the type of the node. Why is it like this? (I am working in Java, if that matters). Thanks you.

Upvotes: 10

Views: 24090

Answers (7)

Celeste
Celeste

Reputation: 1

To add a head node to a linked-list is good because you no longer need to specialize your method when inserting or deleting the first node, you can deal with them just the way you do it with other nodes.

Upvotes: 0

Punith Raj
Punith Raj

Reputation: 2195

Think of it like a Node is a object which has a variable "data" to hold the value and another variable "next" which points to another Node object to make a link. see the below java class.

public class ListNode {
private int data;
private ListNode next = null;

public ListNode() {
    // TODO Auto-generated constructor stub
}
//constructor for creating a listNode
public ListNode(int data){
    this.data = data;
}
/*below are setters and getters  */
public void setData(int data){
    this.data = data;
}
public int getData(){
    return this.data;
}
public void setNext(ListNode next){
    this.next = next;
}
public ListNode getNext(){
    return this.next;
}
}

and this is how i would link it.

//create 3 list node objects
    ListNode one = new ListNode(1);
    ListNode two = new ListNode(2);
    ListNode three = new ListNode(3);

    one.setNext(two);
    two.setNext(three);

But note we need to know the head node for any further manipulation like adding a ListNode at end, beginning or at a random place in the List.

a Head Node is one in our case from where the list chain has started.

Hope it clears :)

Thanks Punith

Upvotes: 1

svKris
svKris

Reputation: 783

A @falmarri answered, you can implement it either way. Head node is similar to any other node in a Singly Linked list. It is just a starting node with which we can traverse the rest of the linked list. We can implement head as either a node or as a pointer.

Upvotes: 2

Michael M. Adkins
Michael M. Adkins

Reputation: 175

If this was in C++, it might be easier for you to understand, but a header node is more just a reference that you use to gain the initial access to the entire list. It references the first "complete" node in the list which contains its data item within the list's data set and a reference to the next node. If this was C++, a node would really be just a "pointer" to a data structure, which is just memory address for the compiler. If you didn't have a header node that pointed to your linked-list, then it would be lost in the "ether" never to be accessed again - while still taking up space in memory.

Since Java takes care of this for you behind the scenes, you don't actually have to worry about pointers. But, that could be the reason behind your confusion. Since, so much detail is hidden, without any prior knowledge behind the concepts you would have to just accept the syntax rules without knowing the reasons behind them.

In concept, arrays are a type of list, just like linked-lists are also a type of list. Arrays are placed in sequential order in memory whereas linked lists are not. An array's member only references its data item but since the members are placed where they are in memory, you can just access them by adding an integer value multiplied by the size of that data item's data type to the reference for the entire array. This differs from linked-lists by the fact that linked lists aren't placed in sequential order and thus a more complex data structure must be used to make up the list - i.e. a "node".

But, since the linked list isn't placed in any kind of order in memory, the compiler doesn't have to set aside a chunk of data for it and thus is why it can have any length you want it to without having to recreate a new one every time you change its length. This differs from an array since arrays' lengths are static. Additionally, an array is referenced by its first member, that is (for an array named "a") this would be "a[0]" or the equivalent "a" if this were C. However, a linked list doesn't work like this and is why you have a "header" or "dummy" node to reference the entire thing.

Another thing about the header node is that when performing various operations on a linked-list, you can also create a node that's similar in structure to your "head node" to help in performing your operation on the list.

Upvotes: 0

ACoolie
ACoolie

Reputation: 1439

These are called "dummy" header nodes, and they allow you to write general code that works for empty and non-empty lists.

Regularly, if you want to insert a Node at the end of your list, you need two cases. If head is null, indicating the list is empty, then you would set head to the new Node. If head is not null, then you follow the next pointers until you have the last Node, and set the next pointer to the new Node.

However, if you use a dummy header, head will never be null. It will be a Node with a null next pointer, which you can point to your new Node, just how you would if the list did contain nodes.

Upvotes: 9

Jonathan Wood
Jonathan Wood

Reputation: 67175

A head node is normally like any other node except that it comes logically at the start of the list, and no other nodes point to it (unless you have a doubly-linked list).

Since no other node points to it, and you also need an easy way to find the start of the list, it is common to store a separate pointer to a node that points to the head node. This pointer is not a node-itself, just a pointer to one.

Upvotes: 0

Falmarri
Falmarri

Reputation: 48559

You can implement it either way. A first node that has no data and just a link would be a pretty weird implementation though.

Upvotes: 0

Related Questions