-
Notifications
You must be signed in to change notification settings - Fork 0
/
linklist.py
60 lines (56 loc) · 1.73 KB
/
linklist.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
# type 'a list = Nil | Cons of 'a * 'a list
class Linked_List_Node:
def __init__(self, x) -> None:
self.item = x
self.next = None
def later_node(self, i):
if i == 0: return self
assert self.next
return self.next.later_node(i - 1)
class Linked_List_Seq:
def __init__(self) -> None:
self.head = None
self.size = 0
def __len__(self) -> int:
return self.size
def __iter__ (self):
node = self.node
while node:
yield self.item
node = node.next
def build(self, A):
for a in reversed(A):
self.insert_first(a)
def insert_first(self, x):
self.head = Linked_List_Node(x, self.head)
self.size += 1
def get_at(self, i):
assert i >= 0 and i < self.size
return self.head.later_node(i).item
def set_at(self, i, x):
assert i >= 0 and i < self.size
self.head.later_node(i).item = x
def delete_first(self):
assert self.size > 0
self.head = self.head.next
self.size -= 1
def insert_at(self, i, x):
assert i >= 0 and i <= self.size
if i == 0:
self.insert_first(x)
else:
self.head.later_node(i - 1).next = Linked_List_Node(x, self.head.later_node(i - 1).next)
self.size += 1
def delete_at(self, i):
assert i >= 0 and i < self.size
if i == 0:
self.delete_first()
else:
self.head.later_node(i - 1).next = self.head.later_node(i - 1).next.next
self.size -= 1
def __str__(self):
return str(self.to_list())
def to_list(self):
return [self.get_at(i) for i in range(self.size)]
def __repr__(self):
return str(self.to_list())