-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathlinked_list.py
95 lines (78 loc) · 2.38 KB
/
linked_list.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
class Node:
next: "Node"
def __init__(self, data) -> None:
self.data = data
self.next = None # type: ignore
def push(self, node: "Node"):
if self.length > 1:
self.next.push(node)
else:
self.next = node
def pop(self):
match self.length:
case 1:
return None
case 2:
popped = self.next
self.next = None # type: ignore
return popped
case _:
return self.next.pop()
@property
def length(self):
if self.next is not None:
return 1 + self.next.length
return 1
def __str__(self) -> str:
if self.length > 1:
return f"Node({self.data}) -> {self.next}"
else:
return f"Node({self.data})"
def __repr__(self) -> str:
return self.__str__()
class LinkedList:
head: Node
def __init__(self, head: Node) -> None:
self.head = head
@property
def length(self):
if self.head is not None:
return self.head.length
return 0
def push(self, node: Node):
if self.head is None:
self.head = node
else:
self.head.push(node)
def pop(self):
match self.length:
case 0:
raise IndexError("The linked list is Empty")
case 1:
popped = self.head
self.head = None # type: ignore
return popped
case _:
return self.head.pop()
def __str__(self) -> str:
if self.head is None:
return "LinkedList([])"
return f"LinkedList([ {self.head} ])"
def __repr__(self) -> str:
return self.__str__()
if __name__ == "__main__":
linked = LinkedList(Node(5))
linked.push(Node(2))
linked.push(Node(7))
linked.push(Node(3))
linked.push(Node(8))
print("Linked List:", linked)
print(f"Popped: {linked.pop()}") # Node(8) is popped out
print(f"Popped: {linked.pop()}") # Node(3) is popped out
print(f"Popped: {linked.pop()}") # Node(7) is popped out
print(f"Popped: {linked.pop()}") # Node(2) is popped out
print(f"Popped: {linked.pop()}") # Node(5) is popped out
try:
linked.pop()
except IndexError as e:
print(f"error: {e}") # The linked list is Empty