Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

I’ve wanted to make simple queue with self parameter, however face with output problem in which my values I’ve added to the queue are only first and last on first and second positions, all in between is being ignored and placed zero’s instead(as created).

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()

The output is:

True
False
[12,2222,0.0,0.0,0.0,0.0,0.0,]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

should be

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

You should also probably be assigning:

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

all as instance variables in the constructor instead of declaring them as class variables

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading