I have a functioning doubly linked list DS that I wrote in python. Using that I want to write queue DS. The specific problem I encounter is in the below.
And I'm not sure whether my way of implementing queue from doubly linked list is correct or not. I googled but didn't find an example of this, in general how do you create a data structure using another data structure in python?
when I run
import import_ipynb
from Queue import *
x=Queue(2)
x.offer(3)
the error it gives me is Queue.__init__() takes from 0 to 1 positional arguments but 2 were given
. But I only pass one variable?
This is DoublyLinkedList
class Node:
def __init__(self,data):
self.data=data
self.prev=None
self.next=None
def __repr__(self):
return f"<Node data:{self.data} Previous Node:{self.prev} Next Node:{self.next}>"
class DoublyLinkedList:
def __init__(self):
self.size=0
self.head=None
self.tail=None
# add an element to the tail of the linked list
def addL(self,data):
new_node=Node(data)
if self.isEmpty()==True:
self.head=self.tail=new_node
else:
self.tail.next=new_node
new_node.prev=self.tail
self.tail=new_node
self.size+=1
This is Queue
import import_ipynb
from DoublyLinkedList import *
class Queue:
def __init__(firstElem=None):
list=DoublyLinkedList()
list.addL(firstElem)
# add an element to back of the queue
def offer(val):
return list.addL(val)
You are forgetting to add the self
attribute to the Queue class methods, by default it is the first argument.
Try changing the code to this:
class Queue:
def __init__(self, firstElem=None):
self.list=DoublyLinkedList()
self.list.addL(firstElem)
# add an element to back of the queue
def offer(self, val):
return self.list.addL(val)
Edit: Since you are trying to access the class attribute list
from another scope inside the class, you should use it with the self before it, just so you reference the class attribute.