@@ -31,7 +31,8 @@ class CircularDoublyLinkedList:
31
31
32
32
def __iter__ (self ) -> Iterator [Any ]:
33
33
"""
34
- Iterate through all nodes in the Circular Doubly Linked List yielding their data.
34
+ Iterate through all nodes in the Circular Doubly Linked List yielding their
35
+ data.
35
36
Yields:
36
37
The data of each node in the linked list.
37
38
"""
@@ -41,6 +42,7 @@ def __iter__(self) -> Iterator[Any]:
41
42
node = self .head
42
43
while True :
43
44
yield node .data
45
+ assert node .next_node is not None
44
46
node = node .next_node
45
47
if node == self .head :
46
48
break
@@ -67,13 +69,15 @@ def insert_tail(self, data: Any) -> None:
67
69
68
70
def insert_head (self , data : Any ) -> None :
69
71
"""
70
- Insert a node with the given data at the beginning of the Circular Doubly Linked List.
72
+ Insert a node with the given data at the beginning of the Circular Doubly
73
+ Linked List.
71
74
"""
72
75
self .insert_nth (0 , data )
73
76
74
77
def insert_nth (self , index : int , data : Any ) -> None :
75
78
"""
76
- Insert the data of the node at the nth position in the Circular Doubly Linked List.
79
+ Insert the data of the node at the nth position in the Circular Doubly
80
+ Linked List.
77
81
Args:
78
82
index: The index at which the data should be inserted.
79
83
data: The data to be inserted.
@@ -126,15 +130,17 @@ def insert_nth(self, index: int, data: Any) -> None:
126
130
127
131
def delete_front (self ) -> Any :
128
132
"""
129
- Delete and return the data of the node at the front of the Circular Doubly Linked List.
133
+ Delete and return the data of the node at the front of the Circular Doubly
134
+ Linked List.
130
135
Raises:
131
136
IndexError: If the list is empty.
132
137
"""
133
138
return self .delete_nth (0 )
134
139
135
140
def delete_tail (self ) -> Any :
136
141
"""
137
- Delete and return the data of the node at the end of the Circular Doubly Linked List.
142
+ Delete and return the data of the node at the end of the Circular Doubly
143
+ Linked List.
138
144
Returns:
139
145
Any: The data of the deleted node.
140
146
Raises:
@@ -144,7 +150,8 @@ def delete_tail(self) -> Any:
144
150
145
151
def delete_nth (self , index : int = 0 ) -> Any :
146
152
"""
147
- Delete and return the data of the node at the nth position in Circular Doubly Linked List.
153
+ Delete and return the data of the node at the nth position in Circular
154
+ Doubly Linked List.
148
155
Args:
149
156
index (int): The index of the node to be deleted. Defaults to 0.
150
157
Returns:
@@ -171,9 +178,10 @@ def delete_nth(self, index: int = 0) -> Any:
171
178
self .tail .next_node = self .head
172
179
else :
173
180
# Find the node to delete
174
- delete_node : Node | None = self .head
181
+ delete_node = self .head
175
182
for _ in range (index ):
176
183
assert delete_node is not None
184
+ assert delete_node .next_node is not None
177
185
delete_node = delete_node .next_node
178
186
179
187
assert delete_node is not None
@@ -220,6 +228,7 @@ def traverse_backward(self) -> list[Any]:
220
228
node = self .tail
221
229
while True :
222
230
result .append (node .data )
231
+ assert node .prev_node is not None
223
232
node = node .prev_node
224
233
if node == self .tail :
225
234
break
0 commit comments