|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +# NOTE: 这里拷贝的 double_link_list.py 里的代码 |
| 4 | + |
| 5 | + |
| 6 | +class Node(object): |
| 7 | + |
| 8 | + def __init__(self, value=None, prev=None, next=None): |
| 9 | + self.value, self.prev, self.next = value, prev, next |
| 10 | + |
| 11 | + |
| 12 | +class CircularDoubleLinkedList(object): |
| 13 | + """循环双端链表 ADT |
| 14 | + 多了个循环其实就是把 root 的 prev 指向 tail 节点,串起来 |
| 15 | + """ |
| 16 | + |
| 17 | + def __init__(self, maxsize=None): |
| 18 | + self.maxsize = maxsize |
| 19 | + node = Node() |
| 20 | + node.next, node.prev = node, node |
| 21 | + self.root = node |
| 22 | + self.length = 0 |
| 23 | + |
| 24 | + def __len__(self): |
| 25 | + return self.length |
| 26 | + |
| 27 | + def headnode(self): |
| 28 | + return self.root.next |
| 29 | + |
| 30 | + def tailnode(self): |
| 31 | + return self.root.prev |
| 32 | + |
| 33 | + def append(self, value): # O(1), 你发现一般不用 for 循环的就是 O(1),有限个步骤 |
| 34 | + if self.maxsize is not None and len(self) > self.maxsize: |
| 35 | + raise Exception('LinkedList is Full') |
| 36 | + node = Node(value=value) |
| 37 | + tailnode = self.tailnode() or self.root |
| 38 | + |
| 39 | + tailnode.next = node |
| 40 | + node.prev = tailnode |
| 41 | + node.next = self.root |
| 42 | + self.root.prev = node |
| 43 | + self.length += 1 |
| 44 | + |
| 45 | + def appendleft(self, value): |
| 46 | + if self.maxsize is not None and len(self) > self.maxsize: |
| 47 | + raise Exception('LinkedList is Full') |
| 48 | + node = Node(value=value) |
| 49 | + if self.root.next is self.root: # empty |
| 50 | + node.next = self.root |
| 51 | + node.prev = self.root |
| 52 | + self.root.next = node |
| 53 | + self.root.prev = node |
| 54 | + else: |
| 55 | + node.prev = self.root |
| 56 | + headnode = self.root.next |
| 57 | + node.next = headnode |
| 58 | + headnode.prev = node |
| 59 | + self.root.next = node |
| 60 | + self.length += 1 |
| 61 | + |
| 62 | + def remove(self, node): # O(1),传入node 而不是 value 我们就能实现 O(1) 删除 |
| 63 | + """remove |
| 64 | + :param node # 在 lru_cache 里实际上根据key 保存了整个node: |
| 65 | + """ |
| 66 | + if node is self.root: |
| 67 | + return |
| 68 | + else: # |
| 69 | + node.prev.next = node.next |
| 70 | + node.next.prev = node.prev |
| 71 | + self.length -= 1 |
| 72 | + return node |
| 73 | + |
| 74 | + def iter_node(self): |
| 75 | + if self.root.next is self.root: |
| 76 | + return |
| 77 | + curnode = self.root.next |
| 78 | + while curnode.next is not self.root: |
| 79 | + yield curnode |
| 80 | + curnode = curnode.next |
| 81 | + yield curnode |
| 82 | + |
| 83 | + def __iter__(self): |
| 84 | + for node in self.iter_node(): |
| 85 | + yield node.value |
| 86 | + |
| 87 | + def iter_node_reverse(self): |
| 88 | + """相比单链表独有的反序遍历""" |
| 89 | + if self.root.prev is self.root: |
| 90 | + return |
| 91 | + curnode = self.root.prev |
| 92 | + while curnode.prev is not self.root: |
| 93 | + yield curnode |
| 94 | + curnode = curnode.prev |
| 95 | + yield curnode |
| 96 | + |
| 97 | + |
| 98 | +############################################################ |
| 99 | +# 分割线,下边是本章 内容实现 |
| 100 | +############################################################ |
| 101 | + |
| 102 | + |
| 103 | +class Deque(CircularDoubleLinkedList): # 注意这里我们用到了继承,嗯,貌似我说过不会用啥 OOP 特性的,抱歉 |
| 104 | + |
| 105 | + def pop(self): |
| 106 | + """删除尾节点""" |
| 107 | + if len(self) == 0: |
| 108 | + raise Exception('empty') |
| 109 | + tailnode = self.tailnode() |
| 110 | + value = tailnode.value |
| 111 | + self.remove(tailnode) |
| 112 | + return value |
| 113 | + |
| 114 | + def popleft(self): |
| 115 | + if len(self) == 0: |
| 116 | + raise Exception('empty') |
| 117 | + headnode = self.headnode() |
| 118 | + value = headnode.value |
| 119 | + self.remove(headnode) |
| 120 | + return value |
| 121 | + |
| 122 | + |
| 123 | +def test_deque(): |
| 124 | + dq = Deque() |
| 125 | + dq.append(1) |
| 126 | + |
| 127 | + dq.append(2) |
| 128 | + assert list(dq) == [1, 2] |
| 129 | + |
| 130 | + dq.appendleft(0) |
| 131 | + assert list(dq) == [0, 1, 2] |
| 132 | + |
| 133 | + dq.pop() |
| 134 | + assert list(dq) == [0, 1] |
| 135 | + |
| 136 | + dq.popleft() |
| 137 | + assert list(dq) == [1] |
| 138 | + |
| 139 | + dq.pop() |
| 140 | + assert len(dq) == 0 |
| 141 | + |
| 142 | + |
| 143 | +class Stack(object): |
| 144 | + def __init__(self): |
| 145 | + self.deque = Deque() |
| 146 | + |
| 147 | + def push(self, value): |
| 148 | + self.deque.append(value) |
| 149 | + |
| 150 | + def pop(self): |
| 151 | + return self.deque.pop() |
| 152 | + |
| 153 | + |
| 154 | +def test_stack(): |
| 155 | + s = Stack() |
| 156 | + s.push(0) |
| 157 | + s.push(1) |
| 158 | + s.push(2) |
| 159 | + |
| 160 | + assert s.pop() == 2 |
| 161 | + assert s.pop() == 1 |
| 162 | + assert s.pop() == 0 |
| 163 | + |
| 164 | + import pytest # pip install pytest |
| 165 | + with pytest.raises(Exception) as excinfo: # 我们来测试是否真的抛出了异常 |
| 166 | + s.pop() |
| 167 | + assert 'empty' in str(excinfo.value) |
| 168 | + |
| 169 | + |
| 170 | +if __name__ == '__main__': |
| 171 | + test_stack() |
0 commit comments