Open In App

What is Two Way Header Linked List?

Last Updated : 01 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Two-Way Header Linked List, also known as a doubly linked list with a header node, is a type of linked list that contains a special node at the beginning called the header node. This node does not contain any data but serves as a permanent head of the list. The rest of the list consists of nodes that each contain data and two links pointing to the previous and next nodes.

What is Two-Way Header Linked List?

Two-way header linked list is a type of linked list which has a header node at the beginning of the linked list which does not store any data and simply points to the first node of the linked list. All the other nodes have three parts: data, pointer to the previous node and pointer to the next node. Unlike Singly Linked List where each node has one pointer, there are two pointers in each node of Two-Way Header Linked List. The previous pointer of the first node and next pointer of the last node points to NULL.

Structure of a Two-Way Header Linked List:

A two-way header Linked List, like any other Linked List, consists of a series of nodes. Each node contains a data field and two pointers, one pointing to the next node and one pointing to the previous node. However, in a two-way header Linked List, there is an additional node at the beginning of the list, known as the header node. This node does not contain any meaningful data but serves as a marker for the start of the list.

Advantages of Two-Way Header Linked List:

  • A Two-Way Header Linked List can be traversed in both forward and backward directions. 
  • The delete operation in Two-Way Header Linked List is more efficient if a pointer to the node to be deleted is given. 
  • We can quickly insert a new node before a given node. 
  • In a singly linked list, to delete a node, a pointer to the previous node is needed. To get this previous node, sometimes the list is traversed. In Two-Way Header Linked List, we can get the previous node using the previous pointer. 

Disadvantages of Two-Way Header Linked List:

  • Every node of Two-Way Header Linked List requires extra space for a previous pointer. It is possible to implement Two-Way Header Linked List with a single pointer though (See this and this). 
  • All operations require an extra pointer previous to be maintained. For example, in insertion, we need to modify previous pointers together with the next pointers. For example in the following functions for insertions at different positions, we need 1 or 2 extra steps to set the previous pointer.

Applications of Two-Way Header Linked List:

  • It is used by web browsers for backward and forward navigation of web pages 
  • LRU ( Least Recently Used ) / MRU ( Most Recently Used ) Cache are constructed using Doubly Linked Lists. 
  • Used by various applications to maintain undo and redo functionalities. 

Conclusion:

In conclusion, a two-way header Linked List is a specific type of Linked List that includes a header node at the beginning. This header node, while not holding any meaningful data, plays a crucial role in simplifying operations on the list and improving code consistency and readability. Whether you’re implementing a complex algorithm or simply storing data for easy access, understanding the concept and benefits of a two-way header LinkedList can be very beneficial.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads