-
Notifications
You must be signed in to change notification settings - Fork 8
/
circularLL.py
240 lines (202 loc) · 6.2 KB
/
circularLL.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import os
class _Node:
'''
Creates a Node with two fields:
1. element (accesed using ._element)
2. link (accesed using ._link)
'''
__slots__ = '_element', '_link'
def __init__(self, element, link):
'''
Initialses _element and _link with element and link respectively.
'''
self._element = element
self._link = link
class CicularLL:
'''
Consists of member funtions to perform different
operations on the circular linked list.
'''
def __init__(self):
'''
Initialises head, tail and size with None, None and 0 respectively.
'''
self._head = None
self._tail = None
self._size = 0
def __len__(self):
'''
Returns length of linked list
'''
return self._size
def isempty(self):
'''
Returns True if circular linked list is empty, otherwise False.
'''
return self._size == 0
def addLast(self, e):
'''
Adds the passed element at the end of the circular linked list.
'''
newest = _Node(e, None)
if self.isempty():
self._head = newest
newest._link = self._head
else:
newest._link = self._tail._link
self._tail._link = newest
self._tail = newest
self._size += 1
def addFirst(self, e):
'''
Adds the passed element at the beginning of the circular linked list.
'''
newest = _Node(e, None)
newest._link = self._head
if self.isempty():
self._head = newest
self._tail = newest
else:
self._tail._link = newest
self._head = newest
self._size += 1
def addAnywhere(self, e, index):
'''
Adds the passed element at the passed index position of the circular linked list.
'''
newest = _Node(e, None)
if index >= self._size:
print(
f"Index out of range. It should be between 0 - {self._size - 1}")
elif self.isempty():
print("List was empty, item will be added in the End.")
self.addLast(e)
elif index == 0:
self.addFirst(e)
else:
p = self._head
for _ in range(index - 1):
p = p._link
newest._link = p._link
p._link = newest
print(f"Added Item at index {index}!\n\n")
self._size += 1
def removeFirst(self):
'''
Removes element from the beginning of the circular linked list.
Returns the removed element.
'''
if self.isempty():
print("List is Empty")
return
e = self._head._element
self._head = self._head._link
self._tail._link = self._head
self._size -= 1
return e
def removeLast(self):
'''
Removes element from the end of the circular linked list.
Returns the removed element.
'''
if self.isempty():
print("List is Empty")
return
p = self._head # you can also use self._tail._link as it also points to head node
if p._link == self._head:
return self.removeFirst()
else:
while p._link._link != self._head:
p = p._link
e = p._link._element
p._link = self._head
self._tail = p
self._size -= 1
return e
def removeAnywhere(self, index):
'''
Removes element from the passed index position of the circular linked list.
Returns the removed element.
'''
if index >= self._size:
print(
f"Index out of range. It should be between 0 - {self._size - 1}")
elif self.isempty():
print("List is Empty")
return
elif index == 0:
return self.removeFirst()
elif index == self._size - 1:
return self.removeLast()
else:
p = self._head
for _ in range(index - 1):
p = p._link
e = p._link._element
p._link = p._link._link
self._size -= 1
return e
def display(self):
'''
Utility function to display the circular linked list.
'''
if self.isempty() == 0:
p = self._head
while True:
print(p._element, end='-->')
p = p._link
if p == self._head:
break
print(f'({p._element} head)')
else:
print("Empty")
#########################################################################
def options():
'''
Prints Menu for operations
'''
options_list = ['Add Last', 'Add First', 'Add Anywhere',
'Remove First', 'Remove Last', 'Remove Anywhere',
'Display List', 'Exit']
print("MENU")
for i, option in enumerate(options_list):
print(f'{i + 1}. {option}')
choice = int(input("Enter choice: "))
return choice
def switch_case(choice):
'''
Switch Case for operations
'''
os.system('cls')
if choice == 1:
elem = int(input("Enter Item: "))
CL.addLast(elem)
print("Added Item at Last!\n\n")
elif choice == 2:
elem = int(input("Enter Item: "))
CL.addFirst(elem)
print("Added Item at First!\n\n")
elif choice == 3:
elem = int(input("Enter Item: "))
index = int(input("Enter Index: "))
CL.addAnywhere(elem, index)
elif choice == 4:
print("Removed Element from First:", CL.removeFirst())
elif choice == 5:
print("Removed Element from last:", CL.removeLast())
elif choice == 6:
index = int(input("Enter Index: "))
print(f"Removed Item: {CL.removeAnywhere(index)} !\n\n")
elif choice == 7:
print("List: ", end='')
CL.display()
print("\n")
elif choice == 8:
import sys
sys.exit()
###############################################################################
if __name__ == '__main__':
CL = CicularLL()
while True:
choice = options()
switch_case(choice)