Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python program to search an element in a doubly linked list
When searching for an element in a doubly linked list, we need to create a Node class with three attributes: the data, a pointer to the next node, and a pointer to the previous node. We also need a main class to manage the list operations including adding nodes, displaying nodes, and searching for specific elements.
In a doubly linked list, each node has bidirectional pointers − one pointing to the next node and another to the previous node. This allows traversal in both directions, making it more flexible than a singly linked list.
Node Class Structure
The Node class represents individual elements in the doubly linked list ?
class Node:
def __init__(self, my_data):
self.previous = None
self.data = my_data
self.next = None
Doubly Linked List Implementation
Here's the complete implementation with search functionality ?
class Node:
def __init__(self, my_data):
self.previous = None
self.data = my_data
self.next = None
class DoublyLinkedList:
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_list(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):
position = 1
found = False
curr = self.head
if self.head == None:
print("List is empty")
return
while curr != None:
if curr.data == val_to_search:
found = True
break
curr = curr.next
position += 1
if found:
print(f"The node is present in the list at position: {position}")
else:
print("The node isn't present in the list")
# Create doubly linked list instance
dll = DoublyLinkedList()
print("Elements are being added to the doubly linked list")
dll.add_data(10)
dll.add_data(24)
dll.add_data(54)
dll.add_data(77)
dll.add_data(24)
dll.add_data(0)
dll.print_list()
print("The element 77 is being searched...")
dll.search_node(77)
print("The element 7 is being searched...")
dll.search_node(7)
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
How the Search Works
The search algorithm follows these steps:
- Initialize − Start from the head node and set position counter to 1
- Traverse − Move through each node comparing data with the target value
- Track Position − Increment position counter for each node visited
- Return Result − Return position if found, otherwise indicate element not found
Key Features
| Feature | Description | Time Complexity |
|---|---|---|
| Search | Linear search from head to tail | O(n) |
| Insertion | Add new node at the end | O(1) |
| Traversal | Forward or backward direction | O(n) |
Conclusion
Searching in a doubly linked list requires traversing nodes from head to tail until the target element is found. The search operation has O(n) time complexity, making it suitable for small to medium-sized datasets where bidirectional traversal is beneficial.
