-
Notifications
You must be signed in to change notification settings - Fork 0
/
llist.py
66 lines (52 loc) · 1.6 KB
/
llist.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
# modified from https://dbader.org/blog/python-linked-list
from time import time
class Node:
def __init__(self, data=None, prev=None, next=None):
self.data = data
self.prev = prev
self.next = next
def __repr__(self):
return f"<Node: {self.data}>"
class CircularDoublyLinkedList:
def __init__(self):
self.head = None
self.tail = None
def __repr__(self):
return repr(list(self))
def walk_from(self, node, reversed=False):
curr = node
while True:
yield curr
curr = curr.next if not reversed else curr.prev
if curr == node:
break
def __iter__(self):
return self.walk_from(self.head)
def reversed(self):
return self.walk_from(self.tail, reversed=True)
def values(self):
return [node.data for node in self]
def append(self, data):
node = Node(data=data, prev=self.tail, next=self.head)
if self.head is None:
self.head = node
self.tail = node
self.tail.next = node
self.head.prev = node
self.tail = node
return node
def insert_after(self, target, node):
node.next = target.next
node.prev = target
target.next.prev = node
target.next = node
def remove_elem(self, node):
node.prev.next = node.next
node.next.prev = node.prev
if node is self.head:
self.head = node.next
if node is self.tail:
self.tail = node.prev
node.prev = None
node.next = None
return node