11from __future__ import annotations
22
33from collections .abc import Iterator
4+ from dataclasses import dataclass
45from typing import Any
56
67
8+ @dataclass
79class 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
1612
1713
14+ @dataclass
1815class 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)
2518
2619 def __iter__ (self ) -> Iterator [Any ]:
2720 """
@@ -32,7 +25,7 @@ def __iter__(self) -> Iterator[Any]:
3225 node = self .head
3326 while node :
3427 yield node .data
35- node = node .next
28+ node = node .next_node
3629 if node == self .head :
3730 break
3831
@@ -76,20 +69,20 @@ def insert_nth(self, index: int, data: Any) -> None:
7669 raise IndexError ("list index out of range." )
7770 new_node : Node = Node (data )
7871 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
8073 self .tail = self .head = new_node
8174 elif index == 0 : # Insert at the head
82- new_node .next = self .head
75+ new_node .next_node = self .head
8376 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
8578 else :
8679 temp : Node | None = self .head
8780 for _ in range (index - 1 ):
8881 assert temp is not None
89- temp = temp .next
82+ temp = temp .next_node
9083 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
9386 if index == len (self ) - 1 : # Insert at the tail
9487 self .tail = new_node
9588
@@ -130,18 +123,18 @@ def delete_nth(self, index: int = 0) -> Any:
130123 if self .head == self .tail : # Just one node
131124 self .head = self .tail = None
132125 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
136129 else :
137130 temp : Node | None = self .head
138131 for _ in range (index - 1 ):
139132 assert temp is not None
140- temp = temp .next
133+ temp = temp .next_node
141134 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
145138 if index == len (self ) - 1 : # Delete at tail
146139 self .tail = temp
147140 return delete_node .data
0 commit comments