Skip to content
Merged
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
23 changes: 16 additions & 7 deletions data_structures/linked_list/circular_doubly_linked_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ class CircularDoublyLinkedList:

def __iter__(self) -> Iterator[Any]:
"""
Iterate through all nodes in the Circular Doubly Linked List yielding their data.
Iterate through all nodes in the Circular Doubly Linked List yielding their
data.
Yields:
The data of each node in the linked list.
"""
Expand All @@ -41,6 +42,7 @@ def __iter__(self) -> Iterator[Any]:
node = self.head
while True:
yield node.data
assert node.next_node is not None
node = node.next_node
if node == self.head:
break
Expand All @@ -67,13 +69,15 @@ def insert_tail(self, data: Any) -> None:

def insert_head(self, data: Any) -> None:
"""
Insert a node with the given data at the beginning of the Circular Doubly Linked List.
Insert a node with the given data at the beginning of the Circular Doubly
Linked List.
"""
self.insert_nth(0, data)

def insert_nth(self, index: int, data: Any) -> None:
"""
Insert the data of the node at the nth position in the Circular Doubly Linked List.
Insert the data of the node at the nth position in the Circular Doubly
Linked List.
Args:
index: The index at which the data should be inserted.
data: The data to be inserted.
Expand Down Expand Up @@ -126,15 +130,17 @@ def insert_nth(self, index: int, data: Any) -> None:

def delete_front(self) -> Any:
"""
Delete and return the data of the node at the front of the Circular Doubly Linked List.
Delete and return the data of the node at the front of the Circular Doubly
Linked List.
Raises:
IndexError: If the list is empty.
"""
return self.delete_nth(0)

def delete_tail(self) -> Any:
"""
Delete and return the data of the node at the end of the Circular Doubly Linked List.
Delete and return the data of the node at the end of the Circular Doubly
Linked List.
Returns:
Any: The data of the deleted node.
Raises:
Expand All @@ -144,7 +150,8 @@ def delete_tail(self) -> Any:

def delete_nth(self, index: int = 0) -> Any:
"""
Delete and return the data of the node at the nth position in Circular Doubly Linked List.
Delete and return the data of the node at the nth position in Circular
Doubly Linked List.
Args:
index (int): The index of the node to be deleted. Defaults to 0.
Returns:
Expand All @@ -171,9 +178,10 @@ def delete_nth(self, index: int = 0) -> Any:
self.tail.next_node = self.head
else:
# Find the node to delete
delete_node: Node | None = self.head
delete_node = self.head
for _ in range(index):
assert delete_node is not None
assert delete_node.next_node is not None
delete_node = delete_node.next_node

assert delete_node is not None
Expand Down Expand Up @@ -220,6 +228,7 @@ def traverse_backward(self) -> list[Any]:
node = self.tail
while True:
result.append(node.data)
assert node.prev_node is not None
node = node.prev_node
if node == self.tail:
break
Expand Down