Data Structures Lab Assignment 7
Data Structures Lab Assignment 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.
class Node:
# Creating a node
def __init__(self, item):
self.item = item
self.next = None
class LinkedList:
def __init__(self):
self.head = None
linked_list = LinkedList()
linked_list.head = Node(1)
second = Node(2)
third = Node(3)
linked_list.head.next = second
second.next = third
print(linked_list.head.item, end="->")
linked_list.head = linked_list.head.next
print("NULL")
class LinkedList:
def __init__(self):
self.head = None
def insertathead(self,val):
new_node = Node(val)
if self.head=None:
self.head=new_node
else:
new_node.next=self.head
self.head=new_node
linked_list = LinkedList()
linked_list.insertathead(10)
linked_list.insertathead(20)
Question: Write a menu driven program to implement a singly linked list having the following operations:
- Insert at beginning
- Delete at beginning