2021-11-17 03:43:02 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-07-11 08:19:52 +00:00
|
|
|
from collections.abc import Iterator
|
2023-10-24 12:45:36 +00:00
|
|
|
from dataclasses import dataclass
|
2022-07-11 08:19:52 +00:00
|
|
|
from typing import Any
|
2019-11-19 09:53:44 +00:00
|
|
|
|
|
|
|
|
2023-10-24 12:45:36 +00:00
|
|
|
@dataclass
|
2019-11-19 09:53:44 +00:00
|
|
|
class Node:
|
2023-10-24 12:45:36 +00:00
|
|
|
data: Any
|
|
|
|
next_node: Node | None = None
|
2019-11-19 09:53:44 +00:00
|
|
|
|
|
|
|
|
2023-10-24 12:45:36 +00:00
|
|
|
@dataclass
|
2019-11-19 09:53:44 +00:00
|
|
|
class CircularLinkedList:
|
2023-10-24 12:45:36 +00:00
|
|
|
head: Node | None = None # Reference to the head (first node)
|
|
|
|
tail: Node | None = None # Reference to the tail (last node)
|
2019-11-19 09:53:44 +00:00
|
|
|
|
2021-10-07 15:18:23 +00:00
|
|
|
def __iter__(self) -> Iterator[Any]:
|
2023-10-04 09:25:26 +00:00
|
|
|
"""
|
|
|
|
Iterate through all nodes in the Circular Linked List yielding their data.
|
|
|
|
Yields:
|
|
|
|
The data of each node in the linked list.
|
|
|
|
"""
|
2020-09-26 14:57:09 +00:00
|
|
|
node = self.head
|
2023-10-04 16:05:00 +00:00
|
|
|
while node:
|
2020-09-26 14:57:09 +00:00
|
|
|
yield node.data
|
2023-10-24 12:45:36 +00:00
|
|
|
node = node.next_node
|
2020-09-26 14:57:09 +00:00
|
|
|
if node == self.head:
|
|
|
|
break
|
2019-11-19 09:53:44 +00:00
|
|
|
|
2020-09-26 14:57:09 +00:00
|
|
|
def __len__(self) -> int:
|
2023-10-04 09:25:26 +00:00
|
|
|
"""
|
|
|
|
Get the length (number of nodes) in the Circular Linked List.
|
|
|
|
"""
|
2023-04-01 06:26:43 +00:00
|
|
|
return sum(1 for _ in self)
|
2019-11-19 09:53:44 +00:00
|
|
|
|
2023-10-04 09:25:26 +00:00
|
|
|
def __repr__(self) -> str:
|
|
|
|
"""
|
|
|
|
Generate a string representation of the Circular Linked List.
|
|
|
|
Returns:
|
|
|
|
A string of the format "1->2->....->N".
|
|
|
|
"""
|
2020-09-26 14:57:09 +00:00
|
|
|
return "->".join(str(item) for item in iter(self))
|
2019-11-19 09:53:44 +00:00
|
|
|
|
2020-09-26 14:57:09 +00:00
|
|
|
def insert_tail(self, data: Any) -> None:
|
2023-10-04 09:25:26 +00:00
|
|
|
"""
|
|
|
|
Insert a node with the given data at the end of the Circular Linked List.
|
|
|
|
"""
|
2020-09-26 14:57:09 +00:00
|
|
|
self.insert_nth(len(self), data)
|
2019-11-19 09:53:44 +00:00
|
|
|
|
2020-09-26 14:57:09 +00:00
|
|
|
def insert_head(self, data: Any) -> None:
|
2023-10-04 09:25:26 +00:00
|
|
|
"""
|
|
|
|
Insert a node with the given data at the beginning of the Circular Linked List.
|
|
|
|
"""
|
2020-09-26 14:57:09 +00:00
|
|
|
self.insert_nth(0, data)
|
2019-11-19 09:53:44 +00:00
|
|
|
|
2020-09-26 14:57:09 +00:00
|
|
|
def insert_nth(self, index: int, data: Any) -> None:
|
2023-10-04 09:25:26 +00:00
|
|
|
"""
|
|
|
|
Insert the data of the node at the nth pos in the Circular Linked List.
|
|
|
|
Args:
|
|
|
|
index: The index at which the data should be inserted.
|
|
|
|
data: The data to be inserted.
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
IndexError: If the index is out of range.
|
|
|
|
"""
|
2020-09-26 14:57:09 +00:00
|
|
|
if index < 0 or index > len(self):
|
|
|
|
raise IndexError("list index out of range.")
|
2023-10-04 16:05:00 +00:00
|
|
|
new_node: Node = Node(data)
|
2020-09-26 14:57:09 +00:00
|
|
|
if self.head is None:
|
2023-10-24 12:45:36 +00:00
|
|
|
new_node.next_node = new_node # First node points to itself
|
2020-09-26 14:57:09 +00:00
|
|
|
self.tail = self.head = new_node
|
2023-10-04 09:25:26 +00:00
|
|
|
elif index == 0: # Insert at the head
|
2023-10-24 12:45:36 +00:00
|
|
|
new_node.next_node = self.head
|
2023-10-04 16:05:00 +00:00
|
|
|
assert self.tail is not None # List is not empty, tail exists
|
2023-10-24 12:45:36 +00:00
|
|
|
self.head = self.tail.next_node = new_node
|
2019-11-19 09:53:44 +00:00
|
|
|
else:
|
2023-10-04 16:05:00 +00:00
|
|
|
temp: Node | None = self.head
|
2020-09-26 14:57:09 +00:00
|
|
|
for _ in range(index - 1):
|
2023-10-04 16:05:00 +00:00
|
|
|
assert temp is not None
|
2023-10-24 12:45:36 +00:00
|
|
|
temp = temp.next_node
|
2023-10-04 16:05:00 +00:00
|
|
|
assert temp is not None
|
2023-10-24 12:45:36 +00:00
|
|
|
new_node.next_node = temp.next_node
|
|
|
|
temp.next_node = new_node
|
2023-10-04 09:25:26 +00:00
|
|
|
if index == len(self) - 1: # Insert at the tail
|
2020-09-26 14:57:09 +00:00
|
|
|
self.tail = new_node
|
|
|
|
|
2023-10-04 09:25:26 +00:00
|
|
|
def delete_front(self) -> Any:
|
|
|
|
"""
|
|
|
|
Delete and return the data of the node at the front of the Circular Linked List.
|
|
|
|
Raises:
|
|
|
|
IndexError: If the list is empty.
|
|
|
|
"""
|
2020-09-26 14:57:09 +00:00
|
|
|
return self.delete_nth(0)
|
|
|
|
|
2021-10-07 15:18:23 +00:00
|
|
|
def delete_tail(self) -> Any:
|
2023-10-04 09:25:26 +00:00
|
|
|
"""
|
|
|
|
Delete and return the data of the node at the end of the Circular Linked List.
|
|
|
|
Returns:
|
|
|
|
Any: The data of the deleted node.
|
|
|
|
Raises:
|
|
|
|
IndexError: If the index is out of range.
|
|
|
|
"""
|
2020-09-26 14:57:09 +00:00
|
|
|
return self.delete_nth(len(self) - 1)
|
|
|
|
|
2021-10-07 15:18:23 +00:00
|
|
|
def delete_nth(self, index: int = 0) -> Any:
|
2023-10-04 09:25:26 +00:00
|
|
|
"""
|
|
|
|
Delete and return the data of the node at the nth pos in Circular Linked List.
|
|
|
|
Args:
|
|
|
|
index (int): The index of the node to be deleted. Defaults to 0.
|
|
|
|
Returns:
|
|
|
|
Any: The data of the deleted node.
|
|
|
|
Raises:
|
|
|
|
IndexError: If the index is out of range.
|
|
|
|
"""
|
2020-09-26 14:57:09 +00:00
|
|
|
if not 0 <= index < len(self):
|
|
|
|
raise IndexError("list index out of range.")
|
2023-10-04 16:05:00 +00:00
|
|
|
|
2023-10-11 18:30:02 +00:00
|
|
|
assert self.head is not None
|
|
|
|
assert self.tail is not None
|
2023-10-04 16:05:00 +00:00
|
|
|
delete_node: Node = self.head
|
2023-10-04 09:25:26 +00:00
|
|
|
if self.head == self.tail: # Just one node
|
2020-09-26 14:57:09 +00:00
|
|
|
self.head = self.tail = None
|
2023-10-04 09:25:26 +00:00
|
|
|
elif index == 0: # Delete head node
|
2023-10-24 12:45:36 +00:00
|
|
|
assert self.tail.next_node is not None
|
|
|
|
self.tail.next_node = self.tail.next_node.next_node
|
|
|
|
self.head = self.head.next_node
|
2019-11-19 09:53:44 +00:00
|
|
|
else:
|
2023-10-04 16:05:00 +00:00
|
|
|
temp: Node | None = self.head
|
2020-09-26 14:57:09 +00:00
|
|
|
for _ in range(index - 1):
|
2023-10-04 16:05:00 +00:00
|
|
|
assert temp is not None
|
2023-10-24 12:45:36 +00:00
|
|
|
temp = temp.next_node
|
2023-10-11 18:30:02 +00:00
|
|
|
assert temp is not None
|
2023-10-24 12:45:36 +00:00
|
|
|
assert temp.next_node is not None
|
|
|
|
delete_node = temp.next_node
|
|
|
|
temp.next_node = temp.next_node.next_node
|
2023-10-04 09:25:26 +00:00
|
|
|
if index == len(self) - 1: # Delete at tail
|
2020-09-26 14:57:09 +00:00
|
|
|
self.tail = temp
|
|
|
|
return delete_node.data
|
|
|
|
|
2021-10-07 15:18:23 +00:00
|
|
|
def is_empty(self) -> bool:
|
2023-10-04 09:25:26 +00:00
|
|
|
"""
|
|
|
|
Check if the Circular Linked List is empty.
|
|
|
|
Returns:
|
|
|
|
bool: True if the list is empty, False otherwise.
|
|
|
|
"""
|
2020-09-26 14:57:09 +00:00
|
|
|
return len(self) == 0
|
2019-11-19 09:53:44 +00:00
|
|
|
|
|
|
|
|
2020-09-26 14:57:09 +00:00
|
|
|
def test_circular_linked_list() -> None:
|
|
|
|
"""
|
2023-10-04 09:25:26 +00:00
|
|
|
Test cases for the CircularLinkedList class.
|
2020-09-26 14:57:09 +00:00
|
|
|
>>> test_circular_linked_list()
|
|
|
|
"""
|
|
|
|
circular_linked_list = CircularLinkedList()
|
|
|
|
assert len(circular_linked_list) == 0
|
|
|
|
assert circular_linked_list.is_empty() is True
|
|
|
|
assert str(circular_linked_list) == ""
|
|
|
|
|
|
|
|
try:
|
|
|
|
circular_linked_list.delete_front()
|
2023-05-26 07:34:17 +00:00
|
|
|
raise AssertionError # This should not happen
|
2020-09-26 14:57:09 +00:00
|
|
|
except IndexError:
|
|
|
|
assert True # This should happen
|
|
|
|
|
|
|
|
try:
|
|
|
|
circular_linked_list.delete_tail()
|
2023-05-26 07:34:17 +00:00
|
|
|
raise AssertionError # This should not happen
|
2020-09-26 14:57:09 +00:00
|
|
|
except IndexError:
|
|
|
|
assert True # This should happen
|
|
|
|
|
|
|
|
try:
|
|
|
|
circular_linked_list.delete_nth(-1)
|
2023-05-26 07:34:17 +00:00
|
|
|
raise AssertionError
|
2020-09-26 14:57:09 +00:00
|
|
|
except IndexError:
|
|
|
|
assert True
|
|
|
|
|
|
|
|
try:
|
|
|
|
circular_linked_list.delete_nth(0)
|
2023-05-26 07:34:17 +00:00
|
|
|
raise AssertionError
|
2020-09-26 14:57:09 +00:00
|
|
|
except IndexError:
|
|
|
|
assert True
|
|
|
|
|
|
|
|
assert circular_linked_list.is_empty() is True
|
|
|
|
for i in range(5):
|
|
|
|
assert len(circular_linked_list) == i
|
|
|
|
circular_linked_list.insert_nth(i, i + 1)
|
|
|
|
assert str(circular_linked_list) == "->".join(str(i) for i in range(1, 6))
|
|
|
|
|
|
|
|
circular_linked_list.insert_tail(6)
|
|
|
|
assert str(circular_linked_list) == "->".join(str(i) for i in range(1, 7))
|
|
|
|
circular_linked_list.insert_head(0)
|
2023-08-29 13:18:10 +00:00
|
|
|
assert str(circular_linked_list) == "->".join(str(i) for i in range(7))
|
2020-09-26 14:57:09 +00:00
|
|
|
|
|
|
|
assert circular_linked_list.delete_front() == 0
|
|
|
|
assert circular_linked_list.delete_tail() == 6
|
|
|
|
assert str(circular_linked_list) == "->".join(str(i) for i in range(1, 6))
|
|
|
|
assert circular_linked_list.delete_nth(2) == 3
|
|
|
|
|
|
|
|
circular_linked_list.insert_nth(2, 3)
|
|
|
|
assert str(circular_linked_list) == "->".join(str(i) for i in range(1, 6))
|
|
|
|
|
|
|
|
assert circular_linked_list.is_empty() is False
|
2019-11-19 09:53:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
import doctest
|
|
|
|
|
|
|
|
doctest.testmod()
|