A header linked list is a special type of linked list which consists of a header node in the beginning of the list. The header node can store meta data about the linked list. This type of list is useful when information other than that found in each node is needed. For example, suppose there is an application in which the number of items in a list is often calculated. Usually, a list is always traversed to find the length of the list. However, information can be easily obtained if the current length is maintained in an additional header node.
It is a list whose last node contains the NULL pointer. In the header linked list the start pointer always points to the header node. start -> next = NULL indicates that the header linked list is empty. The operations that are possible on this type of linked list are Insertion, Deletion, and Traversing.
This structure can be implemented using a singly linked list or a doubly linked list. When using a doubly linked list, the header node simplifies bidirectional traversal and manipulation of the list, further enhancing the efficiency of operations like insertion and deletion.
Below is the implementation of header linked list:
C++
// C++ program to implement header linked list.
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
Node(int x) {
data = x;
next = nullptr;
}
};
// Inserts a node into the linked list.
void insert(Node* header, int x) {
Node* curr = header;
while (curr->next != nullptr)
curr = curr->next;
Node* newNode = new Node(x);
curr->next = newNode;
}
void printList(Node* header) {
Node* curr = header->next;
while (curr != nullptr) {
cout << curr->data << " ";
curr = curr->next;
}
cout << endl;
}
int main() {
// Create a hard-coded header linked list:
// header -> 1 -> 2 -> 3 -> 4 -> 5
Node* header = new Node(0);
insert(header, 1);
insert(header, 2);
insert(header, 3);
insert(header, 4);
printList(header);
return 0;
}
C
// C program to implement header linked list.
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* createNode(int newData);
// Inserts a node into the linked list.
void insert(struct Node* header, int x) {
struct Node* curr = header;
while (curr->next != NULL)
curr = curr->next;
struct Node* newNode = createNode(x);
curr->next = newNode;
}
void printList(struct Node* header) {
struct Node* curr = header->next;
while (curr != NULL) {
printf("%d ", curr->data);
curr = curr->next;
}
printf("\n");
}
struct Node* createNode(int newData) {
struct Node* node =
(struct Node*)malloc(sizeof(struct Node));
node->data = newData;
node->next = NULL;
return node;
}
int main() {
// Create a hard-coded header linked list:
// header -> 1 -> 2 -> 3 -> 4
struct Node* header = createNode(0);
insert(header, 1);
insert(header, 2);
insert(header, 3);
insert(header, 4);
printList(header);
return 0;
}
Java
// Java program to implement header linked list.
class Node {
int data;
Node next;
Node(int x) {
data = x;
next = null;
}
}
class Main {
// Inserts a node into the linked list.
static void insert(Node header, int x) {
Node curr = header;
while (curr.next != null)
curr = curr.next;
Node newNode = new Node(x);
curr.next = newNode;
}
static void printList(Node header) {
Node curr = header.next;
while (curr != null) {
System.out.print(curr.data + " ");
curr = curr.next;
}
System.out.println();
}
public static void main(String[] args) {
// Create a hard-coded header linked list:
// header -> 1 -> 2 -> 3 -> 4
Node header = new Node(0);
insert(header, 1);
insert(header, 2);
insert(header, 3);
insert(header, 4);
printList(header);
}
}
Python
# Python program to implement header linked list.
class Node:
def __init__(self, new_data):
self.data = new_data
self.next = None
# Inserts a node into the linked list.
def insert(header, x):
curr = header
while curr.next is not None:
curr = curr.next
new_node = Node(x)
curr.next = new_node
def print_list(header):
curr = header.next
while curr is not None:
print(curr.data, end=" ")
curr = curr.next
print()
if __name__ == "__main__":
# Create a hard-coded header linked list:
# header -> 1 -> 2 -> 3 -> 4
header = Node(0)
insert(header, 1)
insert(header, 2)
insert(header, 3)
insert(header, 4)
print_list(header)
C#
// C# program to implement header linked list.
using System;
class Node {
public int data;
public Node next;
public Node(int x) {
data = x;
next = null;
}
}
class GfG {
// Inserts a node into the linked list.
static void Insert(Node header, int x) {
Node curr = header;
while (curr.next != null)
curr = curr.next;
Node newNode = new Node(x);
curr.next = newNode;
}
static void PrintList(Node header) {
Node curr = header.next;
while (curr != null) {
Console.Write(curr.data + " ");
curr = curr.next;
}
Console.WriteLine();
}
static void Main(string[] args) {
// Create a hard-coded header linked list:
// header -> 1 -> 2 -> 3 -> 4
Node header = new Node(0);
Insert(header, 1);
Insert(header, 2);
Insert(header, 3);
Insert(header, 4);
PrintList(header);
}
}
JavaScript
// JavaScript program to implement header linked list.
class Node {
constructor(new_data) {
this.data = new_data;
this.next = null;
}
}
// Inserts a node into the linked list.
function insert(header, x) {
let curr = header;
while (curr.next !== null)
curr = curr.next;
let newNode = new Node(x);
curr.next = newNode;
}
function printList(header) {
let curr = header.next;
while (curr !== null) {
console.log(curr.data);
curr = curr.next;
}
}
// Create a hard-coded header linked list:
// header -> 1 -> 2 -> 3 -> 4
let header = new Node(0);
insert(header, 1);
insert(header, 2);
insert(header, 3);
insert(header, 4);
printList(header);
A list in which the last node points back to the header node is called circular header linked list. The chains do not indicate first or last nodes. In this case, external pointers provide a frame of reference because last node of a circular linked list does not contain the NULL pointer. The possible operations on this type of linked list are Insertion, Deletion and Traversing.
Below is the implementation of circular header linked list:
C++
// C++ program to implement circular header linked list.
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
Node(int x) {
data = x;
next = nullptr;
}
};
// Inserts a node into the linked list.
void insert(Node* header, int x) {
// If list is empty
if (header->next == nullptr) {
Node* newNode = new Node(x);
header->next = newNode;
newNode->next = newNode;
return;
}
Node* curr = header->next;
while (curr->next != header->next)
curr = curr->next;
Node* newNode = new Node(x);
// Update the previous and new Node
// next pointers
curr->next = newNode;
newNode->next = header->next;
}
void printList(Node* header) {
if (header->next == nullptr)
return;
Node* curr = header->next;
do {
cout << curr->data << " ";
curr = curr->next;
} while (curr != header->next);
cout << endl;
}
int main() {
// Create a hard-coded circular header linked list:
// header -> 1 -> 2 -> 3 -> 4 -> 5
Node* header = new Node(0);
insert(header, 1);
insert(header, 2);
insert(header, 3);
insert(header, 4);
printList(header);
return 0;
}
C
// C program to implement circular header linked list.
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* createNode(int x);
// Inserts a node into the linked list.
void insert(struct Node* header, int x) {
// If list is empty
if (header->next == NULL) {
struct Node* newNode = createNode(x);
header->next = newNode;
newNode->next = newNode;
return;
}
struct Node* curr = header->next;
while (curr->next != header->next)
curr = curr->next;
struct Node* newNode = createNode(x);
// Update the previous and new Node
// next pointers
curr->next = newNode;
newNode->next = header->next;
}
void printList(struct Node* header) {
if (header->next == NULL)
return;
struct Node* curr = header->next;
do {
printf("%d ", curr->data);
curr = curr->next;
} while (curr != header->next);
printf("\n");
}
struct Node* createNode(int x) {
struct Node* node =
(struct Node*)malloc(sizeof(struct Node));
node->data = x;
node->next = NULL;
return node;
}
int main() {
// Create a hard-coded circular header linked list:
// header -> 1 -> 2 -> 3 -> 4
struct Node* header = createNode(0);
insert(header, 1);
insert(header, 2);
insert(header, 3);
insert(header, 4);
printList(header);
return 0;
}
Java
// Java program to implement circular header linked list.
class Node {
int data;
Node next;
Node(int new_data) {
data = new_data;
next = null;
}
}
class GfG {
// Inserts a node into the linked list.
static void insert(Node header, int x) {
// If list is empty
if (header.next == null) {
Node newNode = new Node(x);
header.next = newNode;
newNode.next = newNode;
return;
}
Node curr = header.next;
while (curr.next != header.next)
curr = curr.next;
Node newNode = new Node(x);
// Update the previous and new Node
// next pointers
curr.next = newNode;
newNode.next = header.next;
}
static void printList(Node header) {
if (header.next == null)
return;
Node curr = header.next;
do {
System.out.print(curr.data + " ");
curr = curr.next;
} while (curr != header.next);
System.out.println();
}
public static void main(String[] args) {
// Create a hard-coded circular header linked list:
// header -> 1 -> 2 -> 3 -> 4
Node header = new Node(0);
insert(header, 1);
insert(header, 2);
insert(header, 3);
insert(header, 4);
printList(header);
}
}
Python
# Python program to implement circular
# header linked list.
class Node:
def __init__(self, new_data):
self.data = new_data
self.next = None
# Inserts a node into the linked list.
def insert(header, x):
# If list is empty
if header.next is None:
new_node = Node(x)
header.next = new_node
new_node.next = new_node
return
curr = header.next
while curr.next != header.next:
curr = curr.next
new_node = Node(x)
# Update the previous and new Node
# next pointers
curr.next = new_node
new_node.next = header.next
def print_list(header):
if header.next is None:
return
curr = header.next
while True:
print(curr.data, end=" ")
curr = curr.next
if curr == header.next:
break
print()
if __name__ == "__main__":
# Create a hard-coded circular header linked list:
# header -> 1 -> 2 -> 3 -> 4
header = Node(0)
insert(header, 1)
insert(header, 2)
insert(header, 3)
insert(header, 4)
print_list(header)
C#
// C# program to implement circular header linked list.
using System;
class Node {
public int data;
public Node next;
public Node(int new_data) {
data = new_data;
next = null;
}
}
class GfG {
// Inserts a node into the linked list.
static void Insert(Node header, int x) {
// If list is empty
if (header.next == null) {
Node newHead = new Node(x);
header.next = newHead;
newHead.next = newHead;
return;
}
Node curr = header.next;
while (curr.next != header.next)
curr = curr.next;
Node newNode = new Node(x);
// Update the previous and new Node
// next pointers
curr.next = newNode;
newNode.next = header.next;
}
static void PrintList(Node header) {
if (header.next == null)
return;
Node curr = header.next;
do {
Console.Write(curr.data + " ");
curr = curr.next;
} while (curr != header.next);
Console.WriteLine();
}
static void Main(string[] args) {
// Create a hard-coded circular header linked list:
// header -> 1 -> 2 -> 3 -> 4
Node header = new Node(0);
Insert(header, 1);
Insert(header, 2);
Insert(header, 3);
Insert(header, 4);
PrintList(header);
}
}
JavaScript
// JavaScript program to implement circular
// header linked list.
class Node {
constructor(new_data) {
this.data = new_data;
this.next = null;
}
}
// Inserts a node into the linked list.
function insert(header, x) {
// If list is empty
if (header.next === null) {
let newNode = new Node(x);
header.next = newNode;
newNode.next = newNode;
return;
}
let curr = header.next;
while (curr.next !== header.next)
curr = curr.next;
let newNode = new Node(x);
// Update the previous and new Node
// next pointers
curr.next = newNode;
newNode.next = header.next;
}
function printList(header) {
if (header.next === null)
return;
let curr = header.next;
do {
console.log(curr.data);
curr = curr.next;
} while (curr !== header.next);
}
// Create a hard-coded circular header linked list:
// header -> 1 -> 2 -> 3 -> 4
let header = new Node(0);
insert(header, 1);
insert(header, 2);
insert(header, 3);
insert(header, 4);
printList(header);
Applications of Header Linked List
Header linked lists, which use dummy nodes, simplify complex linked list tasks. For example, when merging two sorted linked lists, a dummy node at the start makes the process smoother by avoiding extra steps for the head node and ensuring all nodes are merged correctly. Similarly, when segregating even and odd nodes in a list, dummy nodes make it easier to organize and join the two lists. These uses show how header linked lists can make linked list operations more straightforward and manageable.