双向链表
# 实现链表节点
class Node:
def __init__(self,key,value):
self.key = key
self.value = value
self.prev = None
self.next = None
def __str__(self):
val = {{}: {}}.format(self.key,self.value)
return val
def __repr__(self):
val = {{}: {}}.format(self.key,self.value)
return val
# 双向链表实现head指针 tail指针 链表的容量
class DoubleLinkList:
def __init__(self, cap = 0xffff):
self.cap = cap
self.head = None
self.tail = None
self.size = 0 # 当前链表存储了多少个节点
# 从头部添加
def __add_head(self,node):
if not self.head:
self.head = node
self.tail = node
self.head.next = None
self.tail.next = None
else:
node.next = self.head
self.head.prev = node
self.head = node
self.head.prev = None
self.size += 1
return node
# 从尾部添加
def __add_prev(self,node):
if not self.tail:
self.head = node
self.tail = node
self.head.next = None
self.tail.next = None
else:
self.prev.next = node
node.prev = self.tail
self.tail = node
self.tail.next = None
self.size += 1
return node
# 删除任意节点
def __del_node(self,node):
if not node:
node = self.tail
if node == self.tail:
pass
elif node == self.head:
pass
else:
node.prev.next = node.next
node.next.prev = node.prev
self.size -= 1
return node
# 弹出头部节点
def __del_head(self):
pass
# 弹出尾部节点
def __del_prev(self):
pass
基于双向链表实现FIFO,LRU,LFU算法
pass
树的剪枝
- 九宫格问题,围棋问题等
- 都是树的模型,寻求最优解