Wa0001
Wa0001
AIM:
To develop a program in python that implements basic operations on a
singly linked list.
ALGORITHM:
1. Define a class Node to represent a node in the linked list. Each node has a
data element and a reference to the next node in the list.
2. Define a class LinkedList that contains methods to perform operations on the
linked list:
o __init__: Initialize an empty linked list.
o insert_at_end: Insert a new node at the end of the linked list.
o insert_at_beginning: Insert a new node at the beginning of the linked
list.
o delete_node: Delete a node with a given data value from the linked
list.
o display: Display the elements of the linked list.
PROGRAM:
class Node:
def __init__(self, data=None):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def display(self):
current = self.head
while current:
print(current.data, end=" -> ")
current = current.next
print("None")
linked_list = LinkedList()
linked_list.insert_at_end(1)
linked_list.insert_at_end(2)
linked_list.insert_at_end(3)
linked_list.display()
linked_list.insert_at_beginning(0)
linked_list.display()
linked_list.delete_node(2)
linked_list.display()
SAMPLE OUTPUT: