Only one argument is passed. Still I’m getting error that 2 arguments have been passed. head and tail are not being initialized to -1.
class Queue_demo:
head=-1
tail=-1
a=[]
def enqueue(data=10):
if(head==-1 and tail==-1):
head=head+1
tail=tail+1
a.append(data)
else:
tail=tail+1
a.append(data)
def dequeue():
y=a[head]
if(head==tail):
head,tail=-1,-1
else:
head=head+1
return y
q1=Queue_demo()
q2=Queue_demo()
q1.enqueue(12)
while(q1.tail==-1):
print(q1.dequeue())
There are a few issues with your code.
The one that is directly causing your error is that you have not given your methods a
selfargument. When you callq1.enqueue(12)Python translates this toQueue_demo.enqueue(q1, 12). The object the method is being called on is passed to the method as the first argument. By convention, it is usually namedself.This leads me to the second issue you’re going to encounter once you get past the wrong number of arguments error. Your instances are going to all be sharing the same set of data members, since they’re currently accessing class variables
head,tailanda, rather than instance variables. This will be very confusing, as adding an item to one queue will make it appear in all other queues too.To fix this, what you will want to do is create those variables in a constructor (which is simply a method named
__init__), rather than defining them in the class definition. Here’s what your__init__method will probably look like:Remember that Python is often different from other programming languages! You don’t need to declare your member variables, just start assigning things as values on
selfand you’ll be all set.