What is header linked list explain with example?

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.

Header Linked List

  • Post category:Data Structures Using C

A header linked list is a type of linked list that has a header node at the beginning of the list. In a header linked list, HEAD points to the header node instead of the first node of the list.

The header node does not represent an item in the linked list. This data part of this node is generally used to hold any global information about the entire linked list. The next part of the header node points to the first node in the list.

A header linked list can be divided into two types:

  • Grounded header linked list that stores NULL in the last node’s next field.
  • Circular header linked list that stores the address of the header node in the next part of the last node of the list.
Advertisements

Grounded Header Linked List

In this type of Header Linked List, the last node of the list points to NULL or holds the reference to NULL Pointer. The head pointer points to the Header node of the list. If the there is no node to the next of head pointer or head.next equals NULL then we know that the Linked List is empty. The operations performed on the Header Linked List are same as Singly Linked List such as Insertion, Deletion, and Traversal of nodes. Let us understand this with an example:




Advantages of Header linked list

If the list is empty then always the header node is available in the linked list. so we don't need to check whether the linked list is empty or not.


we can easily perform operations like insertion and deletion because we don't need to check if the list is empty or not.

Python program for Implementation of Header linked list

class Node[object]: def __init__[self,value]: self.info = value self.link = None class HeaderLinkedList[object]: def __init__[self]: self.head = Node[0] def display_list[self]: if self.head.link == None: print["List is empty"] return p =self.head.link print["List is : "] while p is not None: print[p.info, " ",end=''] p = p.link print[] def create_list[self]: n = int[input["Enter the number of nodes : "]] for i in range[n]: data = int[input["Enter the element to be inserted : "]] self.insert_at_end[data] def insert_at_end[self,data]: temp = Node[data] p = self.head while p.link is not None: p = p.link p.link = temp def insert_before[self,data,x]: p = self.head while p.link is not None: if p.link.info == x: break p = p.link if p.link is None: print[x, " not present in the list"] else: temp = Node[data] temp.link = p.link p.link = temp def insert_at_position[self,data,x]: p = self.head i = 1 while i

Bài mới nhất

Chủ Đề