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 d4ebd75

Browse files
Refactored Circular Linked List code
1 parent 3dd846f commit d4ebd75

File tree

1 file changed

+51
-4
lines changed

1 file changed

+51
-4
lines changed

‎Circular Linked List/circularLL.py‎

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,47 @@
11
import os
2-
32
class _Node:
3+
'''
4+
Creates a Node with two fields:
5+
1. element (accesed using ._element)
6+
2. link (accesed using ._link)
7+
'''
48
__slots__ = '_element', '_link'
5-
69
def __init__(self, element, link):
10+
'''
11+
Initialses _element and _link with element and link respectively.
12+
'''
713
self._element = element
814
self._link = link
915

1016
class CicularLL:
17+
'''
18+
Consists of member funtions to perform different
19+
operations on the circular linked list.
20+
'''
1121
def __init__(self):
22+
'''
23+
Initialses head, tail and size with None, None and 0 respectively.
24+
'''
1225
self._head = None
1326
self._tail = None
1427
self._size = 0
1528

1629
def __len__(self):
30+
'''
31+
Returns length of linked list
32+
'''
1733
return self._size
1834

1935
def isempty(self):
36+
'''
37+
Returns True if circular linked list is empty, otherwise False.
38+
'''
2039
return self._size == 0
2140

2241
def addLast(self, e):
42+
'''
43+
Adds the passed element at the end of the circular linked list.
44+
'''
2345
newest = _Node(e, None)
2446

2547
if self.isempty():
@@ -33,6 +55,9 @@ def addLast(self, e):
3355
self._size += 1
3456

3557
def addFirst(self, e):
58+
'''
59+
Adds the passed element at the beginning of the circular linked list.
60+
'''
3661
newest = _Node(e, None)
3762

3863
newest._link = self._head
@@ -46,6 +71,9 @@ def addFirst(self, e):
4671
self._size += 1
4772

4873
def addAnywhere(self, e, index):
74+
'''
75+
Adds the passed element at the passed index position of the circular linked list.
76+
'''
4977
newest = _Node(e, None)
5078
if index >= self._size:
5179
print(
@@ -66,6 +94,10 @@ def addAnywhere(self, e, index):
6694
self._size += 1
6795

6896
def removeFirst(self):
97+
'''
98+
Removes element from the beginning of the circular linked list.
99+
Returns the removed element.
100+
'''
69101
if self.isempty():
70102
print("List is Empty")
71103
return
@@ -77,6 +109,10 @@ def removeFirst(self):
77109
return e
78110

79111
def removeLast(self):
112+
'''
113+
Removes element from the end of the circular linked list.
114+
Returns the removed element.
115+
'''
80116
if self.isempty():
81117
print("List is Empty")
82118
return
@@ -95,7 +131,10 @@ def removeLast(self):
95131
return e
96132

97133
def removeAnywhere(self, index):
98-
134+
'''
135+
Removes element from the passed index position of the circular linked list.
136+
Returns the removed element.
137+
'''
99138
if index >= self._size:
100139
print(
101140
f"Index out of range. It should be between 0 - {self._size - 1}")
@@ -118,6 +157,9 @@ def removeAnywhere(self, index):
118157
return e
119158

120159
def display(self):
160+
'''
161+
Utility function to display the circular linked list.
162+
'''
121163
if self.isempty() == 0:
122164
p = self._head
123165
while True:
@@ -131,8 +173,10 @@ def display(self):
131173

132174
#########################################################################
133175

134-
135176
def options():
177+
'''
178+
Prints Menu for operations
179+
'''
136180
options_list = ['Add Last', 'Add First', 'Add Anywhere',
137181
'Remove First', 'Remove Last', 'Remove Anywhere',
138182
'Display List', 'Exit']
@@ -146,6 +190,9 @@ def options():
146190

147191

148192
def switch_case(choice):
193+
'''
194+
Switch Case for operations
195+
'''
149196
os.system('cls')
150197
if choice == 1:
151198
elem = int(input("Enter Item: "))

0 commit comments

Comments
(0)

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