-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathMenu_Driven_Code_for_Circular_LinkedList.py
116 lines (95 loc) · 2.97 KB
/
Menu_Driven_Code_for_Circular_LinkedList.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
"""
Author: Himanshu Agarwal
Github: himanshu-03 (http://github.com/himanshu-03)
LinkedIn: agarwal-himanshu (https://linkedin.com/in/agarwal-himanshu)
# Menu Driven Code for Circular Linked List
"""
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.root = None
self.last = None
def insertLeft(self, data):
n = Node(data)
if self.root == None:
self.root = n
self.last = n
self.last.next = self.root
else:
n.next = self.root
self.root = n
self.last.next = self.root
def deleteLeft(self):
if self.root == None:
print('\nLinked List is empty..!!')
else:
temp = self.root
if self.root == self.last:
self.last = self.root = None
else:
self.root = self.root.next
self.last.next = self.root
print('\nDeleted element: ', temp.data)
def insertRight(self, data):
n = Node(data)
if self.root == None:
self.root = n
self.last = n
self.last.next = self.root
else:
self.last.next = n
self.last = n
self.last.next = self.root
def deleteRight(self):
if self.root == None:
print('\nLinked List is empty..!!')
else:
if self.root == self.last:
self.root = self.last = None
else:
temp = self.root
temp2 = self.root
while temp.next != self.root:
temp2 = temp
temp = temp.next
self.last = temp2
temp2.next = self.root
print('\nDeleted element: ', temp.data)
def printList(self):
if self.root == None:
print('\nLinked List is empty..!!')
temp = self.root
print('\nElements in Linked List are: ')
while True:
print('|', temp.data, '| -> ', end='')
temp = temp.next
if temp == self.root:
break
print('None')
print()
o = LinkedList()
while True:
print('----------------------')
print('\n1. Insert from Left\n2. Insert from Right\n3. Delete from Left\n4. Delete from Right\n5. Print Linked List\n0. Exit')
print('----------------------')
ch = int(input('\nEnter your choice: '))
if ch == 1:
data = int(input('\nEnter value to be inserted in left: '))
o.insertLeft(data)
elif ch == 2:
data = int(input('\nEnter value to be inserted in right: '))
o.insertRight(data)
elif ch == 3:
o.deleteLeft()
elif ch == 4:
o.deleteRight()
elif ch == 5:
o.printList()
elif ch == 0:
print('You are out of the program..!!')
break
else:
print('\nWrong Input..\nEnter the correct choice..!!\n')