栈和队列
栈
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| class Stack:
def __init__(self): self.stack = []
def pop(self): if len(self.stack) < 1: return None return self.stack.pop()
def push(self, item): self.stack.append(item)
def size(self): return len(self.stack)
|
队列
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| class Queue:
def __init__(self): self.queue = []
def enqueue(self, item): self.queue.append(item)
def dequeue(self): if len(self.queue) < 1: return None return self.queue.pop(0)
def size(self): return len(self.queue)
|
参考资料
Stacks and Queues in Python