简体   繁体   English

Python链接列表追加

[英]Python Linked List Append

I made an append method for an UnorderedList() class that works fine in my IDLE window but when its assigned to the University's test of: 我为一个UnorderedList()类创建了一个append方法,它在我的IDLE窗口中工作正常但是当它被分配给大学的测试时:

my_list = UnorderedList()
my_list.append(13)
for num in my_list: 
    print(num, end=" ")
print()    

it returns an error: AttributeError: Nonetype object has no attribute 'getNext' . 它返回一个错误: AttributeError: Nonetype object has no attribute 'getNext'

Here is the append method: 这是append方法:

def append(self,item):
    current = self.head
    while current.getNext() != None:
        current = current.getNext()
    current.setNext(Node(item))

Here is the rest of my classes and code: 这是我的其他类和代码:

class Node:
    def __init__(self,initdata):
        self.data = initdata
        self.next = None

    def getData(self):
        return self.data

    def getNext(self):
        return self.next

    def setData(self,newdata):
        self.data = newdata

    def setNext(self,newnext):
        self.next = newnext

class UnorderedList:

    def __init__(self):
        self.head = None
        self.count = 0

    def append(self,item):
        current = self.head
        while current.getNext() != None:
            current = current.getNext()
        current.setNext(Node(item))

Why is the test returning that error and how I can fix my append method? 为什么测试会返回该错误以及如何修复我的追加方法?

The problem is here in the append method: 问题在于append方法:

def append(self,item):
    current = self.head
    while current.getNext() != None:
        current = current.getNext()
    current.setNext(Node(item))

In the first iteration, the value of current is self.head , which is set to None initially, and you don't check for it. 在第一次迭代中, current的值是self.head ,最初设置为None ,并且不检查它。

So instead, alter this and introduce checks for this condition ad below: 所以相反,改变这一点并在下面引入对这种情况的检查:

def append(self,item):
    current = self.head
    if current:
        while current.getNext() != None:
            current = current.getNext()
        current.setNext(Node(item))
    else:
        self.head = Node(item)

PS: you are also using a variable self.count , which you are not updating. PS:你也在使用变量self.count ,你没有更新。 You might want to update the same as well. 您可能也希望更新相同的内容。

your append method works fine but it traverses the list until it finds the last node - which makes it O(n). 你的append方法工作正常,但它遍历列表,直到找到最后一个节点 - 这使得它成为O(n)。 If you keep track of the last node, you can make an append which is O(1): 如果跟踪最后一个节点,可以添加一个O(1):

def append_O1(self, item):
        temp = Node(item)
        last = self.tail
        last.setnext(temp)
        self.tail = temp
        self.length += 1

To enable this your list class constructor should be: 要启用此功能,列表类构造函数应为:

class unorderedList:
    def __init__(self):
        self.head = None
        self.tail = None
        self.length = 0

Adding little more elaborated append methods 添加更多精心设计的追加方法

Method to insert at the beginning 在开头插入的方法

def inserAtBeginning(self, item):
    newNode = Node(item)
    newNode.setdata(item)
    if self.listLength() == 0:
        self.head = newNode
    else:
        newNode.setnext(self.head)
        self.head = newNode

Method to insert at the end 最后插入的方法

def insertAtEnd(self, item):
    newNode = Node(item)
    newNode.setdata(item)
    current = self.head
    while current.getnext() != None:
        current = current.getnext()
    current.setnext(newNode)

Method to insert at the specified position 插入指定位置的方法

def insertAtPos(self, pos, item):
    if pos > self.listLength() or pos < 0:
        return None
    if pos == 0:
        self.inserAtBeginning(item)
    else:
        if pos == self.listLength():
            self.insertAtEnd(item)
        else:
            newNode = Node(item)
            newNode.setdata(item)
            current = self.head
            count = 0
            while count < pos - 1:
                count += 1
                current = current.getnext()
            newNode.setnext(current.getnext())
            current.setnext(newNode)

This O(n) implementation should work for an empty list too : 这个O(n)实现也适用于空列表:

def append(self,item):
    current = self.head
    if current == None:
        self.head = Node(item)
    else:
        while current.getNext() != None:
            current = current.getNext()

        current.setNext(Node(item))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM