Python实现栈
Python实现栈
class Node(object): def __init__(self, value=None, prev=None, next=None): self.value, self.prev, self.next = value, prev, next class CirculaDoubleLinkedList(object): def __init__(self, msxsize=None): # msxsize=None代表无限大 self.maxsize = msxsize node = Node() node.next, node.prev = node, node self.root = node self.length = 0 def __len__(self): return self.length def headnode(self): return self.root.next def tailnode(self): # 根节点的prev指向尾节点 return self.root.prev def append(self, value): # 判断当前最大值是否超过了允许的最大值 if self.maxsize is not None and len(self) > self.maxsize: raise Exception("Full") node = Node(value=value) tailnode = self.tailnode() # 最后一个节点指向新的最后节点 tailnode.next = node # 新的最后节点指向前节点 node.prev = tailnode # node的下一个节点指向root node.next = self.root # 根节点的上一个节点指向新的最后节点,形成闭环 self.root.prev = node self.length += 1 def appendleft(self, value): # 判断当前最大值是否超过了允许的最大值 if self.maxsize is not None and len(self) > self.maxsize: raise Exception("Full") node = Node(value=value) # 如果根节点的下一个节点是自己,则证明是空的 if self.root.next is self.root: # 新节点的下一个指向根节点 node.next = self.root # 新节点的上一个指向根节点 node.prev = self.root # 根节点的下一个指向新节点 self.root.next = node # 根节点的上一个指向新节点 self.root.prev = node else: # 新节点的上一个节点指向根节点 node.prev = self.root # 获取头节点 headnode = self.headnode() # 新节点的下一个指向头节点 node.next = headnode # 头节点的上一个指向新节点 headnode.prev = node # 根节点的下一个指向新节点 self.root.next = node self.length+=1 def remove(self, node): # O(1) node is not value if node is self.root: return else: # 被删除节点的上一个节点的next指向被删除节点的下一个节点 node.prev.next = node.next # 被删除节点的下一个节点的prev指向被删除节点的上一个节点 node.next.prev = node.prev self.length -= 1 return node def iter_node(self): if self.root.next is self.root: return curnode = self.root.next while curnode is not self.root: yield curnode curnode = curnode.next yield curnode def __iter__(self): for node in self.iter_node(): yield node.value def iter_node_reverse(self): if self.root.next is self.root: return curnode = self.root.prev while curnode is not self.root: yield curnode curnode = curnode.prev yield curnode class Deque(CirculaDoubleLinkedList): def pop(self): if len(self) == 0: raise Exception("empty") tailnade = self.tailnode() value = tailnade.value self.remove(tailnade) return value def popleft(self): if len(self) == 0: raise Exception("empty") headnode = self.headnode() value = headnode.value self.remove(headnode) return value class Stack(object): def __init__(self): self.deque = Deque() def push(self, value): return self.deque.append(value) def pop(self): return self.deque.pop() def __len__(self): return len(self.deque) def is_empty(self): return len(self) == 0
相关推荐
boneix 2020-10-21
seanzed 2020-10-15
ifconfig 2020-10-14
学留痕 2020-09-20
往后余生 2020-09-17
kka 2020-09-14
redis 2020-09-07
lzccheng 2020-09-06
soyo 2020-08-31
stonerkuang 2020-08-18
LxyPython 2020-08-17
raksmart0 2020-08-17
Lzs 2020-08-14
MrHaoNan 2020-07-31
80530895 2020-07-05
lengyu0 2020-06-28
YarnSup 2020-06-28
huanglianhuabj00 2020-06-27