-
Notifications
You must be signed in to change notification settings - Fork 8
/
doublyLL.py
256 lines (216 loc) · 6.58 KB
/
doublyLL.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import os
class _Node:
'''
Creates a Node with three fields:
1. element (accessed using ._element)
2. link (accessed using ._link)
3. prev (accessed using ._prev)
'''
__slots__ = '_element', '_link', '_prev'
def __init__(self, element, link, prev):
'''
Initialses _element, _link and _prev with element, link and prev respectively.
'''
self._element = element
self._link = link
self._prev = prev
class DoublyLL:
'''
Consists of member funtions to perform different
operations on the doubly 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 doubly linked list is empty, otherwise False.
'''
return self._size == 0
def addLast(self, e):
'''
Adds the passed element at the end of the doubly linked list.
'''
newest = _Node(e, None, None)
if self.isempty():
self._head = newest
else:
self._tail._link = newest
newest._prev = self._tail
self._tail = newest
self._size += 1
def addFirst(self, e):
'''
Adds the passed element at the beginning of the doubly linked list.
'''
newest = _Node(e, None, None)
if self.isempty():
self._head = newest
self._tail = newest
else:
newest._link = self._head
self._head._prev = newest
self._head = newest
self._size += 1
def addAnywhere(self, e, index):
'''
Adds the passed element at the passed index position of the doubly linked list.
'''
if index >= self._size:
print(
f'Index value out of range, it should be between 0 - {self._size - 1}')
elif self.isempty():
print("List was empty, item will be added at the end")
self.addLast(e)
elif index == 0:
self.addFirst(e)
elif index == self._size - 1:
self.addLast(e)
else:
newest = _Node(e, None, None)
p = self._head
for _ in range(index - 1):
p = p._link
newest._link = p._link
p._link._prev = newest
newest._prev = p
p._link = newest
self._size += 1
def removeFirst(self):
'''
Removes element from the beginning of the doubly linked list.
Returns the removed element.
'''
if self.isempty():
print('List is already empty')
return
e = self._head._element
self._head = self._head._link
self._size -= 1
if self.isempty():
self._tail = None
else:
self._head._prev = None
return e
def removeLast(self):
'''
Removes element from the end of the doubly linked list.
Returns the removed element.
'''
if self.isempty():
print("List is already empty")
return
e = self._tail._element
self._tail = self._tail._prev
self._size -= 1
if self.isempty():
self._head = None
else:
self._tail._link = None
return e
def removeAnywhere(self, index):
'''
Removes element from the passed index position of the doubly linked list.
Returns the removed element.
'''
if index >= self._size:
print(
f'Index value out of range, it should be between 0 - {self._size - 1}')
elif self.isempty():
print("List is empty")
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
p._link._prev = p
self._size -= 1
return e
def display(self):
'''
Utility function to display the doubly linked list in
both the directions i.e. forward and reverse.
'''
if self.isempty():
print("List is Empty")
return
print("Forward direction")
p = self._head
print("NULL<-->", end='')
while p:
print(p._element, end="<-->")
p = p._link
print("NULL")
print("\nReverse direction")
p = self._tail
print("NULL<-->", end='')
while p:
print(p._element, end="<-->")
p = p._prev
print("NULL")
print(
f"\nHead : {self._head._element}, Tail : {self._tail._element}")
###############################################################################
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: "))
DL.addLast(elem)
print("Added Item at Last!\n\n")
elif choice == 2:
elem = int(input("Enter Item: "))
DL.addFirst(elem)
print("Added Item at First!\n\n")
elif choice == 3:
elem = int(input("Enter Item: "))
index = int(input("Enter Index: "))
DL.addAnywhere(elem, index)
elif choice == 4:
print("Removed Element from First:", DL.removeFirst())
elif choice == 5:
print("Removed Element from last:", DL.removeLast())
elif choice == 6:
index = int(input("Enter Index: "))
print(f"Removed Item: {DL.removeAnywhere(index)} !\n\n")
elif choice == 7:
print("List:")
DL.display()
print("\n")
elif choice == 8:
import sys
sys.exit()
###############################################################################
if __name__ == '__main__':
DL = DoublyLL()
while True:
choice = options()
switch_case(choice)