python实现栈和队列 发表于 2019-09-30 | 分类于 python数据结构与算法 | 阅读次数: python实现栈12345678910111213141516171819202122232425262728293031class Stack: def __init__(self): self.items = [] def isEmpty(self): return self.items == [] def push(self, item): self.items.append(item) def pop(self): return self.items.pop() def peek(self): return self.items[len(self.items) - 1] def size(self): return len(self.items)#将一个新项添加到栈顶s.push('dog')#从栈返回顶部元素但不会删除s.peek()#返回栈中的item数量s.size()#判断栈是否为空s.isEmpty()#从栈中删除顶部项s.pop()方式二:直接导包(下载)from pythonds.basic.stack import Stack python实现队列1234567891011121314151617181920212223class Queue: def __init__(self): self.items = [] def isEmpty(self): return self.items == [] def enqueue(self, item): self.items.insert(0,item) def dequeue(self): return self.items.pop() def size(self): return len(self.items)#入队enqueue#出队dequeue#查看队列大小size#查看是否为空isEmpty