0% found this document useful (0 votes)
15 views

DS Lab Assignment1

Uploaded by

bchgpdqk57
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

DS Lab Assignment1

Uploaded by

bchgpdqk57
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

Write a program to implement singly linked list as an ADT that


supports the following operations:
i. Insert an element x at the beginning of the singly linked list
ii. Insert an element x at ith position in the singly linked list
iii. Remove an element from the beginning of the doubly linked list
iv. Remove an element from ith position in the singly linked list.
v. Remove an element from the beginning of the singly linked list
vi. Search for an element x in the singly linked list and return its
pointer

CODE
class Node:
"""A class to represent a node in a singly linked list."""
def __init__(self, data):
self.data = data
self.next = None

class SinglyLinkedList:
"""A class to represent a singly linked list."""
def __init__(self):
self.head = None

def insert_at_beginning(self, x):


"""Insert an element at the beginning of the list."""
new_node = Node(x)
new_node.next = self.head
self.head = new_node
print(f"Inserted {x} at the beginning.")

def insert_at_position(self, x, position):


"""Insert an element at the ith position in the list."""
if position < 0:
print("Invalid position!")
return

new_node = Node(x)

if position == 0:
new_node.next = self.head
self.head = new_node
print(f"Inserted {x} at position {position}.")
return

current = self.head
count = 0
while current is not None and count < position - 1:
current = current.next
count += 1

if current is None:
print(f"Position {position} is out of bounds.")
else:
new_node.next = current.next
current.next = new_node
print(f"Inserted {x} at position {position}.")

def remove_from_beginning(self):
"""Remove an element from the beginning of the list."""
if self.head is None:
print("List is empty! Nothing to remove.")
return

removed_data = self.head.data
self.head = self.head.next
print(f"Removed {removed_data} from the beginning.")

def remove_from_position(self, position):


"""Remove an element from the ith position in the list."""
if position < 0 or self.head is None:
print("Invalid position or empty list!")
return

if position == 0:
removed_data = self.head.data
self.head = self.head.next
print(f"Removed {removed_data} from position {position}.")
return

current = self.head
count = 0
while current.next is not None and count < position - 1:
current = current.next
count += 1

if current.next is None:
print(f"Position {position} is out of bounds.")
else:
removed_data = current.next.data
current.next = current.next.next
print(f"Removed {removed_data} from position {position}.")

def search(self, x):


"""Search for an element in the list and return its pointer."""
current = self.head
position = 0
while current is not None:
if current.data == x:
print(f"Element {x} found at position {position}.")
return current
current = current.next
position += 1

print(f"Element {x} not found in the list.")


return None

def display(self):
"""Display the elements of the list."""
if self.head is None:
print("The list is empty.")
return

elements = []
current = self.head
while current is not None:
elements.append(current.data)
current = current.next

print("List:", " -> ".join(map(str, elements)))

# Example usage of the singly linked list


if __name__ == "__main__":
sll = SinglyLinkedList()
sll.insert_at_beginning(10)
sll.insert_at_beginning(5)
sll.insert_at_position(20, 1)
sll.insert_at_position(30, 3)
sll.display()
sll.remove_from_beginning()
sll.remove_from_position(1)
sll.display()
sll.search(20)
sll.search(100)
OUTPUT

You might also like