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 3dd846f

Browse files
Refactored Code
1 parent 904a188 commit 3dd846f

File tree

2 files changed

+59
-13
lines changed

2 files changed

+59
-13
lines changed

‎Circular Linked List/circularLL.py‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import os
22

3-
43
class _Node:
54
__slots__ = '_element', '_link'
65

76
def __init__(self, element, link):
87
self._element = element
98
self._link = link
109

11-
1210
class CicularLL:
1311
def __init__(self):
1412
self._head = None

‎Linked List/linkedlist.py‎

Lines changed: 59 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,47 @@
11
import os
22
class _Node:
3+
'''
4+
Creates a Node with two fields:
5+
1. element (accesed using ._element)
6+
2. link (accesed using ._link)
7+
'''
38
__slots__ = '_element', '_link'
4-
59
def __init__(self, element, link):
10+
'''
11+
Initialses _element and _link with element and link respectively.
12+
'''
613
self._element = element
714
self._link = link
815

9-
1016
class LinkedList:
11-
17+
'''
18+
Consists of member funtions to perform different
19+
operations on the linked list.
20+
'''
1221
def __init__(self):
22+
'''
23+
Initialses head, tail and size with None, None and 0 respectively.
24+
'''
1325
self._head = None
1426
self._tail = None
1527
self._size = 0
1628

1729
def __len__(self):
30+
'''
31+
Returns length of linked list
32+
'''
1833
return self._size
1934

2035
def isempty(self):
36+
'''
37+
Returns True if linked list is empty, otherwise False.
38+
'''
2139
return self._size == 0
2240

2341
def addLast(self, e):
42+
'''
43+
Adds the passed element at the end of the linked list.
44+
'''
2445
newest = _Node(e, None)
2546

2647
if self.isempty():
@@ -32,6 +53,9 @@ def addLast(self, e):
3253
self._size += 1
3354

3455
def addFirst(self, e):
56+
'''
57+
Adds the passed element at the beginning of the linked list.
58+
'''
3559
newest = _Node(e, None)
3660

3761
if self.isempty():
@@ -43,6 +67,9 @@ def addFirst(self, e):
4367
self._size += 1
4468

4569
def addAnywhere(self, e, index):
70+
'''
71+
Adds the passed element at the passed index position of the linked list.
72+
'''
4673
newest = _Node(e, None)
4774

4875
i = index - 1
@@ -55,9 +82,14 @@ def addAnywhere(self, e, index):
5582
p = p._link
5683
newest._link = p._link
5784
p._link = newest
85+
print(f"Added Item at index {index}!\n\n")
5886
self._size += 1
5987

6088
def removeFirst(self):
89+
'''
90+
Removes element from the beginning of the linked list.
91+
Returns the removed element.
92+
'''
6193
if self.isempty():
6294
print("List is Empty. Cannot perform deletion operation.")
6395
return
@@ -72,12 +104,15 @@ def removeFirst(self):
72104
return e
73105

74106
def removeLast(self):
107+
'''
108+
Removes element from the end of the linked list.
109+
Returns the removed element.
110+
'''
75111
if self.isempty():
76112
print("List is Empty. Cannot perform deletion operation.")
77113
return
78114

79115
p = self._head
80-
81116
if p._link == None:
82117
e = p._element
83118
self._head = None
@@ -92,7 +127,10 @@ def removeLast(self):
92127
return e
93128

94129
def removeAnywhere(self, index):
95-
130+
'''
131+
Removes element from the passed index position of the linked list.
132+
Returns the removed element.
133+
'''
96134
p = self._head
97135
i = index - 1
98136

@@ -110,6 +148,9 @@ def removeAnywhere(self, index):
110148
return e
111149

112150
def display(self):
151+
'''
152+
Utility function to display the linked list.
153+
'''
113154
if self.isempty() == 0:
114155
p = self._head
115156
while p:
@@ -122,6 +163,10 @@ def display(self):
122163
print("Empty")
123164

124165
def search(self, key):
166+
'''
167+
Searches for the passed element in the linked list.
168+
Returns the index position if found, else -1.
169+
'''
125170
p = self._head
126171
index = 0
127172
while p:
@@ -133,8 +178,10 @@ def search(self, key):
133178

134179
###############################################################################
135180

136-
137181
def options():
182+
'''
183+
Prints Menu for operations
184+
'''
138185
options_list = ['Add Last', 'Add First', 'Add Anywhere',
139186
'Remove First', 'Remove Last', 'Remove Anywhere',
140187
'Display List', 'Print Size', 'Search', 'Exit']
@@ -148,7 +195,9 @@ def options():
148195

149196

150197
def switch_case(choice):
151-
198+
'''
199+
Switch Case for operations
200+
'''
152201
os.system('cls')
153202
if choice == 1:
154203
elem = int(input("Enter Item: "))
@@ -164,7 +213,6 @@ def switch_case(choice):
164213
elem = int(input("Enter Item: "))
165214
index = int(input("Enter Index: "))
166215
L.addAnywhere(elem, index)
167-
print(f"Added Item at index {index}!\n\n")
168216

169217
elif choice == 4:
170218
print("Removed Element from First:", L.removeFirst())
@@ -174,8 +222,7 @@ def switch_case(choice):
174222

175223
elif choice == 6:
176224
index = int(input("Enter Index: "))
177-
L.removeAnywhere(index)
178-
print(f"Removed Item at index {index}!\n\n")
225+
print(f"Removed Item: {L.removeAnywhere(index)} !\n\n")
179226
elif choice == 7:
180227
print("List: ", end='')
181228
L.display()
@@ -196,8 +243,9 @@ def switch_case(choice):
196243
import sys
197244
sys.exit()
198245

246+
###############################################################################
199247

200248
L = LinkedList()
201249
while True:
202250
choice = options()
203-
switch_case(choice)
251+
switch_case(choice)

0 commit comments

Comments
(0)

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