python - about doubly linked list error after test -
i have following 3 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 add object dllist , tried search it. , brings me 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 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 no position attribute. yet, near end of search function, have line return current.position. python doesn't know since there no definition it. may wish add sort of counter function , increment iterate , return that.
Comments
Post a Comment