Skip to content
  • Linkedin
  • Youtube
  • Pinterest
  • Home
  • Privacy Policy
  • About
  • Contact
Programmingoneonone

Programmingoneonone

Programmingoneonone is a website that publishes daily tutorials, methods, guides, and articles on IT, Education, and technology.

  • Home
  • Human Values
  • DSA
  • IoT Tutorials
  • Interview Questions and Answers
  • Toggle search form
header linked list in data structure

Header Linked list in Data structure

Posted on 23 May 202023 April 2023 By YASH PAL No Comments on Header Linked list in Data structure

In Data Structure The header linked list is a list that has a header node at the beginning of the linked list. and the head always refers to the header node of the list.

What is Header Node

the info part of the header node is always None. Therefore we can use this part to store some useful information about the linked list. like storing the number of nodes in a list and some of the values of the linked list etc.

and the linked part of the header node is always referred to as the first node of the linked list.

header linked list in data structure

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.

header linked list in DSA

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

Header Linked List program in Python.

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 <= k-1 and p is not None:
            p = p.link
            i += 1
        if p is None:
            print("You can insert only upto ", (i-1), " th position")
        else:
            temp = Node(data)
            temp.link = p.link
            p.link  = temp

    def delete_node(self,data):
        p = self.head
        while p.link is not None:
            if p.link.info == data:
                break            p = p.link

        if p.link == None:
            print(data, " not found")
        else:
            p.link = p.link.link

    def reverse_list(self):
        prev = None        p = self.head.link
        while p is not None:
            next = p.link
            p.link = prev
            prev = p
            p = next

        self.head.link = prev

list = HeaderLinkedList()

list.create_list()

while True:
    print("1. Display list")
    print("2. Insert a node at the end of the list")
    print("3. Insert a node before a specified node")
    print("4. Insert a node at a given position")
    print("5. Delete a node")
    print("6. Reverse the list")
    print("9. Quit")

    option = int(input("Enter your choice : "))

    if option == 1:
        list.display_list()
    elif option == 2:
        data = int(input("Enter the element to be inserted : "))
        list.insert_at_end(data)
    elif option == 3:
        data = int(input("Enter the element to be inserted : "))
        x = int(input("Enter the element before which to insert : "))
        list.insert_before(data,x)
    elif option == 4:
        data = int(input("Enter the element to be inserted : "))
        k = int(input("Enter the position at which to insert : "))
        list.insert_at_position(data,x)
    elif option == 5:
        data = int(input("Enter the element to be deleted : "))
        list.delete_node(data)
    elif option == 7:
        break    else:
        print("Wrong option")
    print()
Computer Science Tutorials, Data Structures Tutorials Tags:computer science, Data Structure

Post navigation

Previous Post: Merge Two Linked List in Data Structure
Next Post: Sorted linked list in Data structure

Related Tutorials

python libraries for text transformation REPHRASING REVOLUTION: HOW PYTHON LIBRARIES FACILITATE TEXT TRANSFORMATION Computer Science Tutorials
Introduction to Statistics for Data Science: Building a Solid Foundation Computer Science Tutorials
programming languages for machine learning and data science Top Programming Languages For Machine Learning Computer Science Tutorials
How to Become a Successful Data Engineer in the Data Science Field – Complete Guide Computer Science Tutorials
Is Python a good language for Machine Learning/AI? Computer Science Tutorials
basics of boolean algebra Its Operators, Laws, and Examples Basics of Boolean Algebra: Its Operators, Laws, and Examples Boolean Algebra

Leave a Reply

You must be logged in to post a comment.

  • Java Interview Questions and Answers (2023)
    Thinking of becoming a Java developer? I must say it’s a good choice! Java is continuously named the most popular programming language. And the...

    Learn More “Java Interview Questions and Answers (2023)” »

  • Iot(Internet of things) in healthcare
    IoT in Healthcare
    IoMT (Internet of Medical Things) stands for devices that can collect and exchange data – either with users or other devices via the internet,...

    Learn More “IoT in Healthcare” »

  • four stages of iot solution for industry
    IoT for Industry
    In this post, we are going to learn about use cases of IoT for Industry and four stages for providing IoT solutions. Machine Diagnosis...

    Learn More “IoT for Industry” »

  • Iot for agricultural
    IoT in Agriculture
    IoT technology has realized smart wearables, connected devices, automated machines, and driverless cars. However, in agriculture, the IoT has brought the greatest impact. Amongst the challenges...

    Learn More “IoT in Agriculture” »

  • Iot for logistics
    IoT in Logistics and Supply Chain
    IoT applications for smart logistics and supply chain systems:  Logistics Fleet Tracking  To track the locations of the vehicles in real time, the vehicle...

    Learn More “IoT in Logistics and Supply Chain” »

  • Algorithms Tutorials
  • Basic Programming
  • Boolean Algebra
  • C Programming Tutorials
  • C++ Tutorials
  • Compiler Design Tutorials
  • Computer Networks Tutorials
  • Computer Organization Tutorials
  • Computer Science Tutorials
  • Data Structures Tutorials
  • DBMS Tutorials
  • Developer Guide
  • Digital Communication
  • Digital Logic Tutorials
  • Internet of Things Tutorials
  • Internet Tutorials
  • Interview questions answers
  • Java Tutorials
  • Javascript Tutorials
  • Linux
  • Machine Learning Tutorials
  • Operating Systems Tutorials
  • Programming Tutorials
  • Projects
  • Python Tutorials
  • Tips&Tricks
  • Tools
  • VBScript Tutorials
  • Java Interview Questions and Answers (2023)
    Thinking of becoming a Java developer? I must say it’s a good choice! Java is continuously named the most popular programming language. And the...

    Learn More “Java Interview Questions and Answers (2023)” »

  • Iot(Internet of things) in healthcare
    IoT in Healthcare
    IoMT (Internet of Medical Things) stands for devices that can collect and exchange data – either with users or other devices via the internet,...

    Learn More “IoT in Healthcare” »

  • four stages of iot solution for industry
    IoT for Industry
    In this post, we are going to learn about use cases of IoT for Industry and four stages for providing IoT solutions. Machine Diagnosis...

    Learn More “IoT for Industry” »

  • Iot for agricultural
    IoT in Agriculture
    IoT technology has realized smart wearables, connected devices, automated machines, and driverless cars. However, in agriculture, the IoT has brought the greatest impact. Amongst the challenges...

    Learn More “IoT in Agriculture” »

  • Iot for logistics
    IoT in Logistics and Supply Chain
    IoT applications for smart logistics and supply chain systems:  Logistics Fleet Tracking  To track the locations of the vehicles in real time, the vehicle...

    Learn More “IoT in Logistics and Supply Chain” »

Copyright © 2023 Programmingoneonone.

Powered by PressBook Blog WordPress theme