Python: Queue class
2015-07-09 08:55
369 查看
class Queue(): def __init__(self, size): self.queue = [] self.size = size self.head = 0 self.tail = 0 def isEmpty(self): if self.head == self.tail: return True else: return False def isFull(self): if self.tail - self.head == self.size: return True else: return False def inQueue(self, content): if self.isFull(): print "Queue is full" else: self.queue.append(content) self.tail += 1 def outQueue(self): if self.isEmpty(): print "Queue is empty" else: self.queue.remove(self.queue[0]) self.tail -= 1 def printQueueInfo(aQueue): print "isEmpty:\t{0}".format(aQueue.isEmpty()) print "isFull:\t\t{0}".format(aQueue.isFull()) print "head:\t\t{0}".format(aQueue.head) print "tail:\t\t{0}".format(aQueue.tail) print "queue:\t\t{0}".format(aQueue.queue) print "1. Initialise a stack with a size of 2, i.e. store at most 2 elements" q = Queue(2) printQueueInfo(q) print "\n2. Add 'a'" q.inQueue('a') printQueueInfo(q) print "\n3. Add 'b'" q.inQueue('b') printQueueInfo(q) print "\n4. Remove 'a'" q.outQueue() printQueueInfo(q) print "\n5. Add 'c'" q.inQueue('c') printQueueInfo(q)
相关文章推荐
- Python: Stack class
- python,dict的setdefault方法
- numpy中的broadcast
- Python内存管理机制
- python compile、eval、exec内建函数
- Windows7下搭建Eclipse+Python开发环境
- 《Python学习手册》学习笔记——第4章
- python实现自动重启本程序的方法
- python实现用户登陆邮件通知的方法
- Python实现文件按照日期命名的方法
- Python基于checksum计算文件是否相同的方法
- python查看zip包中文件及大小的方法
- Python使用turtule画五角星的方法
- python简单猜数游戏实例
- python基于BeautifulSoup实现抓取网页指定内容的方法
- python冒泡排序简单实现方法
- Python中的anydbm模版和shelve模版使用指南
- Python中的fileinput模块的简单实用示例
- python文件随机读写例子
- python