The ones who are crazy enough to think they can change the world are the ones who do.
- Steve Jobs

Header Linked List

A Header linked list is one more variant of linked list. In Header linked list, we have a special node present at the beginning of the linked list. This special node is used to store number of nodes present in the linked list. In other linked list variant, if we want to know the size of the linked list we use traversal method. But in Header linked list, the size of the linked list is stored in its header itself.

Types of Header linked list

  • Grounded header linked list.
  • Circular header linked list.

Structure - Header Linked List

Structure - Header Linked List
struct node
{
int data;
struct node *next;
};

C program - Header Linked List

Here is the program to demonstrate, how to create and use header linked list.

header-linked-list.c
#include <stdio.h>
#include <malloc.h>
struct node
{
int data;
struct node *next;
};
struct node *start = NULL;
struct node *create_hll(struct node *);
struct node *display(struct node *);
int main()
{
int option;
do
{
printf("\n\n -----Main Menu-----");
printf("\n 1. Create a list");
printf("\n 2. Display the list");
printf("\n 3. Exit");
printf("\n Enter your choice : ");
scanf("%d", &option);
switch(option)
{
case 1:
start = create_hll(start);
printf("\n Header Linked List Created Successfully");
break;
case 2:
start = display(start);
break;
}
}while(option != 3);
return 0;
}
struct node *create_hll(struct node *start)
{
struct node *new_node, *ptr;
int num;
printf("\n Enter -1 to end");
printf("\n Enter the data :");
scanf("%d", &num);
while(num != -1)
{
new_node = (struct node*)malloc(sizeof(struct node));
new_node -> data = num;
new_node -> next = NULL;
if(start == NULL)
{
start = (struct node*)malloc(sizeof(struct node));
start -> next = new_node;
}
else
{
ptr = start;
while(ptr -> next != NULL)
ptr = ptr -> next;
ptr -> next = new_node;
}
printf("\n Enter the data :");
scanf("%d", &num);
}
return start;
}
struct node *display(struct node *start)
{
struct node *ptr;
ptr = start;
ptr = ptr -> next;
while(ptr != NULL)
{
printf("\t %d",ptr -> data);
ptr = ptr -> next;
}
return start;
}
-----Main Menu-----
1. Create a list
2. Display the list
3. Exit
Enter your choice : 1
Enter -1 to end
Enter the data : 5
Enter the data : 1 
Enter the data : 2
Enter the data : -1
Header Linked List Created Successfully

-----Main Menu-----
1. Create a list
2. Display the list
3. Exit the list
Enter your choice : 2
5    1    2

-----Main Menu-----
1. Create a list
2. Display the list
3. Exit the list
Enter your choice : 3

Report Us

We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.

Report

We to update you