100% found this document useful (1 vote)
52 views

Assignment 7

This document contains code for a program that implements a singly linked list with functions to insert elements at the beginning, end, and specified positions, as well as delete elements from specified positions. The code defines Node and LinkedList classes, with methods for insertion like At_Begin, Insert, and At_End, and deletion with Delete. It tests the functions by prompting the user for insertions and deletions and printing the final list.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
52 views

Assignment 7

This document contains code for a program that implements a singly linked list with functions to insert elements at the beginning, end, and specified positions, as well as delete elements from specified positions. The code defines Node and LinkedList classes, with methods for insertion like At_Begin, Insert, and At_End, and deletion with Delete. It tests the functions by prompting the user for insertions and deletions and printing the final list.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

ASSIGNMENT 7: SINGLY LINKED LIST

NAME: KINGSHUK MUKHERJEE


ROLL NO: 19CE8040
SUBJECT CODE: CSC432
DEPARTMENT: CIVIL ENGINEERING
DATE: 5-4-2021

QUESTION: Write a program using linked list to


insert elements at the beginning, at the end, at any
specified position and
also delete the elements from specified positions.

CODE:

class Node:
def __init__(self, data):
self.data = data
self.next = None

class LinkedList:
def __init__(self):
self.head = None
self.next = None

def At_Begin(self, new_data):


new_node = Node(new_data)
new_node.next = self.head
self.head = new_node

def Insert(self, n, p):


temp = Node(n)
if p == 0 or self.head == None:
temp.next = self.head
self.head = temp
return
c = self.head
for i in range(p-1):
c = c.next
if c.next == None:
break
temp.next = c.next
c.next = temp

def At_End(self, new_data):


new_node = Node(new_data)
if self.head is None:
self.head = new_node
return
last = self.head
while (last.next):
last = last.next
last.next = new_node

def Delete(self, p):


if self.head == None:
return
temp = self.head
if p == 0:
self.head = temp.next
temp = None
return
for i in range(p - 1):
temp = temp.next
if temp is None:
break
if temp is None:
return
if temp.next is None:
return
next = temp.next.next
temp.next = None
temp.next = next

def Print(self):
temp = self.head
while (temp):
print(temp.data)
temp = temp.next

l = LinkedList()
n = int(input("Enter The Total nos : "))
for i in range(n):
s = input("\nWhich insert operation you want to do ? : \nAt Begin -- (1)
/ Anywhere -- (2) / At End -- (3) : ")
no = int(input("Enter Any No. : "))
if (s.strip()).lower() == '1' :
l.At_Begin(no)
if (s.strip()).lower() == '2':
p = int(input("Enter The Position of Insertion : "))
l.Insert(no,p-1)
elif (s.strip()).lower() == '3' :
l.At_End(no)
print("\nLinked List after Insertion : ")
l.Print()
g = int(input("\nEnter The No of Elements to be deleted : "))
if g>n:
g = n
for i in range(g):
p = int(input("Enter The Position of Deletion : "))
l.Delete(p-1)
n -= 1
print("Only",n,"Elements are left\n")
print("\nLinked List after Deletion : ")
l.Print()

OUTPUT:

You might also like