关于测试后的双重链表错误

时间:2015-06-19 15:26:42

标签: python doubly-linked-list

我有以下三个类:

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            

我尝试将对象添加到DLList并尝试搜索它。它给我带来了以下错误:

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'

1 个答案:

答案 0 :(得分:1)

您的DLLNode班级没有position属性。然而,在搜索功能的末尾,您有一行return current.position。 Python不知道如何处理它,因为它没有定义。您可能希望为函数添加某种计数器,并在迭代时递增它,然后返回它。