1-
21class Node :
3-
4- def __init__ (self ,value ,rptr = None ) -> None :
2+ def __init__ (self , value , rptr = None ) -> None :
53 self .value = value
64 self .rptr = rptr
75
86 @property
97 def get_value (self ):
108 return self .value
11-
9+
1210 @property
1311 def next (self ):
1412 return self .rptr
15-
13+
1614 @next .setter
17- def next (self ,ptr ):
15+ def next (self , ptr ):
1816 self .rptr = ptr
19-
17+
2018
2119class Unordered_Linked_List :
2220 def __init__ (self ) -> None :
2321 self .first_node = None
24-
25- def insert (self ,value ):
22+
23+ def insert (self , value ):
2624 # inserts a new node with the given value
2725 if self .first_node is None :
2826 self .first_node = Node (value )
2927 else :
3028 tmp = Node (value )
3129 tmp .next = self .first_node
3230 self .first_node = tmp
33-
34- def find (self ,value ):
31+
32+ def find (self , value ):
3533 # returns true if the specified value is in your list
3634 ptr = self .first_node
3735 while ptr is not None :
3836 if ptr .value == value :
39- print (f' { value } is in your list' )
37+ print (f" { value } is in your list" )
4038 return True
4139 else :
4240 ptr = ptr .next
43-
44- print (f' { value } is not in your list' )
41+
42+ print (f" { value } is not in your list" )
4543 return False
46-
47- def size (self ): # returns size of the list
44+
45+ def size (self ): # returns size of the list
4846 ptr = self .first_node
4947 i = 0
5048 while ptr is not None :
5149 ptr = ptr .next
52- i += 1
53- print (f' Your list is of size { i } ' )
50+ i += 1
51+ print (f" Your list is of size { i } " )
5452 return i
55-
56- def remove (self ,value ):
53+
54+ def remove (self , value ):
5755 # removes all instances of a given value
5856 ptr = self .first_node
5957 prev = None
@@ -64,19 +62,20 @@ def remove(self,value):
6462 self .first_node = tmp
6563 else :
6664 prev .next = ptr .next
67-
65+
6866 prev = ptr
6967 ptr = ptr .next
70-
68+
7169 def show (self ):
7270 ptr = self .first_node
7371 val = []
7472 while ptr is not None :
7573 val .append (ptr .get_value )
7674 ptr = ptr .next
77-
75+
7876 print (val )
7977
78+
8079def main ():
8180 list = Unordered_Linked_List ()
8281
@@ -102,17 +101,13 @@ def main():
102101 list .insert (3 )
103102 list .insert (5 )
104103 list .insert (3 )
105-
104+
106105 list .show ()
107106
108107 list .remove (3 )
109108
110109 list .show ()
111110
112111
113-
114-
115112if __name__ == "__main__" :
116113 main ()
117-
118-
0 commit comments