mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-23 21:11:08 +00:00
Use dataclasses in circular_linked_list.py (#10884)
* Use dataclasses in circular_linked_list.py * updating DIRECTORY.md --------- Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
parent
17059b7ece
commit
eb17fcf8f5
|
@ -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)
|
||||
|
@ -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)
|
||||
|
|
|
@ -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]:
|
||||
"""
|
||||
|
@ -32,7 +25,7 @@ class CircularLinkedList:
|
|||
node = self.head
|
||||
while node:
|
||||
yield node.data
|
||||
node = node.next
|
||||
node = node.next_node
|
||||
if node == self.head:
|
||||
break
|
||||
|
||||
|
@ -76,20 +69,20 @@ class CircularLinkedList:
|
|||
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
|
||||
|
||||
|
@ -130,18 +123,18 @@ class CircularLinkedList:
|
|||
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
|
||||
|
|
Loading…
Reference in New Issue
Block a user