美文网首页
双向链表

双向链表

作者: 梦醒家先生 | 来源:发表于2018-09-05 22:53 被阅读0次
    • 双向链表定义: “双向链表”相对于“单向链表”是一种更复杂的链表。原因在于, 双向链表的每个节点有两个链接地址:
      • next:指向下一个节点,当此节点为 节点时,指向空值;
      • pre: 指向前一个节点,当此节点为 节点时,指向空值
    class LRUCache(object):
        def __init__(self, capacity):
            # cache 的最大记录数
            self.maxsize = capacity
            # 用于真实的存储数据
            self.inner_dd = {}
            # 链表-头指针
            self.head = None
            # 链表-尾指针
            self.tail = None
    
        def put(self, key, value):
            # 达到指定大小
            if len(self.inner_dd) >= self.maxsize:
                self.remove_head_node()
    
            node = Node()
            node.data = (key, value)
            self.insert_to_tail(node)
            self.inner_dd[key] = node
    
        def insert_to_tail(self, node):
            if self.tail is None:
                self.tail = node
                self.head = node
            else:
                self.tail.next = node
                node.pre = self.tail
                self.tail = node
    
        def remove_head_node(self):
            node = self.head
            del self.inner_dd[node.data[0]]
            node = None
            self.head = self.head.next
            self.head.pre = None
    
        def get(self, key):
            if key in self.inner_dd:
                # 如果命中, 需要将对应的节点移动到队列的尾部
                node = self.inner_dd.get(key)
                self.move_to_tail(node)
                return node.data[1]
            return None
    
        def move_to_tail(self, node):
            # 只需处理在队列头部和中间的情况
            if not (node == self.tail):
                if node == self.head:
                    self.head = node.next
                    self.head.pre = None
                    self.tail.next = node
                    node.pre = self.tail
                    node.next = None
                    self.tail = node
                else:
                    pre_node = node.pre
                    next_node = node.next
                    pre_node.next = next_node
                    next_node.pre = pre_node
    
                    self.tail.next = node
                    node.pre = self.tail
                    node.next = None
                    self.tail = node
    
    
    class Node(object):
        def __init__(self):
            self.pre = None
            self.next = None
            # (key, value)
            self.data = None
    
        def __eq__(self, other):
            if self.data[0] == other.data[0]:
                return True
            return False
    
        def __str__(self):
            return str(self.data)
    
    
    if __name__ == '__main__':
        obj = LRUCache(10)
        param_1 = obj.get(key)
        obj.put(key, value)
    
    

    相关文章

      网友评论

          本文标题:双向链表

          本文链接:https://www.haomeiwen.com/subject/uwozwftx.html