1
1
import os
2
-
3
2
class _Node :
3
+ '''
4
+ Creates a Node with two fields:
5
+ 1. element (accesed using ._element)
6
+ 2. link (accesed using ._link)
7
+ '''
4
8
__slots__ = '_element' , '_link'
5
-
6
9
def __init__ (self , element , link ):
10
+ '''
11
+ Initialses _element and _link with element and link respectively.
12
+ '''
7
13
self ._element = element
8
14
self ._link = link
9
15
10
16
class CicularLL :
17
+ '''
18
+ Consists of member funtions to perform different
19
+ operations on the circular linked list.
20
+ '''
11
21
def __init__ (self ):
22
+ '''
23
+ Initialses head, tail and size with None, None and 0 respectively.
24
+ '''
12
25
self ._head = None
13
26
self ._tail = None
14
27
self ._size = 0
15
28
16
29
def __len__ (self ):
30
+ '''
31
+ Returns length of linked list
32
+ '''
17
33
return self ._size
18
34
19
35
def isempty (self ):
36
+ '''
37
+ Returns True if circular linked list is empty, otherwise False.
38
+ '''
20
39
return self ._size == 0
21
40
22
41
def addLast (self , e ):
42
+ '''
43
+ Adds the passed element at the end of the circular linked list.
44
+ '''
23
45
newest = _Node (e , None )
24
46
25
47
if self .isempty ():
@@ -33,6 +55,9 @@ def addLast(self, e):
33
55
self ._size += 1
34
56
35
57
def addFirst (self , e ):
58
+ '''
59
+ Adds the passed element at the beginning of the circular linked list.
60
+ '''
36
61
newest = _Node (e , None )
37
62
38
63
newest ._link = self ._head
@@ -46,6 +71,9 @@ def addFirst(self, e):
46
71
self ._size += 1
47
72
48
73
def addAnywhere (self , e , index ):
74
+ '''
75
+ Adds the passed element at the passed index position of the circular linked list.
76
+ '''
49
77
newest = _Node (e , None )
50
78
if index >= self ._size :
51
79
print (
@@ -66,6 +94,10 @@ def addAnywhere(self, e, index):
66
94
self ._size += 1
67
95
68
96
def removeFirst (self ):
97
+ '''
98
+ Removes element from the beginning of the circular linked list.
99
+ Returns the removed element.
100
+ '''
69
101
if self .isempty ():
70
102
print ("List is Empty" )
71
103
return
@@ -77,6 +109,10 @@ def removeFirst(self):
77
109
return e
78
110
79
111
def removeLast (self ):
112
+ '''
113
+ Removes element from the end of the circular linked list.
114
+ Returns the removed element.
115
+ '''
80
116
if self .isempty ():
81
117
print ("List is Empty" )
82
118
return
@@ -95,7 +131,10 @@ def removeLast(self):
95
131
return e
96
132
97
133
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
+ '''
99
138
if index >= self ._size :
100
139
print (
101
140
f"Index out of range. It should be between 0 - { self ._size - 1 } " )
@@ -118,6 +157,9 @@ def removeAnywhere(self, index):
118
157
return e
119
158
120
159
def display (self ):
160
+ '''
161
+ Utility function to display the circular linked list.
162
+ '''
121
163
if self .isempty () == 0 :
122
164
p = self ._head
123
165
while True :
@@ -131,8 +173,10 @@ def display(self):
131
173
132
174
#########################################################################
133
175
134
-
135
176
def options ():
177
+ '''
178
+ Prints Menu for operations
179
+ '''
136
180
options_list = ['Add Last' , 'Add First' , 'Add Anywhere' ,
137
181
'Remove First' , 'Remove Last' , 'Remove Anywhere' ,
138
182
'Display List' , 'Exit' ]
@@ -146,6 +190,9 @@ def options():
146
190
147
191
148
192
def switch_case (choice ):
193
+ '''
194
+ Switch Case for operations
195
+ '''
149
196
os .system ('cls' )
150
197
if choice == 1 :
151
198
elem = int (input ("Enter Item: " ))
0 commit comments