|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | + |
| 4 | +class Node(object): |
| 5 | + |
| 6 | + def __init__(self, value=None, prev=None, next=None): |
| 7 | + self.value, self.prev, self.next = value, prev, next |
| 8 | + |
| 9 | + |
| 10 | +class CircularDoubleLinkedList(object): |
| 11 | + """循环双端链表 ADT |
| 12 | + 多了个循环其实就是把 root 的 prev 指向 tail 节点,串起来 |
| 13 | + """ |
| 14 | + |
| 15 | + def __init__(self, maxsize=None): |
| 16 | + self.maxsize = maxsize |
| 17 | + node = Node() |
| 18 | + node.next, node.prev = node, node |
| 19 | + self.root = node |
| 20 | + self.length = 0 |
| 21 | + |
| 22 | + def __len__(self): |
| 23 | + return self.length |
| 24 | + |
| 25 | + def headnode(self): |
| 26 | + return self.root.next |
| 27 | + |
| 28 | + def tailnode(self): |
| 29 | + return self.root.prev |
| 30 | + |
| 31 | + def append(self, value): # O(1), 你发现一般不用 for 循环的就是 O(1),有限个步骤 |
| 32 | + if self.maxsize is not None and len(self) > self.maxsize: |
| 33 | + raise Exception('LinkedList is Full') |
| 34 | + node = Node(value=value) |
| 35 | + tailnode = self.tailnode() or self.root |
| 36 | + |
| 37 | + tailnode.next = node |
| 38 | + node.prev = tailnode |
| 39 | + node.next = self.root |
| 40 | + self.root.prev = node |
| 41 | + self.length += 1 |
| 42 | + |
| 43 | + def appendleft(self, value): |
| 44 | + if self.maxsize is not None and len(self) > self.maxsize: |
| 45 | + raise Exception('LinkedList is Full') |
| 46 | + node = Node(value=value) |
| 47 | + if self.root.next is self.root: # empty |
| 48 | + node.next = self.root |
| 49 | + node.prev = self.root |
| 50 | + self.root.next = node |
| 51 | + self.root.prev = node |
| 52 | + else: |
| 53 | + node.prev = self.root |
| 54 | + headnode = self.root.next |
| 55 | + node.next = headnode |
| 56 | + headnode.prev = node |
| 57 | + self.root.next = node |
| 58 | + self.length += 1 |
| 59 | + |
| 60 | + def remove(self, node): # O(1),传入node 而不是 value 我们就能实现 O(1) 删除 |
| 61 | + """remove |
| 62 | + :param node # 在 lru_cache 里实际上根据key 保存了整个node: |
| 63 | + """ |
| 64 | + if node is self.root: |
| 65 | + return |
| 66 | + else: # |
| 67 | + node.prev.next = node.next |
| 68 | + node.next.prev = node.prev |
| 69 | + self.length -= 1 |
| 70 | + return node |
| 71 | + |
| 72 | + def iter_node(self): |
| 73 | + if self.root.next is self.root: |
| 74 | + return |
| 75 | + curnode = self.root.next |
| 76 | + while curnode.next is not self.root: |
| 77 | + yield curnode |
| 78 | + curnode = curnode.next |
| 79 | + yield curnode |
| 80 | + |
| 81 | + def __iter__(self): |
| 82 | + for node in self.iter_node(): |
| 83 | + yield node.value |
| 84 | + |
| 85 | + def iter_node_reverse(self): |
| 86 | + """相比单链表独有的反序遍历""" |
| 87 | + if self.root.prev is self.root: |
| 88 | + return |
| 89 | + curnode = self.root.prev |
| 90 | + while curnode.prev is not self.root: |
| 91 | + yield curnode |
| 92 | + curnode = curnode.prev |
| 93 | + yield curnode |
| 94 | + |
| 95 | + |
| 96 | +def test_double_link_list(): |
| 97 | + dll = CircularDoubleLinkedList() |
| 98 | + assert len(dll) == 0 |
| 99 | + |
| 100 | + dll.append(0) |
| 101 | + dll.append(1) |
| 102 | + dll.append(2) |
| 103 | + |
| 104 | + assert list(dll) == [0, 1, 2] |
| 105 | + |
| 106 | + assert [node.value for node in dll.iter_node()] == [0, 1, 2] |
| 107 | + assert [node.value for node in dll.iter_node_reverse()] == [2, 1, 0] |
| 108 | + |
| 109 | + headnode = dll.headnode() |
| 110 | + assert headnode.value == 0 |
| 111 | + dll.remove(headnode) |
| 112 | + assert len(dll) == 2 |
| 113 | + assert [node.value for node in dll.iter_node()] == [1, 2] |
| 114 | + |
| 115 | + dll.appendleft(0) |
| 116 | + assert [node.value for node in dll.iter_node()] == [0, 1, 2] |
| 117 | + |
| 118 | + |
| 119 | +if __name__ == '__main__': |
| 120 | + test_double_link_list() |
0 commit comments