写了好久终于把逻辑理顺了,删删减减的注释没写多少。小白很欣慰
class MyCircularQueue(object): # 设置定义传进来的参数K,也就是队列最大值,然后头指针,尾指针,创建一个内部都为空且最大为K+1的列表 def __init__(self, k): self.queue = [None] * k # 空列表 self.maxsize = k + 1 # 最大值 self.head = 0 # 头指针 self.tial = 0 # 尾指针 self.tag = 0 # 添加标识符,判断队列空满状态 def enQueue(self, value): # 判断列表空满状态,不满就进行插入,并让头指针为0,尾指针向下移动一位 if (self.isFull() == -1): # print('列表满') self.tag = 1 return if (self.isEmpty() == True): # print('列表为空') self.queue[self.tial] = value self.tial = (self.tial + 1) % self.maxsize self.tag = 1 return self.queue[self.tial] = value self.tial = (self.tial + 1) % self.maxsize self.tag = 1 def deQueue(self): # 判断列表是否为空 if (self.isEmpty() == True): print('队列为空,无法判定!') self.tag = 0 return data = self.queue[self.head] # 保留的火种,可能有用 self.queue[self.head] = None self.head = (self.head + 1) % self.maxsize if self.head == self.tial: self.tag = 0 return def Front(self): # 判断列表是否为空 if (self.isEmpty() == True): print('队列为空,没法取出来!') self.tag = 0 return -1 else: data = self.queue[self.head] self.queue[self.head] = None self.head = (self.head + 1) % self.maxsize self.tag = 1 if self.head != self.tial: return data else: self.tag = 0 return '队列空' def Rear(self): # 判断列表是否为空 if (self.isFull() == 0): print('队列为空,没法取出来!') return -1 else: data = self.queue[self.tial - 1] self.queue[self.tial - 1] = None self.tial = (self.tial - 1) % self.maxsize return data def isFull(self): if (self.tag == 1) and ((self.tial + 1) % self.maxsize == self.head): # print('队列为满') return -1 else: # print('队列非满') return 1 def isEmpty(self): if (self.tag == 0) and (self.tial == self.head): return True obj = MyCircularQueue(4) # param_1 = obj.enQueue(1) for i in range(4): obj.enQueue(i) print(obj.queue) # param_2 = obj.deQueue() for i in range(4): obj.deQueue() print(obj.queue) # param_3 = obj.Front() print(obj.Front()) # param_4 = obj.Rear() print(obj.Rear()) # param_5 = obj.isEmpty() # print(obj.isEmpty()) # param_6 = obj.isFull() # print(obj.isFull())