1
1
from __future__ import annotations
2
2
3
3
from collections .abc import Iterator
4
+ from dataclasses import dataclass
4
5
from typing import Any
5
6
6
7
8
+ @dataclass
7
9
class Node :
8
- def __init__ (self , data : Any ):
9
- """
10
- Initialize a new Node with the given data.
11
- Args:
12
- data: The data to be stored in the node.
13
- """
14
- self .data : Any = data
15
- self .next : Node | None = None # Reference to the next node
10
+ data : Any
11
+ next_node : Node | None = None
16
12
17
13
14
+ @dataclass
18
15
class CircularLinkedList :
19
- def __init__ (self ) -> None :
20
- """
21
- Initialize an empty Circular Linked List.
22
- """
23
- self .head : Node | None = None # Reference to the head (first node)
24
- self .tail : Node | None = None # Reference to the tail (last node)
16
+ head : Node | None = None # Reference to the head (first node)
17
+ tail : Node | None = None # Reference to the tail (last node)
25
18
26
19
def __iter__ (self ) -> Iterator [Any ]:
27
20
"""
@@ -32,7 +25,7 @@ def __iter__(self) -> Iterator[Any]:
32
25
node = self .head
33
26
while node :
34
27
yield node .data
35
- node = node .next
28
+ node = node .next_node
36
29
if node == self .head :
37
30
break
38
31
@@ -76,20 +69,20 @@ def insert_nth(self, index: int, data: Any) -> None:
76
69
raise IndexError ("list index out of range." )
77
70
new_node : Node = Node (data )
78
71
if self .head is None :
79
- new_node .next = new_node # First node points to itself
72
+ new_node .next_node = new_node # First node points to itself
80
73
self .tail = self .head = new_node
81
74
elif index == 0 : # Insert at the head
82
- new_node .next = self .head
75
+ new_node .next_node = self .head
83
76
assert self .tail is not None # List is not empty, tail exists
84
- self .head = self .tail .next = new_node
77
+ self .head = self .tail .next_node = new_node
85
78
else :
86
79
temp : Node | None = self .head
87
80
for _ in range (index - 1 ):
88
81
assert temp is not None
89
- temp = temp .next
82
+ temp = temp .next_node
90
83
assert temp is not None
91
- new_node .next = temp .next
92
- temp .next = new_node
84
+ new_node .next_node = temp .next_node
85
+ temp .next_node = new_node
93
86
if index == len (self ) - 1 : # Insert at the tail
94
87
self .tail = new_node
95
88
@@ -130,18 +123,18 @@ def delete_nth(self, index: int = 0) -> Any:
130
123
if self .head == self .tail : # Just one node
131
124
self .head = self .tail = None
132
125
elif index == 0 : # Delete head node
133
- assert self .tail .next is not None
134
- self .tail .next = self .tail .next . next
135
- self .head = self .head .next
126
+ assert self .tail .next_node is not None
127
+ self .tail .next_node = self .tail .next_node . next_node
128
+ self .head = self .head .next_node
136
129
else :
137
130
temp : Node | None = self .head
138
131
for _ in range (index - 1 ):
139
132
assert temp is not None
140
- temp = temp .next
133
+ temp = temp .next_node
141
134
assert temp is not None
142
- assert temp .next is not None
143
- delete_node = temp .next
144
- temp .next = temp .next . next
135
+ assert temp .next_node is not None
136
+ delete_node = temp .next_node
137
+ temp .next_node = temp .next_node . next_node
145
138
if index == len (self ) - 1 : # Delete at tail
146
139
self .tail = temp
147
140
return delete_node .data
0 commit comments