6
6
7
7
class Node :
8
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
+ """
9
14
self .data : Any = data
10
- self .next : Node | None = None
15
+ self .next : Node | None = None # Reference to the next node
11
16
12
17
13
18
class CircularLinkedList :
14
- def __init__ (self ):
15
- self .head = None
16
- self .tail = None
19
+ def __init__ (self ) -> None :
20
+ """
21
+ Initialize an empty Circular Linked List.
22
+ """
23
+ self .head = None # Reference to the head (first node)
24
+ self .tail = None # Reference to the tail (last node)
17
25
18
26
def __iter__ (self ) -> Iterator [Any ]:
27
+ """
28
+ Iterate through all nodes in the Circular Linked List yielding their data.
29
+ Yields:
30
+ The data of each node in the linked list.
31
+ """
19
32
node = self .head
20
33
while self .head :
21
34
yield node .data
@@ -24,25 +37,48 @@ def __iter__(self) -> Iterator[Any]:
24
37
break
25
38
26
39
def __len__ (self ) -> int :
40
+ """
41
+ Get the length (number of nodes) in the Circular Linked List.
42
+ """
27
43
return sum (1 for _ in self )
28
44
29
- def __repr__ (self ):
45
+ def __repr__ (self ) -> str :
46
+ """
47
+ Generate a string representation of the Circular Linked List.
48
+ Returns:
49
+ A string of the format "1->2->....->N".
50
+ """
30
51
return "->" .join (str (item ) for item in iter (self ))
31
52
32
53
def insert_tail (self , data : Any ) -> None :
54
+ """
55
+ Insert a node with the given data at the end of the Circular Linked List.
56
+ """
33
57
self .insert_nth (len (self ), data )
34
58
35
59
def insert_head (self , data : Any ) -> None :
60
+ """
61
+ Insert a node with the given data at the beginning of the Circular Linked List.
62
+ """
36
63
self .insert_nth (0 , data )
37
64
38
65
def insert_nth (self , index : int , data : Any ) -> None :
66
+ """
67
+ Insert the data of the node at the nth pos in the Circular Linked List.
68
+ Args:
69
+ index: The index at which the data should be inserted.
70
+ data: The data to be inserted.
71
+
72
+ Raises:
73
+ IndexError: If the index is out of range.
74
+ """
39
75
if index < 0 or index > len (self ):
40
76
raise IndexError ("list index out of range." )
41
77
new_node = Node (data )
42
78
if self .head is None :
43
- new_node .next = new_node # first node points itself
79
+ new_node .next = new_node # First node points to itself
44
80
self .tail = self .head = new_node
45
- elif index == 0 : # insert at head
81
+ elif index == 0 : # Insert at the head
46
82
new_node .next = self .head
47
83
self .head = self .tail .next = new_node
48
84
else :
@@ -51,22 +87,43 @@ def insert_nth(self, index: int, data: Any) -> None:
51
87
temp = temp .next
52
88
new_node .next = temp .next
53
89
temp .next = new_node
54
- if index == len (self ) - 1 : # insert at tail
90
+ if index == len (self ) - 1 : # Insert at the tail
55
91
self .tail = new_node
56
92
57
- def delete_front (self ):
93
+ def delete_front (self ) -> Any :
94
+ """
95
+ Delete and return the data of the node at the front of the Circular Linked List.
96
+ Raises:
97
+ IndexError: If the list is empty.
98
+ """
58
99
return self .delete_nth (0 )
59
100
60
101
def delete_tail (self ) -> Any :
102
+ """
103
+ Delete and return the data of the node at the end of the Circular Linked List.
104
+ Returns:
105
+ Any: The data of the deleted node.
106
+ Raises:
107
+ IndexError: If the index is out of range.
108
+ """
61
109
return self .delete_nth (len (self ) - 1 )
62
110
63
111
def delete_nth (self , index : int = 0 ) -> Any :
112
+ """
113
+ Delete and return the data of the node at the nth pos in Circular Linked List.
114
+ Args:
115
+ index (int): The index of the node to be deleted. Defaults to 0.
116
+ Returns:
117
+ Any: The data of the deleted node.
118
+ Raises:
119
+ IndexError: If the index is out of range.
120
+ """
64
121
if not 0 <= index < len (self ):
65
122
raise IndexError ("list index out of range." )
66
123
delete_node = self .head
67
- if self .head == self .tail : # just one node
124
+ if self .head == self .tail : # Just one node
68
125
self .head = self .tail = None
69
- elif index == 0 : # delete head node
126
+ elif index == 0 : # Delete head node
70
127
self .tail .next = self .tail .next .next
71
128
self .head = self .head .next
72
129
else :
@@ -75,16 +132,22 @@ def delete_nth(self, index: int = 0) -> Any:
75
132
temp = temp .next
76
133
delete_node = temp .next
77
134
temp .next = temp .next .next
78
- if index == len (self ) - 1 : # delete at tail
135
+ if index == len (self ) - 1 : # Delete at tail
79
136
self .tail = temp
80
137
return delete_node .data
81
138
82
139
def is_empty (self ) -> bool :
140
+ """
141
+ Check if the Circular Linked List is empty.
142
+ Returns:
143
+ bool: True if the list is empty, False otherwise.
144
+ """
83
145
return len (self ) == 0
84
146
85
147
86
148
def test_circular_linked_list () -> None :
87
149
"""
150
+ Test cases for the CircularLinkedList class.
88
151
>>> test_circular_linked_list()
89
152
"""
90
153
circular_linked_list = CircularLinkedList ()
0 commit comments