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 for an Element in the Linked List without using Recursion
Searching for an element in a linked list is a fundamental operation. This article demonstrates how to implement a non-recursive search algorithm that finds an element and returns its index position in a linked list.
Node Class Definition
First, we create a Node class to represent individual elements in the linked list ?
class Node:
def __init__(self, data):
self.data = data
self.next = None
Linked List Implementation
The linked list class contains methods for adding elements, displaying the list, and searching for elements ?
class LinkedList:
def __init__(self):
self.head = None
self.last_node = None
def add_value(self, data):
if self.last_node is None:
self.head = Node(data)
self.last_node = self.head
else:
self.last_node.next = Node(data)
self.last_node = self.last_node.next
def print_list(self):
current = self.head
while current is not None:
print(current.data)
current = current.next
def find_index(self, key):
current = self.head
index = 0
while current:
if current.data == key:
return index
current = current.next
index += 1
return -1 # Element not found
Complete Example
Here's a complete program demonstrating the search functionality ?
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
self.last_node = None
def add_value(self, data):
if self.last_node is None:
self.head = Node(data)
self.last_node = self.head
else:
self.last_node.next = Node(data)
self.last_node = self.last_node.next
def print_list(self):
current = self.head
while current is not None:
print(current.data)
current = current.next
def find_index(self, key):
current = self.head
index = 0
while current:
if current.data == key:
return index
current = current.next
index += 1
return -1
# Create linked list and add elements
linked_list = LinkedList()
elements = [67, 4, 78, 98, 32, 0, 11, 8]
for element in elements:
linked_list.add_value(element)
print('The linked list is:')
linked_list.print_list()
# Search for specific elements
search_key = 11
index = linked_list.find_index(search_key)
if index == -1:
print(f'{search_key} was not found.')
else:
print(f'Element {search_key} was found at index {index}.')
# Search for non-existent element
search_key = 99
index = linked_list.find_index(search_key)
if index == -1:
print(f'{search_key} was not found.')
else:
print(f'Element {search_key} was found at index {index}.')
The linked list is: 67 4 78 98 32 0 11 8 Element 11 was found at index 6. 99 was not found.
How the Search Algorithm Works
The search algorithm uses an iterative approach instead of recursion:
Start from the head node with index 0
Compare current node's data with the search key
If match found, return the current index
If no match, move to next node and increment index
Continue until element found or end of list reached
Return -1 if element not found
Key Features
| Feature | Description |
|---|---|
| Time Complexity | O(n) - Linear search |
| Space Complexity | O(1) - No recursion stack |
| Return Value | Index if found, -1 if not found |
Conclusion
This non-recursive search approach is memory-efficient and easy to understand. The algorithm traverses the linked list linearly and returns the index of the found element or -1 if not found.
