I have the following three classes:
class DLLNode(object):
def __init__ (self, data, prev_node=None, next_node=None):
self.data=data
self.prev_node=prev_node
self.next_node=next_node
def __str__ (self):
return str(self.data)
class DLList(object):
def __init__(self):
self.head=None
self.tail=None
def add_to_head(self,add_obj):
newNode=DLLNode(add_obj)
if self.head==None:
self.head=self.tail=newNode
self.head.prev_node=self.tail.next_node=None
else:
self.head.prev_node=newNode
newNode.next_node=self.head
self.head=newNode
self.head.prev_node=None
def add_to_tail(self, add_obj):
newNode=DLLNode(add_obj)
if self.head==None:
self.head=self.tail=newNode
self.head.prev_node=self.tail.next_node=None
else:
self.tail.next_node=newNode
newNode.prev_node=self.tail
self.tail=newNode
self.tail.next_node=None
def remove_head(self):
if self.head==self.tail:
self.prev_node=self.next_node=self.head=self.tail=None
return
if self.head != self.tail:
self.head=self.head.next_node
self.head.prev_node=None
return self.head
def remove_tail(self):
if self.head==self.tail:
self.prev_node=self.next_node=self.head=self.tail=None
return
if self.head != self.tail:
self.tail=self.tail.prev_node
self.tail.next_node=None
return self.tail
def search (self, element):
current=self.head
if current == None:
return -1
else:
while current != None:
if current == None:
return -1
else:
if current.data==element:
return current.position
else:
current=current.next_node
class SortedList(object):
def __init__(self):
self.head=None
self.tail=None
def add (self, add_obj):
newNode=DLLNode(add_obj)
current=self.head
if current==None:
self.head=self.tail=newNode
else:
while add_obj>current.data:
current=current.next_node
newNode.next_node=current
newNode.prev_node=current.prev_node
current.prev_node.next_node=newNode
current.prev_node=newNode
def remove (self, element):
current=self.head
while element != current.data:
current=current.next_node
current.next_node.prev_node=current.prev_node
current.prev_node.next_node=current.next_node
current=None
def middle (self):
length=0
current=self.head
while current != None:
current=current.next_node
length =+ 1
headNode=self.head
tailNode=self.tail
if length/2%1:
while headNode != tailNode:
headNode=headNode.next_node
tailNode=tailNode.prev_node
return headNode
elif length/2%0:
tailnode=tailNode.prev_node
while headNode != tailNode:
headNode=headNode.next_node
tailNode=tailNode.prev_node
return headNode
I tried to add object to DLList and tried to search for it. And it brings me the follow error:
Traceback (most recent call last):
File "C:\Program Files (x86)\Wing IDE 101 5.1\src\debug\tserver\_sandbox.py", line 1, in <module>
# Used internally for debug sandbox under external interpreter
File "C:\Program Files (x86)\Wing IDE 101 5.1\src\debug\tserver\_sandbox.py", line 65, in search
builtins.AttributeError: 'DLLNode' object has no attribute 'position'
Your
DLLNode
class has noposition
attribute. Yet, near the end of your search function, you have the linereturn current.position
. Python doesn't know what to do with that since there is no definition for it. You may wish to add some sort of counter to the function and increment it as you iterate and then return that.