Python program to search an element in doubly linked list

1
403
Python program to search an element in doubly linked list

When it is necessary to search for an element in a doubly linked list, a ‘Node’ class should be created. Python Program This class has three properties, data that exists in a node, access to the next node in the linked list, and access to the previous node in the linked list.

You need to create another class that has an initialization function, and the node header will be initialized to ‘None’ within it.

Another Program:- Python Program to Display the multiplication Table 13

The user defines various methods for adding nodes to the linked list, displaying the nodes, and searching for a specific node in the linked list.

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

Python program to search an element in doubly linked list

doubly linked list Source Code

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)

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

Algorithm

Define a node class that represents a node in the list. It will have three properties: data, previous, which will point to the previous node, and next, which will point to the next node. Define another class to create a doubly linked list that has two nodes: head and tail. Initially head and tail will point to zero.

Another Program:- Python Program to Find the Square Root

Explanation

  • The class ‘Node’ has been created.
  • Another class is created with the required attributes.
  • Another method called ‘add_data’ is defined, which is used to add data to the circular linked list.
  • Another method called ‘search_node’ is defined, which takes a parameter to search in the doubly linked list.
  • Find the element and return the index.
  • Another method called ‘print_it’ is defined which is used to display linked list data in the console.
  • An object of class ‘double_list’ is created and methods are called to add data.
  • The ‘search_node’ method is called.
  • Iterates through the nodes in the linked list and return the index of the element if found.
  • It is displayed in the console using the ‘print_it’ method

Gets tech update

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here