无法将值推送到队列中并仅停留在第一个和最后一个输入上(python)

Can't push values in a queue and stuck on only first and last input(python)

我想用 self 参数制作简单的队列,但是面临输出问题,我添加到队列中的值只是第一个和最后一个在第一个和第二个位置,中间的所有值都被忽略并改为放置零(创建时)。

class Queue():
    data = []
    r = 0
    f = 0
    maxSize = 0
    size = 0
    def __init__(self,maxSize):
        self.maxSize = maxSize
        self.data = [0.0]*maxSize

    def addValue(self, val):
        if self.size == self.maxSize:
            print('Queue overflow')
        else:
            self.data[self.r] = val
            self.r =+ 1
            self.size =+ 1

    def printQ(self):
        if self.size == 0:
            print('[]')
        else:
            print('[',end='')
            for i in range(0,len(self.data)):
                print(self.data[i],end=',')
            print(']')

myQueue = Queue(7)
print(myQueue.isEmpty())
myQueue.addValue(12)
myQueue.addValue(135)
myQueue.addValue(1)
myQueue.addValue(5)
myQueue.addValue(19)
myQueue.addValue(123)
myQueue.addValue(2222)

print(myQueue.isEmpty())
myQueue.printQ()

输出为:

True
False
[12,2222,0.0,0.0,0.0,0.0,0.0,]
self.r =+ 1
self.size =+ 1

应该是

self.r += 1
self.size += 1

您可能还应该分配:

data = []
r = 0
f = 0
maxSize = 0
size = 0

所有作为构造函数中的实例变量,而不是将它们声明为 class 变量

我不知道 r[0.0]*maxSize 有什么用,但这段代码有效:

class Queue():
    data = []
    f = 0
    maxSize = 0
    size = 0

    def __init__(self,maxSize):
        self.maxSize = maxSize
        self.data = []

    def addValue(self, val):
        if self.size == self.maxSize:
            print('Queue overflow')
        else:
            self.data.append(val)
            self.size =+ 1

    def printQ(self):
        if self.size == 0:
            print('[]')
        else:
            print('[',end='')
            print(",".join(str(x) for x in self.data),end='')
            print(']')

    def isEmpty(self):
      return len(self.data) == 0

myQueue = Queue(7)
print(myQueue.isEmpty())
myQueue.addValue(12)
myQueue.addValue(135)
myQueue.addValue(1)
myQueue.addValue(5)
myQueue.addValue(19)
myQueue.addValue(123)
myQueue.addValue(2222)

print(myQueue.isEmpty())
myQueue.printQ()

# prints
# True
# False
# [12,135,1,5,19,123,2222,]