python数据结构中实现队列的几种方法
1.list实现 enqueue append()dequeue pop(0) 或 enqueue insert(0,item) dequeue pop()MAX_SIZE = 100class MyQueue1(object): """模拟队列""" def __init__(self): self.items = [] self.size = 0 def is_empty(self): """判断是否为空""" return self.size == 0 def size(self): """返回队列的大小""" return self.size def enqueue(self, item): """入队(加入元素)""" self.items.append(item) self.size += 1 def dequeue(self): """出队(弹出元素)""" if self.size < MAX_SIZE and self.size >= 0: self.size -= 1 return self.items.pop(0) else: print("队列已经为空") return None def getFront(self): if not self.is_empty(): return self.items else: return None def getRear(self): if not self.is_empty(): return self.items else: return None def __str__(self): return str(self.items)class MyQueue2(object): """模拟队列""" def __init__(self): self.items = [] self.size = 0 def is_empty(self): """判断是否为空""" return self.size == 0 def size(self): """返回队列的大小""" if self.size
页:
[1]