Skip to content

Commit c8d28a7

Browse files
cclaussgithub-actions
authored andcommitted
Use dataclasses in circular_linked_list.py (TheAlgorithms#10884)
* Use dataclasses in circular_linked_list.py * updating DIRECTORY.md --------- Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent 94cd513 commit c8d28a7

File tree

2 files changed

+23
-28
lines changed

2 files changed

+23
-28
lines changed

DIRECTORY.md

+2
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@
157157
* [Prefix Conversions](conversions/prefix_conversions.py)
158158
* [Prefix Conversions String](conversions/prefix_conversions_string.py)
159159
* [Pressure Conversions](conversions/pressure_conversions.py)
160+
* [Rgb Cmyk Conversion](conversions/rgb_cmyk_conversion.py)
160161
* [Rgb Hsv Conversion](conversions/rgb_hsv_conversion.py)
161162
* [Roman Numerals](conversions/roman_numerals.py)
162163
* [Speed Conversions](conversions/speed_conversions.py)
@@ -198,6 +199,7 @@
198199
* [Lowest Common Ancestor](data_structures/binary_tree/lowest_common_ancestor.py)
199200
* [Maximum Fenwick Tree](data_structures/binary_tree/maximum_fenwick_tree.py)
200201
* [Merge Two Binary Trees](data_structures/binary_tree/merge_two_binary_trees.py)
202+
* [Mirror Binary Tree](data_structures/binary_tree/mirror_binary_tree.py)
201203
* [Non Recursive Segment Tree](data_structures/binary_tree/non_recursive_segment_tree.py)
202204
* [Number Of Possible Binary Trees](data_structures/binary_tree/number_of_possible_binary_trees.py)
203205
* [Red Black Tree](data_structures/binary_tree/red_black_tree.py)

data_structures/linked_list/circular_linked_list.py

+21-28
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,20 @@
11
from __future__ import annotations
22

33
from collections.abc import Iterator
4+
from dataclasses import dataclass
45
from typing import Any
56

67

8+
@dataclass
79
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
1612

1713

14+
@dataclass
1815
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)
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

Comments
 (0)