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
|
|
|
|
from typing import Any
|
2019-11-19 09:53:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Node:
|
|
|
|
def __init__(self, data: Any):
|
2021-10-07 15:18:23 +00:00
|
|
|
self.data: Any = data
|
2021-11-17 03:43:02 +00:00
|
|
|
self.next: Node | None = None
|
2019-11-19 09:53:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
class CircularLinkedList:
|
|
|
|
def __init__(self):
|
|
|
|
self.head = None
|
2020-09-26 14:57:09 +00:00
|
|
|
self.tail = None
|
2019-11-19 09:53:44 +00:00
|
|
|
|
2021-10-07 15:18:23 +00:00
|
|
|
def __iter__(self) -> Iterator[Any]:
|
2020-09-26 14:57:09 +00:00
|
|
|
node = self.head
|
|
|
|
while self.head:
|
|
|
|
yield node.data
|
|
|
|
node = node.next
|
|
|
|
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-04-01 06:26:43 +00:00
|
|
|
return sum(1 for _ in self)
|
2019-11-19 09:53:44 +00:00
|
|
|
|
2020-09-26 14:57:09 +00:00
|
|
|
def __repr__(self):
|
|
|
|
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:
|
|
|
|
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:
|
|
|
|
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:
|
|
|
|
if index < 0 or index > len(self):
|
|
|
|
raise IndexError("list index out of range.")
|
2019-11-19 09:53:44 +00:00
|
|
|
new_node = Node(data)
|
2020-09-26 14:57:09 +00:00
|
|
|
if self.head is None:
|
|
|
|
new_node.next = new_node # first node points itself
|
|
|
|
self.tail = self.head = new_node
|
|
|
|
elif index == 0: # insert at head
|
|
|
|
new_node.next = self.head
|
|
|
|
self.head = self.tail.next = new_node
|
2019-11-19 09:53:44 +00:00
|
|
|
else:
|
2020-09-26 14:57:09 +00:00
|
|
|
temp = self.head
|
|
|
|
for _ in range(index - 1):
|
|
|
|
temp = temp.next
|
|
|
|
new_node.next = temp.next
|
|
|
|
temp.next = new_node
|
|
|
|
if index == len(self) - 1: # insert at tail
|
|
|
|
self.tail = new_node
|
|
|
|
|
|
|
|
def delete_front(self):
|
|
|
|
return self.delete_nth(0)
|
|
|
|
|
2021-10-07 15:18:23 +00:00
|
|
|
def delete_tail(self) -> Any:
|
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:
|
2020-09-26 14:57:09 +00:00
|
|
|
if not 0 <= index < len(self):
|
|
|
|
raise IndexError("list index out of range.")
|
|
|
|
delete_node = self.head
|
|
|
|
if self.head == self.tail: # just one node
|
|
|
|
self.head = self.tail = None
|
|
|
|
elif index == 0: # delete head node
|
|
|
|
self.tail.next = self.tail.next.next
|
|
|
|
self.head = self.head.next
|
2019-11-19 09:53:44 +00:00
|
|
|
else:
|
2020-09-26 14:57:09 +00:00
|
|
|
temp = self.head
|
|
|
|
for _ in range(index - 1):
|
|
|
|
temp = temp.next
|
|
|
|
delete_node = temp.next
|
|
|
|
temp.next = temp.next.next
|
|
|
|
if index == len(self) - 1: # delete at tail
|
|
|
|
self.tail = temp
|
|
|
|
return delete_node.data
|
|
|
|
|
2021-10-07 15:18:23 +00:00
|
|
|
def is_empty(self) -> bool:
|
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:
|
|
|
|
"""
|
|
|
|
>>> 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()
|
2022-10-13 16:03:06 +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()
|
2022-10-13 16:03:06 +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)
|
2022-10-13 16:03:06 +00:00
|
|
|
raise AssertionError()
|
2020-09-26 14:57:09 +00:00
|
|
|
except IndexError:
|
|
|
|
assert True
|
|
|
|
|
|
|
|
try:
|
|
|
|
circular_linked_list.delete_nth(0)
|
2022-10-13 16:03:06 +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)
|
|
|
|
assert str(circular_linked_list) == "->".join(str(i) for i in range(0, 7))
|
|
|
|
|
|
|
|
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()
|