队列(Queue)是一种先进先出(FIFO)的线性数据结构,插入操作在队尾(rear)进行,删除操作在队首(front)进行。
队列ADT(抽象数据类型)一般提供以下接口:
① Queue() 创建队列
② enqueue(item) 向队尾插入项
③ dequeue() 返回队首的项,并从队列中删除该项
④ empty() 判断队列是否为空
⑤ size() 返回队列中项的个数
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Created by xuehz on 2017/8/8
class Queue():
def __init__(self,size):
self.queue = []
self.size = size
self.head = -1
self.tail = -1
def Empty(self):
if self.head == self.tail:
return True
else:
return False
def Full(self):
if self.tail - self.head +1 == self.size:
return True
else:
return False
def enQueue(self, content):
if self.Full():
print 'Queue is Full'
else:
self.queue.append(content)
self.tail = self.tail+1
def outQueue(self):
if self.Empty():
print 'Queue is Empty'
else:
self.head = self.head+1
class Queue:
def __init__(self):
self.items = []
def enqueue(self, item):
self.items.append(item)
def dequeue(self):
return self.items.pop(0)
def empty(self):
return self.size() == 0
def size(self):
return len(self.items)