这样做的目的是在Python中实现队列类。
类Queue编写如下:
class Queue():
def __init__(self):
self.MAX = 30
self.list = [''] * self.MAX
self.FP = 0
self.BP = 0
def queue(self, item):
if self.isFull() == False:
self.list[self.FP] = item
if self.FP == self.BP:
self.FP += 1
else:
self.FP += 1
self.BP += 1
if self.FP == self.MAX:
self.FP == 0
elif self.BP == self.MAX:
self.BP == 0
else:
print 'The queue is full.'
def Dequeue(self):
if self.isEmpty() == False:
if self.FP == 1 and self.BP == 0:
self.FP -= 1
else:
self.FP -= 1
self.BP -= 1
return self.list[self.SP]
else:
print 'The queue is empty.'
def isEmpty(self):
if self.FP == self.BP:
return True
else:
return False
def isFull(self):
if self.FP - self.BP == self.MAX:
return True
else:
return False
def peek(self):
print self.list
然后下面的代码是主要功能:
def main():
choice = raw_input('Choose:\na - Queue\nb - Dequeue\nc - Peek\nResponse: ')
a = Queue()
if choice in ('a','A'):
inputted = raw_input('Enter the data you want to add. ')
Queue.queue(a, inputted)
elif choice in ('b','B'):
Queue.dequeue(a)
elif choice in ('c','C'):
Queue.peek(a)
但是,当我尝试使用 main()运行此操作,然后使用选项a然后再次运行 main()进行查看时,列表中只包含 [''] * 30 并且在您选择选项a时输入的数字不会替换任何内容。为什么呢?