class seqqueue(object): 8 def __init__(self,maxsize): 9 self.maxsize = maxsize 10 self.queueelem = [] 11 self.count = 0 12 13 def isEmpty(self): 14 return self.count == 0 15 def length(self): 16 return self.count 17 def clear(self): 18 self.queueelem = [None] 19 self.count = 0 20 def get_front(self): 21 return self.queueelem[0] 22 def insert(self,item): 23 if self.count >= self.maxsize: 24 raise Exception("队列已满") 25 else: 26 self.queueelem.append(item) 27 self.count += 1 28 def delelem(self): 29 if self.isEmpty(): 30 raise Exception("队列为空") 31 else: 32 del self.queueelem[0] 33 self.count -= 1 34 def display(self): 35 for i in self.queueelem: 36 print(i,end = " ") 37 print() 38 39 if __name__ == "__main__": 40 seqqueue1 = seqqueue(5) 41 seqqueue1.insert(1) 42 seqqueue1.insert(2) 43 seqqueue1.insert(3) 44 seqqueue1.insert(4) 45 seqqueue1.display() 46 seqqueue1.insert(5) 47 seqqueue1.display() 48 seqqueue1.delelem() 49 seqqueue1.display() 50 print(seqqueue1.get_front()) 51 print(seqqueue1.length()) 52 seqqueue1.clear() 53 seqqueue1.display()
运行结果:
1 2 3 4
1 2 3 4 5
2 3 4 5
2
4
None