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

Data Structures Lab Assignment 7

The document provides instructions for a Data Structures Lab focused on implementing a singly linked list in Python. It includes sample code for creating nodes, linked lists, and methods for inserting nodes at the beginning. Additionally, it outlines a requirement for a menu-driven program to perform operations such as insertion, deletion, and traversal of the linked list.

Uploaded by

prisha.kashyap24
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)
6 views

Data Structures Lab Assignment 7

The document provides instructions for a Data Structures Lab focused on implementing a singly linked list in Python. It includes sample code for creating nodes, linked lists, and methods for inserting nodes at the beginning. Additionally, it outlines a requirement for a menu-driven program to perform operations such as insertion, deletion, and traversal of the linked list.

Uploaded by

prisha.kashyap24
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/ 2

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):
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 the linked list item

while linked_list.head != None:

print(linked_list.head.item, end="->")

linked_list.head = linked_list.head.next

print("NULL")

#Sample code implementation for insertion at beginning


class Node:
# Creating a node
def __init__(self, item):
self.item = item
self.next = None

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)

# Print the linked list item


while linked_list.head != None:
print(linked_list.head.item, end="->")
linked_list.head = linked_list.head.next
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

You might also like