Certainly! Here's an implementation of a singly linked list in Python with basic operations such as insertion, deletion, and searching:
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def is_empty(self):
return self.head is None
def insert_at_beginning(self, data):
new_node = Node(data)
new_node.next = self.head
self.head = new_node
def insert_at_end(self, data):
new_node = Node(data)
if self.head is None:
self.head = new_node
return
last = self.head
while last.next:
last = last.next
last.next = new_node
def delete(self, key):
temp = self.head
# If the key to be deleted is at the head
if temp is not None:
if temp.data == key:
self.head = temp.next
temp = None
return
# Search for the key to be deleted, keep track of the previous node as well
while temp is not None:
if temp.data == key:
break
prev = temp
temp = temp.next
# If key was not present in linked list
if temp == None:
return
# Unlink the node from the linked list
prev.next = temp.next
temp = None
def search(self, key):
current = self.head
while current is not None:
if current.data == key:
return True
current = current.next
return False
def display(self):
current = self.head
while current:
print(current.data, end=' ')
current = current.next
print()
# Example usage:
if __name__ == '__main__':
linked_list = LinkedList()
# Insert elements
linked_list.insert_at_end(1)
linked_list.insert_at_end(2)
linked_list.insert_at_end(3)
# Display the linked list
print("Linked list:")
linked_list.display() # Output: 1 2 3
# Delete an element
linked_list.delete(2)
# Display the linked list after deletion
print("Linked list after deletion of 2:")
linked_list.display() # Output: 1 3
# Search for an element
key = 3
if linked_list.search(key):
print(f"{key} found in the linked list.")
else:
print(f"{key} not found in the linked list.")
Explanation:
Node Class (
Node
):- Represents a single node in the linked list containing
data
and a reference to thenext
node.
- Represents a single node in the linked list containing
Linked List Class (
LinkedList
):- Initializes with a
head
pointer set toNone
.
- Initializes with a
Insertion at Beginning (
insert_at_beginning
method):- Inserts a new node with
data
at the beginning of the linked list. Adjustsself.head
to point to the new node.
- Inserts a new node with
Insertion at End (
insert_at_end
method):- Inserts a new node with
data
at the end of the linked list. Traverses the list to find the last node and appends the new node there.
- Inserts a new node with
Deletion (
delete
method):- Deletes the node with
data
equal tokey
from the linked list. Handles cases where the node to be deleted is at the beginning or somewhere in the middle of the list.
- Deletes the node with
Search (
search
method):- Searches for a node with
data
equal tokey
in the linked list. ReturnsTrue
if found, otherwiseFalse
.
- Searches for a node with
Display (
display
method):- Prints the elements of the linked list starting from
self.head
.
- Prints the elements of the linked list starting from
Example Usage:
- Demonstrates how to create a linked list, insert elements, delete an element, and search for an element.
This implementation provides the basic functionalities required for a singly linked list in Python. Adjustments can be made based on specific requirements or additional operations needed for the linked list.