Data Structures Lab 7:
Instructions
1. You should create the node object and the linked list as mentioned in the sample code.
2. Consider all the possible test cases.
#Sample code to understand the working of linked list
class Node:
# Creating a node
def __init__(self, item):
[Link] = item
[Link] = None
class LinkedList:
def __init__(self):
[Link] = None
linked_list = LinkedList()
linked_list.head = Node(1)
second = Node(2)
third = Node(3)
linked_list.[Link] = second
[Link] = third
# Print the linked list item
while linked_list.head != None:
print(linked_list.[Link], end="->")
linked_list.head = linked_list.[Link]
print("NULL")
#Sample code implementation for insertion at beginning
class Node:
# Creating a node
def __init__(self, item):
[Link] = item
[Link] = None
class LinkedList:
def __init__(self):
[Link] = None
def insertathead(self,val):
new_node = Node(val)
if [Link]=None:
[Link]=new_node
else:
new_node.next=[Link]
[Link]=new_node
linked_list = LinkedList()
linked_list.insertathead(10)
linked_list.insertathead(20)
# Print the linked list item
while linked_list.head != None:
print(linked_list.[Link], end="->")
linked_list.head = linked_list.[Link]
print("NULL")
Question: Write a menu driven program to implement a singly linked list having the following operations:
- Insert at beginning
- Delete at beginning
- Traverse the linked list