Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use dataclasses in circular_linked_list.py #10884

Merged
merged 2 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@
* [Prefix Conversions](conversions/prefix_conversions.py)
* [Prefix Conversions String](conversions/prefix_conversions_string.py)
* [Pressure Conversions](conversions/pressure_conversions.py)
* [Rgb Cmyk Conversion](conversions/rgb_cmyk_conversion.py)
* [Rgb Hsv Conversion](conversions/rgb_hsv_conversion.py)
* [Roman Numerals](conversions/roman_numerals.py)
* [Speed Conversions](conversions/speed_conversions.py)
Expand Down Expand Up @@ -198,6 +199,7 @@
* [Lowest Common Ancestor](data_structures/binary_tree/lowest_common_ancestor.py)
* [Maximum Fenwick Tree](data_structures/binary_tree/maximum_fenwick_tree.py)
* [Merge Two Binary Trees](data_structures/binary_tree/merge_two_binary_trees.py)
* [Mirror Binary Tree](data_structures/binary_tree/mirror_binary_tree.py)
* [Non Recursive Segment Tree](data_structures/binary_tree/non_recursive_segment_tree.py)
* [Number Of Possible Binary Trees](data_structures/binary_tree/number_of_possible_binary_trees.py)
* [Red Black Tree](data_structures/binary_tree/red_black_tree.py)
Expand Down
49 changes: 21 additions & 28 deletions data_structures/linked_list/circular_linked_list.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,20 @@
from __future__ import annotations

from collections.abc import Iterator
from dataclasses import dataclass
from typing import Any


@dataclass
class Node:
def __init__(self, data: Any):
"""
Initialize a new Node with the given data.
Args:
data: The data to be stored in the node.
"""
self.data: Any = data
self.next: Node | None = None # Reference to the next node
data: Any
next_node: Node | None = None


@dataclass
class CircularLinkedList:
def __init__(self) -> None:
"""
Initialize an empty Circular Linked List.
"""
self.head: Node | None = None # Reference to the head (first node)
self.tail: Node | None = None # Reference to the tail (last node)
head: Node | None = None # Reference to the head (first node)
tail: Node | None = None # Reference to the tail (last node)

def __iter__(self) -> Iterator[Any]:
"""
Expand All @@ -32,7 +25,7 @@ def __iter__(self) -> Iterator[Any]:
node = self.head
while node:
yield node.data
node = node.next
node = node.next_node
if node == self.head:
break

Expand Down Expand Up @@ -76,20 +69,20 @@ def insert_nth(self, index: int, data: Any) -> None:
raise IndexError("list index out of range.")
new_node: Node = Node(data)
if self.head is None:
new_node.next = new_node # First node points to itself
new_node.next_node = new_node # First node points to itself
self.tail = self.head = new_node
elif index == 0: # Insert at the head
new_node.next = self.head
new_node.next_node = self.head
assert self.tail is not None # List is not empty, tail exists
self.head = self.tail.next = new_node
self.head = self.tail.next_node = new_node
else:
temp: Node | None = self.head
for _ in range(index - 1):
assert temp is not None
temp = temp.next
temp = temp.next_node
assert temp is not None
new_node.next = temp.next
temp.next = new_node
new_node.next_node = temp.next_node
temp.next_node = new_node
if index == len(self) - 1: # Insert at the tail
self.tail = new_node

Expand Down Expand Up @@ -130,18 +123,18 @@ def delete_nth(self, index: int = 0) -> Any:
if self.head == self.tail: # Just one node
self.head = self.tail = None
elif index == 0: # Delete head node
assert self.tail.next is not None
self.tail.next = self.tail.next.next
self.head = self.head.next
assert self.tail.next_node is not None
self.tail.next_node = self.tail.next_node.next_node
self.head = self.head.next_node
else:
temp: Node | None = self.head
for _ in range(index - 1):
assert temp is not None
temp = temp.next
temp = temp.next_node
assert temp is not None
assert temp.next is not None
delete_node = temp.next
temp.next = temp.next.next
assert temp.next_node is not None
delete_node = temp.next_node
temp.next_node = temp.next_node.next_node
if index == len(self) - 1: # Delete at tail
self.tail = temp
return delete_node.data
Expand Down