Python: Queue class
1 class Queue(): 2 def __init__(self, size): 3 self.queue = [] 4 self.size = size 5 self.head = 0 6 self.tail = 0 7 8 def isEmpty(self): 9 if self.head == self.tail: 10 return True 11 else: 12 return False 13 14 def isFull(self): 15 if self.tail - self.head == self.size: 16 return True 17 else: 18 return False 19 20 def inQueue(self, content): 21 if self.isFull(): 22 print "Queue is full" 23 else: 24 self.queue.append(content) 25 self.tail += 1 26 27 def outQueue(self): 28 if self.isEmpty(): 29 print "Queue is empty" 30 else: 31 self.queue.remove(self.queue[0]) 32 self.tail -= 1 33 34 def printQueueInfo(aQueue): 35 print "isEmpty:\t{0}".format(aQueue.isEmpty()) 36 print "isFull:\t\t{0}".format(aQueue.isFull()) 37 print "head:\t\t{0}".format(aQueue.head) 38 print "tail:\t\t{0}".format(aQueue.tail) 39 print "queue:\t\t{0}".format(aQueue.queue) 40 41 print "1. Initialise a stack with a size of 2, i.e. store at most 2 elements" 42 q = Queue(2) 43 printQueueInfo(q) 44 45 print "\n2. Add 'a'" 46 q.inQueue('a') 47 printQueueInfo(q) 48 49 print "\n3. Add 'b'" 50 q.inQueue('b') 51 printQueueInfo(q) 52 53 print "\n4. Remove 'a'" 54 q.outQueue() 55 printQueueInfo(q) 56 57 print "\n5. Add 'c'" 58 q.inQueue('c') 59 printQueueInfo(q)