1

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,]
1
  • As a style comment: -- your code seems to mix the usage of class variables and instance variables. There is a difference in their usage. You even have some variables as both class and instance variables (i.e. data, max_Size) which I wouldn't suggest since it creates confusing code. Commented Feb 1, 2022 at 13:32

2 Answers 2

1
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

Sign up to request clarification or add additional context in comments.

1 Comment

you might want to explicit that self.r =+ 1 is equivalent to self.r = (+1) ;)
0

I don't know what is the use of the r and [0.0]*maxSize but this code works:

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,]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.