1
1
import os
2
2
class _Node :
3
+ '''
4
+ Creates a Node with two fields:
5
+ 1. element (accesed using ._element)
6
+ 2. link (accesed using ._link)
7
+ '''
3
8
__slots__ = '_element' , '_link'
4
-
5
9
def __init__ (self , element , link ):
10
+ '''
11
+ Initialses _element and _link with element and link respectively.
12
+ '''
6
13
self ._element = element
7
14
self ._link = link
8
15
9
-
10
16
class LinkedList :
11
-
17
+ '''
18
+ Consists of member funtions to perform different
19
+ operations on the linked list.
20
+ '''
12
21
def __init__ (self ):
22
+ '''
23
+ Initialses head, tail and size with None, None and 0 respectively.
24
+ '''
13
25
self ._head = None
14
26
self ._tail = None
15
27
self ._size = 0
16
28
17
29
def __len__ (self ):
30
+ '''
31
+ Returns length of linked list
32
+ '''
18
33
return self ._size
19
34
20
35
def isempty (self ):
36
+ '''
37
+ Returns True if linked list is empty, otherwise False.
38
+ '''
21
39
return self ._size == 0
22
40
23
41
def addLast (self , e ):
42
+ '''
43
+ Adds the passed element at the end of the linked list.
44
+ '''
24
45
newest = _Node (e , None )
25
46
26
47
if self .isempty ():
@@ -32,6 +53,9 @@ def addLast(self, e):
32
53
self ._size += 1
33
54
34
55
def addFirst (self , e ):
56
+ '''
57
+ Adds the passed element at the beginning of the linked list.
58
+ '''
35
59
newest = _Node (e , None )
36
60
37
61
if self .isempty ():
@@ -43,6 +67,9 @@ def addFirst(self, e):
43
67
self ._size += 1
44
68
45
69
def addAnywhere (self , e , index ):
70
+ '''
71
+ Adds the passed element at the passed index position of the linked list.
72
+ '''
46
73
newest = _Node (e , None )
47
74
48
75
i = index - 1
@@ -55,9 +82,14 @@ def addAnywhere(self, e, index):
55
82
p = p ._link
56
83
newest ._link = p ._link
57
84
p ._link = newest
85
+ print (f"Added Item at index { index } !\n \n " )
58
86
self ._size += 1
59
87
60
88
def removeFirst (self ):
89
+ '''
90
+ Removes element from the beginning of the linked list.
91
+ Returns the removed element.
92
+ '''
61
93
if self .isempty ():
62
94
print ("List is Empty. Cannot perform deletion operation." )
63
95
return
@@ -72,12 +104,15 @@ def removeFirst(self):
72
104
return e
73
105
74
106
def removeLast (self ):
107
+ '''
108
+ Removes element from the end of the linked list.
109
+ Returns the removed element.
110
+ '''
75
111
if self .isempty ():
76
112
print ("List is Empty. Cannot perform deletion operation." )
77
113
return
78
114
79
115
p = self ._head
80
-
81
116
if p ._link == None :
82
117
e = p ._element
83
118
self ._head = None
@@ -92,7 +127,10 @@ def removeLast(self):
92
127
return e
93
128
94
129
def removeAnywhere (self , index ):
95
-
130
+ '''
131
+ Removes element from the passed index position of the linked list.
132
+ Returns the removed element.
133
+ '''
96
134
p = self ._head
97
135
i = index - 1
98
136
@@ -110,6 +148,9 @@ def removeAnywhere(self, index):
110
148
return e
111
149
112
150
def display (self ):
151
+ '''
152
+ Utility function to display the linked list.
153
+ '''
113
154
if self .isempty () == 0 :
114
155
p = self ._head
115
156
while p :
@@ -122,6 +163,10 @@ def display(self):
122
163
print ("Empty" )
123
164
124
165
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
+ '''
125
170
p = self ._head
126
171
index = 0
127
172
while p :
@@ -133,8 +178,10 @@ def search(self, key):
133
178
134
179
###############################################################################
135
180
136
-
137
181
def options ():
182
+ '''
183
+ Prints Menu for operations
184
+ '''
138
185
options_list = ['Add Last' , 'Add First' , 'Add Anywhere' ,
139
186
'Remove First' , 'Remove Last' , 'Remove Anywhere' ,
140
187
'Display List' , 'Print Size' , 'Search' , 'Exit' ]
@@ -148,7 +195,9 @@ def options():
148
195
149
196
150
197
def switch_case (choice ):
151
-
198
+ '''
199
+ Switch Case for operations
200
+ '''
152
201
os .system ('cls' )
153
202
if choice == 1 :
154
203
elem = int (input ("Enter Item: " ))
@@ -164,7 +213,6 @@ def switch_case(choice):
164
213
elem = int (input ("Enter Item: " ))
165
214
index = int (input ("Enter Index: " ))
166
215
L .addAnywhere (elem , index )
167
- print (f"Added Item at index { index } !\n \n " )
168
216
169
217
elif choice == 4 :
170
218
print ("Removed Element from First:" , L .removeFirst ())
@@ -174,8 +222,7 @@ def switch_case(choice):
174
222
175
223
elif choice == 6 :
176
224
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 " )
179
226
elif choice == 7 :
180
227
print ("List: " , end = '' )
181
228
L .display ()
@@ -196,8 +243,9 @@ def switch_case(choice):
196
243
import sys
197
244
sys .exit ()
198
245
246
+ ###############################################################################
199
247
200
248
L = LinkedList ()
201
249
while True :
202
250
choice = options ()
203
- switch_case (choice )
251
+ switch_case (choice )
0 commit comments