Python program to search an element in a doubly linked list



When it is required to search for an element in a doubly linked list, a ‘Node’ class needs to be created. In this class, there are three attributes, the data that is present in the node, the access to the next node of the linked list, and the access to the previous node of the linked list.

Another class needs to be created that would have an initialization function, and the head of the node would be initialized to ‘None’ inside this.

Multiple methods are defined by the user to add node to the linked list, to display the nodes it and to search for a specific node in the linked list.

In a doubly linked list, the nodes have pointers. The current node would have a pointer to the next node as well as the previous node. The last value in the list will have ‘NULL’ value in the next pointer. It can be traversed in both the directions.

Below is a demonstration for the same −

Example

 Live Demo

class Node:    def __init__(self, my_data):       self.previous = None       self.data = my_data       self.next = None class double_list:    def __init__(self):       self.head = None       self.tail = None    def add_data(self, my_data):       new_node = Node(my_data)       if(self.head == None):          self.head = self.tail = new_node          self.head.previous = None          self.tail.next = None       else:          self.tail.next = new_node          new_node.previous = self.tail          self.tail = new_node          self.tail.next = None    def print_it(self):       curr = self.head       if (self.head == None):          print("The list is empty")          return       print("The nodes in the doubly linked list are :")       while curr != None:          print(curr.data)          curr = curr.next    def search_node(self, val_to_search):       i = 1;       flag_val = False;       curr = self.head;       if(self.head == None):          print("List is empty")          return       while(curr != None):          if(curr.data == val_to_search):             flag_val = True             break          curr = curr.next          i = i + 1       if(flag_val):          print("The node is present in the list at position : ")          print(i)       else:          print("The node isn't present in the list") my_instance = double_list() print("Elements are being added to the doubly linked list") my_instance.add_data(10) my_instance.add_data(24) my_instance.add_data(54) my_instance.add_data(77) my_instance.add_data(24) my_instance.add_data(0) my_instance.print_it() print("The element 77 is being searched... ") my_instance.search_node(77) print("The element 7 is being searched... ") my_instance.search_node(7)

Output

Elements are being added to the doubly linked list The nodes in the doubly linked list are : 10 24 54 77 24 0 The element 77 is being searched... The node is present in the list at position : 4 The element 7 is being searched... The node isn't present in the list

Explanation

  • The ‘Node’ class is created.
  • Another class with required attributes is created.
  • Another method named ‘add_data’ is defined, that is used to add data to the circular linked list.
  • Another method named ‘search_node’ is defined, that takes a parameter that needs to be searched for in the doubly linked list.
  • It searches for the element and returns the index
  • Another method named ‘print_it’ is defined that is used to display the linked list data on the console.
  • An object of the ‘double_list’ class is created, and the methods are called on it to add data.
  • The ‘search_node’ method is called.
  • It iterates through the nodes in the linked list, and gives the index of the element if found.
  • This is displayed on the console using the ‘print_it’ method.
Updated on: 2021-03-11T12:33:43+05:30

558 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements