@@ -3,30 +3,30 @@ def __init__(self, data):
3
3
self .data = data # given data
4
4
self .next = None # given next to None
5
5
6
- def __repr__ (self ): # String Representation of a Node
7
- return f"< Node: { self .data } > "
6
+ def __repr__ (self ): # string representation of a Node
7
+ return f"Node( { self .data } ) "
8
8
9
9
10
10
class LinkedList :
11
11
def __init__ (self ):
12
- self .head = None # Initialize head to None
12
+ self .head = None # initialize head to None
13
13
14
- def insert_tail (self , data ):
14
+ def insert_tail (self , data ) -> None :
15
15
if self .head is None :
16
- self .insert_head (data ) # If this is first node, call insert_head
16
+ self .insert_head (data ) # if this is first node, call insert_head
17
17
else :
18
18
temp = self .head
19
19
while temp .next : # traverse to last node
20
20
temp = temp .next
21
21
temp .next = Node (data ) # create node & link to tail
22
22
23
- def insert_head (self , data ):
23
+ def insert_head (self , data ) -> None :
24
24
newNod = Node (data ) # create a new node
25
25
if self .head :
26
26
newNod .next = self .head # link newNode to head
27
27
self .head = newNod # make NewNode as head
28
28
29
- def printList (self ): # print every node data
29
+ def print_list (self ) -> None : # print every node data
30
30
temp = self .head
31
31
while temp :
32
32
print (temp .data )
@@ -47,14 +47,12 @@ def delete_tail(self): # delete from tail
47
47
else :
48
48
while temp .next .next : # find the 2nd last element
49
49
temp = temp .next
50
- temp .next , temp = (
51
- None ,
52
- temp .next ,
53
- ) # (2nd last element).next = None and temp = last element
50
+ # (2nd last element).next = None and temp = last element
51
+ temp .next , temp = None , temp .next
54
52
return temp
55
53
56
- def isEmpty (self ):
57
- return self .head is None # Return if head is none
54
+ def is_empty (self ) -> bool :
55
+ return self .head is None # return True if head is none
58
56
59
57
def reverse (self ):
60
58
prev = None
@@ -76,17 +74,16 @@ def __repr__(self): # String representation/visualization of a Linked Lists
76
74
current = self .head
77
75
string_repr = ""
78
76
while current :
79
- string_repr += f"{ current } --- > "
77
+ string_repr += f"{ current } --> "
80
78
current = current .next
81
79
# END represents end of the LinkedList
82
- string_repr += "END"
83
- return string_repr
80
+ return string_repr + "END"
84
81
85
82
# Indexing Support. Used to get a node at particaular position
86
83
def __getitem__ (self , index ):
87
84
current = self .head
88
85
89
- # If LinkedList is Empty
86
+ # If LinkedList is empty
90
87
if current is None :
91
88
raise IndexError ("The Linked List is empty" )
92
89
@@ -113,39 +110,30 @@ def __setitem__(self, index, data):
113
110
114
111
def main ():
115
112
A = LinkedList ()
116
- print ("Inserting 1st at head" )
117
- a1 = input ()
118
- A .insert_head (a1 )
119
- print ("Inserting 2nd at head" )
120
- a2 = input ()
121
- A .insert_head (a2 )
122
- print ("\n Print List : " )
123
- A .printList ()
124
- print ("\n Inserting 1st at Tail" )
125
- a3 = input ()
126
- A .insert_tail (a3 )
127
- print ("Inserting 2nd at Tail" )
128
- a4 = input ()
129
- A .insert_tail (a4 )
130
- print ("\n Print List : " )
131
- A .printList ()
113
+ A .insert_head (input ("Inserting 1st at head " ).strip ())
114
+ A .insert_head (input ("Inserting 2nd at head " ).strip ())
115
+ print ("\n Print list:" )
116
+ A .print_list ()
117
+ A .insert_tail (input ("\n Inserting 1st at tail " ).strip ())
118
+ A .insert_tail (input ("Inserting 2nd at tail " ).strip ())
119
+ print ("\n Print list:" )
120
+ A .print_list ()
132
121
print ("\n Delete head" )
133
122
A .delete_head ()
134
- print ("Delete Tail " )
123
+ print ("Delete tail " )
135
124
A .delete_tail ()
136
- print ("\n Print List : " )
137
- A .printList ()
138
- print ("\n Reverse Linked List " )
125
+ print ("\n Print list: " )
126
+ A .print_list ()
127
+ print ("\n Reverse linked list " )
139
128
A .reverse ()
140
- print ("\n Print List : " )
141
- A .printList ()
142
- print ("\n String Representation of Linked List :" )
129
+ print ("\n Print list: " )
130
+ A .print_list ()
131
+ print ("\n String representation of linked list :" )
143
132
print (A )
144
- print ("\n Reading/Changing Node Data using Indexing :" )
133
+ print ("\n Reading/changing Node data using indexing :" )
145
134
print (f"Element at Position 1: { A [1 ]} " )
146
- p1 = input ("Enter New Value: " )
147
- A [1 ] = p1
148
- print ("New List:" )
135
+ A [1 ] = input ("Enter New Value: " ).strip ()
136
+ print ("New list:" )
149
137
print (A )
150
138
151
139
0 commit comments