Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 96365ff

Browse files
Refactored code
1 parent 29938c1 commit 96365ff

File tree

3 files changed

+139
-18
lines changed

3 files changed

+139
-18
lines changed

‎Linked List/circularLL.py‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,31 @@
11
import os
2+
3+
24
class _Node:
35
'''
46
Creates a Node with two fields:
57
1. element (accesed using ._element)
68
2. link (accesed using ._link)
79
'''
810
__slots__ = '_element', '_link'
11+
912
def __init__(self, element, link):
1013
'''
1114
Initialses _element and _link with element and link respectively.
1215
'''
1316
self._element = element
1417
self._link = link
1518

19+
1620
class CicularLL:
1721
'''
1822
Consists of member funtions to perform different
1923
operations on the circular linked list.
2024
'''
25+
2126
def __init__(self):
2227
'''
23-
Initialses head, tail and size with None, None and 0 respectively.
28+
Initialises head, tail and size with None, None and 0 respectively.
2429
'''
2530
self._head = None
2631
self._tail = None
@@ -67,7 +72,6 @@ def addFirst(self, e):
6772
else:
6873
self._tail._link = newest
6974
self._head = newest
70-
7175
self._size += 1
7276

7377
def addAnywhere(self, e, index):
@@ -90,8 +94,7 @@ def addAnywhere(self, e, index):
9094
newest._link = p._link
9195
p._link = newest
9296
print(f"Added Item at index {index}!\n\n")
93-
94-
self._size += 1
97+
self._size += 1
9598

9699
def removeFirst(self):
97100
'''
@@ -138,7 +141,6 @@ def removeAnywhere(self, index):
138141
if index >= self._size:
139142
print(
140143
f"Index out of range. It should be between 0 - {self._size - 1}")
141-
return
142144
elif self.isempty():
143145
print("List is Empty")
144146
return
@@ -152,8 +154,7 @@ def removeAnywhere(self, index):
152154
p = p._link
153155
e = p._link._element
154156
p._link = p._link._link
155-
156-
self._size -= 1
157+
self._size -= 1
157158
return e
158159

159160
def display(self):
@@ -173,6 +174,7 @@ def display(self):
173174

174175
#########################################################################
175176

177+
176178
def options():
177179
'''
178180
Prints Menu for operations
@@ -230,7 +232,8 @@ def switch_case(choice):
230232

231233
###############################################################################
232234

235+
233236
CL = CicularLL()
234237
while True:
235238
choice = options()
236-
switch_case(choice)
239+
switch_case(choice)

‎Linked List/doublyLL.py‎

Lines changed: 119 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,54 @@
11
import os
2-
fromtypingimportNewType, Sized
2+
33

44
class _Node:
5+
'''
6+
Creates a Node with three fields:
7+
1. element (accessed using ._element)
8+
2. link (accessed using ._link)
9+
3. prev (accessed using ._prev)
10+
'''
511
__slots__ = '_element', '_link', '_prev'
12+
613
def __init__(self, element, link, prev):
14+
'''
15+
Initialses _element, _link and _prev with element, link and prev respectively.
16+
'''
717
self._element = element
818
self._link = link
919
self._prev = prev
10-
20+
21+
1122
class DoublyLL:
23+
'''
24+
Consists of member funtions to perform different
25+
operations on the doubly linked list.
26+
'''
27+
1228
def __init__(self):
29+
'''
30+
Initialises head, tail and size with None, None and 0 respectively.
31+
'''
1332
self._head = None
1433
self._tail = None
1534
self._size = 0
16-
35+
1736
def __len__(self):
37+
'''
38+
Returns length of linked list
39+
'''
1840
return self._size
19-
41+
2042
def isempty(self):
43+
'''
44+
Returns True if doubly linked list is empty, otherwise False.
45+
'''
2146
return self._size == 0
2247

2348
def addLast(self, e):
49+
'''
50+
Adds the passed element at the end of the doubly linked list.
51+
'''
2452
newest = _Node(e, None, None)
2553

2654
if self.isempty():
@@ -32,6 +60,9 @@ def addLast(self, e):
3260
self._size += 1
3361

3462
def addFirst(self, e):
63+
'''
64+
Adds the passed element at the beginning of the doubly linked list.
65+
'''
3566
newest = _Node(e, None, None)
3667

3768
if self.isempty():
@@ -44,8 +75,12 @@ def addFirst(self, e):
4475
self._size += 1
4576

4677
def addAnywhere(self, e, index):
78+
'''
79+
Adds the passed element at the passed index position of the doubly linked list.
80+
'''
4781
if index >= self._size:
48-
print(f'Index value out of range, it should be between 0 - {self._size - 1}')
82+
print(
83+
f'Index value out of range, it should be between 0 - {self._size - 1}')
4984
elif self.isempty():
5085
print("List was empty, item will be added at the end")
5186
self.addLast(e)
@@ -62,25 +97,100 @@ def addAnywhere(self, e, index):
6297
p._link._prev = newest
6398
newest._prev = p
6499
p._link = newest
65-
self._size += 1
100+
self._size += 1
101+
102+
def removeFirst(self):
103+
'''
104+
Removes element from the beginning of the doubly linked list.
105+
Returns the removed element.
106+
'''
107+
if self.isempty():
108+
print("List is already empty")
109+
return
110+
e = self._head._element
111+
self._head = self._head._link
112+
self._size -= 1
113+
114+
if self.isempty():
115+
self._tail = None
116+
else:
117+
self._head._prev = None
118+
return e
119+
120+
def removeLast(self):
121+
'''
122+
Removes element from the end of the doubly linked list.
123+
Returns the removed element.
124+
'''
125+
if self.isempty():
126+
print("List is already empty")
127+
return
128+
e = self._tail._element
129+
self._tail = self._tail._prev
130+
self._size -= 1
66131

132+
if self.isempty():
133+
self._head = None
134+
else:
135+
self._tail._link = None
136+
137+
return e
138+
139+
def removeAnywhere(self, index):
140+
'''
141+
Removes element from the passed index position of the doubly linked list.
142+
Returns the removed element.
143+
'''
144+
if index >= self._size:
145+
print(
146+
f'Index value out of range, it should be between 0 - {self._size - 1}')
147+
elif self.isempty():
148+
print("List is empty")
149+
elif index == 0:
150+
return self.removeFirst()
151+
elif index == self._size - 1:
152+
return self.removeLast()
153+
else:
154+
p = self._head
155+
for _ in range(index - 1):
156+
p = p._link
157+
e = p._link._element
158+
p._link = p._link._link
159+
p._link._prev = p
160+
self._size -= 1
161+
return e
67162

68163
def display(self):
164+
'''
165+
Utility function to display the doubly linked list in
166+
both the directions i.e. forward and reverse.
167+
'''
69168
if self.isempty():
70169
print("List is Empty")
71170
return
72-
171+
172+
print("Forward direction")
73173
p = self._head
74174
print("NULL<-->", end='')
75175
while p:
76176
print(p._element, end="<-->")
77177
p = p._link
78178
print("NULL")
179+
180+
print("\nReverse direction")
181+
p = self._tail
182+
print("NULL<-->", end='')
183+
while p:
184+
print(p._element, end="<-->")
185+
p = p._prev
186+
print("NULL")
187+
79188
print(
80-
f"Head : {self._head._element}, Tail : {self._tail._element}")
189+
f"\nHead : {self._head._element}, Tail : {self._tail._element}")
81190

82191
###############################################################################
83192

193+
84194
def options():
85195
'''
86196
Prints Menu for operations
@@ -128,7 +238,7 @@ def switch_case(choice):
128238
print(f"Removed Item: {DL.removeAnywhere(index)} !\n\n")
129239

130240
elif choice == 7:
131-
print("List: ", end='')
241+
print("List:")
132242
DL.display()
133243
print("\n")
134244

‎Linked List/linkedlist.py‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
11
import os
2+
3+
24
class _Node:
35
'''
46
Creates a Node with two fields:
57
1. element (accesed using ._element)
68
2. link (accesed using ._link)
79
'''
810
__slots__ = '_element', '_link'
11+
912
def __init__(self, element, link):
1013
'''
1114
Initialses _element and _link with element and link respectively.
1215
'''
1316
self._element = element
1417
self._link = link
1518

19+
1620
class LinkedList:
1721
'''
1822
Consists of member funtions to perform different
1923
operations on the linked list.
2024
'''
25+
2126
def __init__(self):
2227
'''
2328
Initialses head, tail and size with None, None and 0 respectively.
@@ -178,6 +183,7 @@ def search(self, key):
178183

179184
###############################################################################
180185

186+
181187
def options():
182188
'''
183189
Prints Menu for operations
@@ -193,6 +199,7 @@ def options():
193199
choice = int(input("Enter choice: "))
194200
return choice
195201

202+
196203
def switch_case(choice):
197204
'''
198205
Switch Case for operations
@@ -244,7 +251,8 @@ def switch_case(choice):
244251

245252
###############################################################################
246253

254+
247255
L = LinkedList()
248256
while True:
249257
choice = options()
250-
switch_case(choice)
258+
switch_case(choice)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /