This program is about circular single linked list. In circular single linked list as the last node contains the address of first node there will be no concept of Null. Either for deletion or insertion it is necessary to find the last node in this linked list except for insertion and deletion at intermediate position.
The program covers the following functions:
1.Creation of circular linked list
2.Insertion of node at beginning
3.Insertion of node at the end
4.Insertion of node at intermediate position
5.Deletion of node at beginning
6.Deletion of node at the end
7.Deletion of node at intermediate position
8.Count of nodes
9.Display of nodes
class Node:
def __init__(self,data):
self.data = data
self.next = None
class Csll:
def __init__(self):
self.start = None
def createlist(self):
n=int(input("enter no of nodes"))
for i in range(n):
data = int(input("enter value"))
newnode = Node(data)
if self.start == None:
self.start = newnode
else:
temp=self.start
while temp.next != None:
temp=temp.next
temp.next=newnode
newnode.next=self.start
def insertend(self):
data=int(input("enter value"))
newnode = Node(data)
if self.start == None:
self.start = newnode
else:
temp=self.start
while temp.next != self.start:
temp=temp.next
temp.next=newnode
newnode.next=self.start
def insertmid(self):
data=int(input("enter value"))
newnode = Node(data)
pos=int(input("enter position"))
c=self.count()
if pos>1 and pos<=c:
temp=self.start
prev=temp
i=1
while i<pos:
prev=temp
temp=temp.next
i=i+1
prev.next=newnode
newnode.next=temp
else:
print("check position")
def count(self):
nc=1
temp=self.start
while temp.next!=self.start:
nc=nc+1
temp=temp.next
print("number of nodes till now:%d" %nc)
return nc
def deletemid(self):
count=1
if self.start==None:
print("empty")
else:
position=int(input("enter position"))
c=self.count()
if position>c:
print("check position")
if position>1 and position<c:
temp=prev=self.start
while count<position:
prev=temp
temp=temp.next
count=count+1
prev.next=temp.next
del temp
print("node deleted")
else:
print("check position")
def deleteend(self):
global prev
if self.start == None:
print('empty')
else:
temp=self.start
prev=self.start
while temp.next != self.start:
prev=temp
temp=temp.next
prev.next=self.start
del temp
def insertbegin(self):
data=int(input("enter value"))
newnode = Node(data)
if self.start == None:
self.start = newnode
newnode.next=self.start
else:
temp=self.start
while temp.next!=self.start:
temp=temp.next
newnode.next=self.start
self.start=newnode
temp.next=self.start
def deletebegin(self):
global prev
if self.start == None:
print('empty')
else:
temp=last=self.start
while last.next!=self.start:
last=last.next
self.start=self.start.next
last.next=self.start
del temp
#self.start=newstart
def display(self):
print("elements in single linked list are:")
if self.start == None:
print("empty")
else:
temp=self.start
print ("%d" %(temp.data))
while temp.next != self.start:
temp=temp.next
print ("%d" %(temp.data))
def menu():
print("1.createlist\n2.insertbegin\n3.insertend\n4.insertmid")
print("5.deletebegin\n6.deleteend\n7.deletemid\n8.count\n9.display\n10.exit")
def stop():
print("you are about to terminate the program")
exit(0)
s=Csll()
def default():
print("check your input")
menu()
while True:
menu= {
1: s.createlist,
2: s.insertbegin,
3: s.insertend,
4: s.insertmid,
5: s.deletebegin,
6: s.deleteend,
7: s.deletemid,
8: s.count,
9: s.display,
10: stop}
option = int(input("Please enter your choice"))
menu.get(option,default)()
Output:
Python 3.4.0 (v3.4.0:04f714765c13, Mar 16 2014, 19:24:06) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
1.createlist
2.insertbegin
3.insertend
4.insertmid
5.deletebegin
6.deleteend
7.deletemid
8.count
9.display
10.exit
Please enter your choice1
enter no of nodes3
enter value1
enter value2
enter value3
Please enter your choice9
elements in single linked list are:
1
2
3
Please enter your choice2
enter value4
Please enter your choice9
elements in single linked list are:
4
1
2
3
Please enter your choice3
enter value5
Please enter your choice9
elements in single linked list are:
4
1
2
3
5
Please enter your choice4
enter value6
enter position3
number of nodes till now:5
Please enter your choice9
elements in single linked list are:
4
1
6
2
3
5
Please enter your choice8
number of nodes till now:6
Please enter your choice5
Please enter your choice9
elements in single linked list are:
1
6
2
3
5
Please enter your choice6
Please enter your choice9
elements in single linked list are:
1
6
2
3
Please enter your choice7
enter position2
number of nodes till now:4
node deleted
Please enter your choice9
elements in single linked list are:
1
2
3
Please enter your choice8
number of nodes till now:3
Please enter your choice