2021-09-07 11:37:03 +00:00
|
|
|
from __future__ import annotations
|
2019-11-10 08:47:04 +00:00
|
|
|
|
2023-10-05 21:55:13 +00:00
|
|
|
from collections.abc import Iterable, Iterator
|
|
|
|
from dataclasses import dataclass
|
2019-11-14 18:59:43 +00:00
|
|
|
|
2023-10-05 21:55:13 +00:00
|
|
|
|
|
|
|
@dataclass
|
2019-11-10 08:47:04 +00:00
|
|
|
class Node:
|
2023-10-05 21:55:13 +00:00
|
|
|
data: int
|
|
|
|
next_node: Node | None = None
|
|
|
|
|
|
|
|
|
|
|
|
class LinkedList:
|
|
|
|
"""A class to represent a Linked List.
|
|
|
|
Use a tail pointer to speed up the append() operation.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self) -> None:
|
|
|
|
"""Initialize a LinkedList with the head node set to None.
|
|
|
|
>>> linked_list = LinkedList()
|
|
|
|
>>> (linked_list.head, linked_list.tail)
|
|
|
|
(None, None)
|
|
|
|
"""
|
|
|
|
self.head: Node | None = None
|
|
|
|
self.tail: Node | None = None # Speeds up the append() operation
|
|
|
|
|
|
|
|
def __iter__(self) -> Iterator[int]:
|
|
|
|
"""Iterate the LinkedList yielding each Node's data.
|
|
|
|
>>> linked_list = LinkedList()
|
|
|
|
>>> items = (1, 2, 3, 4, 5)
|
|
|
|
>>> linked_list.extend(items)
|
|
|
|
>>> tuple(linked_list) == items
|
|
|
|
True
|
|
|
|
"""
|
|
|
|
node = self.head
|
|
|
|
while node:
|
|
|
|
yield node.data
|
|
|
|
node = node.next_node
|
|
|
|
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
"""Returns a string representation of the LinkedList.
|
|
|
|
>>> linked_list = LinkedList()
|
|
|
|
>>> str(linked_list)
|
|
|
|
''
|
|
|
|
>>> linked_list.append(1)
|
|
|
|
>>> str(linked_list)
|
|
|
|
'1'
|
|
|
|
>>> linked_list.extend([2, 3, 4, 5])
|
|
|
|
>>> str(linked_list)
|
|
|
|
'1 -> 2 -> 3 -> 4 -> 5'
|
|
|
|
"""
|
|
|
|
return " -> ".join([str(data) for data in self])
|
2019-11-10 08:47:04 +00:00
|
|
|
|
2023-10-05 21:55:13 +00:00
|
|
|
def append(self, data: int) -> None:
|
|
|
|
"""Appends a new node with the given data to the end of the LinkedList.
|
|
|
|
>>> linked_list = LinkedList()
|
|
|
|
>>> str(linked_list)
|
|
|
|
''
|
|
|
|
>>> linked_list.append(1)
|
|
|
|
>>> str(linked_list)
|
|
|
|
'1'
|
|
|
|
>>> linked_list.append(2)
|
|
|
|
>>> str(linked_list)
|
|
|
|
'1 -> 2'
|
|
|
|
"""
|
|
|
|
if self.tail:
|
|
|
|
self.tail.next_node = self.tail = Node(data)
|
|
|
|
else:
|
|
|
|
self.head = self.tail = Node(data)
|
2019-11-10 08:47:04 +00:00
|
|
|
|
2023-10-05 21:55:13 +00:00
|
|
|
def extend(self, items: Iterable[int]) -> None:
|
|
|
|
"""Appends each item to the end of the LinkedList.
|
|
|
|
>>> linked_list = LinkedList()
|
|
|
|
>>> linked_list.extend([])
|
|
|
|
>>> str(linked_list)
|
|
|
|
''
|
|
|
|
>>> linked_list.extend([1, 2])
|
|
|
|
>>> str(linked_list)
|
|
|
|
'1 -> 2'
|
|
|
|
>>> linked_list.extend([3,4])
|
|
|
|
>>> str(linked_list)
|
|
|
|
'1 -> 2 -> 3 -> 4'
|
|
|
|
"""
|
|
|
|
for item in items:
|
|
|
|
self.append(item)
|
2019-11-10 08:47:04 +00:00
|
|
|
|
2023-10-05 21:55:13 +00:00
|
|
|
|
|
|
|
def make_linked_list(elements_list: Iterable[int]) -> LinkedList:
|
2019-11-10 08:47:04 +00:00
|
|
|
"""Creates a Linked List from the elements of the given sequence
|
2020-10-05 11:08:57 +00:00
|
|
|
(list/tuple) and returns the head of the Linked List.
|
|
|
|
>>> make_linked_list([])
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
Exception: The Elements List is empty
|
|
|
|
>>> make_linked_list([7])
|
|
|
|
7
|
|
|
|
>>> make_linked_list(['abc'])
|
|
|
|
abc
|
|
|
|
>>> make_linked_list([7, 25])
|
2023-10-05 21:55:13 +00:00
|
|
|
7 -> 25
|
2020-10-05 11:08:57 +00:00
|
|
|
"""
|
2019-11-10 08:47:04 +00:00
|
|
|
if not elements_list:
|
|
|
|
raise Exception("The Elements List is empty")
|
|
|
|
|
2023-10-05 21:55:13 +00:00
|
|
|
linked_list = LinkedList()
|
|
|
|
linked_list.extend(elements_list)
|
|
|
|
return linked_list
|
2019-11-10 08:47:04 +00:00
|
|
|
|
2019-11-14 18:59:43 +00:00
|
|
|
|
2023-10-05 21:55:13 +00:00
|
|
|
def in_reverse(linked_list: LinkedList) -> str:
|
2020-10-05 11:08:57 +00:00
|
|
|
"""Prints the elements of the given Linked List in reverse order
|
2023-10-05 21:55:13 +00:00
|
|
|
>>> in_reverse(LinkedList())
|
|
|
|
''
|
|
|
|
>>> in_reverse(make_linked_list([69, 88, 73]))
|
|
|
|
'73 <- 88 <- 69'
|
2020-10-05 11:08:57 +00:00
|
|
|
"""
|
2023-10-05 21:55:13 +00:00
|
|
|
return " <- ".join(str(line) for line in reversed(tuple(linked_list)))
|
2019-11-10 08:47:04 +00:00
|
|
|
|
|
|
|
|
2023-10-05 21:55:13 +00:00
|
|
|
if __name__ == "__main__":
|
2020-10-05 11:08:57 +00:00
|
|
|
from doctest import testmod
|
|
|
|
|
|
|
|
testmod()
|
2023-10-05 21:55:13 +00:00
|
|
|
linked_list = make_linked_list((14, 52, 14, 12, 43))
|
|
|
|
print(f"Linked List: {linked_list}")
|
|
|
|
print(f"Reverse List: {in_reverse(linked_list)}")
|