【问题标题】:Linked List Python链表 Python
【发布时间】:2019-03-13 08:36:31
【问题描述】:
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

class LinkedList:
    def __init__(self):
        self.head = None

    def __len__(self):
        cur = self.head
        count = 0
        while cur is not None:
            count += 1
            cur = cur.next
        return count

    def append(self, item):
        cur = self.head
        while cur is not None:
            cur = cur.next
        cur.next = ?

我正在尝试追加到链接列表,但我不能使用 'cur.next',因为 cur 没有属性 'next'。对此有何提示?

谢谢!

我的测试用例:

def test_append_empty() -> None:
    lst = LinkedList()
    lst.append(1)
    assert lst.head.data == 1


def test_append_one() -> None:
    lst = LinkedList()
    lst.head = Node(1)
    lst.append(2)
    assert lst.head.next.data == 2

【问题讨论】:

  • 无论如何你都必须创建一个元素。self.head == None 有一个特殊情况。将其分配给 self.head 如果它是 None
  • 这不是有效的python代码
  • @MadPhysicist 你在说 OP 吗?
  • @PatrickArtner 哦,我编辑了 OP。对不起!

标签: python python-3.x python-2.7 linked-list singly-linked-list


【解决方案1】:

我希望通过 mad_ 的明确示例,您已经理解了您仍然需要处理这个节点概念的想法。

链表不仅是值的列表,还是链接的列表。

由于您似乎有兴趣在一个课程中完成,这里有一个快速实现:

class LinkedList:
    def __init__(self, item=None):
        self.next = None
        self.val = item

    def __len__(self):
        cur = self
        count = 1 if self.val is not None else 0
        while cur.next is not None:
            count += 1
            cur = cur.next
        return count    

    def append(self, item):
        if not self.val:
            self.val = item
            return

        cur = self
        while cur.next is not None:
            cur = cur.next
        cur.next = LinkedList(item)

编辑:

由于您在问题中包含了 mad_ 的 len() 成员,因此我还添加了适用于此类的成员。

以下是一些用法示例:

myList = LinkedList()
myList.append('a')
myList.append('b')
myList.append('c')
myList.append('d')
print(len(myList))

【讨论】:

  • 我的 LinkedList 类中没有 self.val,所以我无法关联:(
  • val 实际上是每个节点的值: myList.val == 'a', myList.next.val == 'b' ... 无论是通过单独的节点类还是通过将它直接放在 LinkedList 类中,您需要一个 val 成员,您可以将其称为 data 或 item 或任何您喜欢的名称!
  • nvm 我在不更改类文档的情况下修复了它
【解决方案2】:

你必须寻找self.headNone 的情况,因为接下来 Nonetype 将没有属性。还要迭代直到您发现节点中的下一个指针为无。所以它应该是cur.next is not None 而不是cur is None,它正在查找节点是否为无。从逻辑上讲,您不能将任何内容附加到 None

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

class LinkedList:
    def __init__(self):
        self.head = None

    def __len__(self):
        if not self.head:
            return 0
        cur = self.head
        count = 0
        while cur is not None:
            count += 1
            cur = cur.next
        return count

    def append(self, item):
        if not self.head:
            self.head=Node(item)
            return
        cur = self.head
        while cur.next is not None:
            cur = cur.next
        cur.next = Node(item)

测试用例1

l=LinkedList()
l.append(1)
l.__len__() #prints 1

测试用例2

l=LinkedList()
l.append(2)
l.append(3)
l.__len__() #2

temp=l.head
while(temp):
    print(temp.data)
    temp=temp.next

也包括 OP 的测试用例

def test_append_empty():
    lst = LinkedList()
    lst.append(1)
    print(lst.head.data == 1)


def test_append_one():
    lst = LinkedList()
    lst.head = Node(1)
    lst.append(2)
    print(lst.head.next.data == 2)

test_append_empty() #True
test_append_one() #True

【讨论】:

  • 你不觉得最后一行会有属性错误吗?
  • 我有。我得到了一个 AttributeError!
  • @Kaisr 很难相信。自从您执行后,我可能已经编辑了我的答案。因为我已经运行了测试用例,它对我来说很好。如果没有,请包含完整的堆栈跟踪
  • 我可以通过电子邮件将整个代码发送给您,无需进行编辑。我也有自己的测试用例
  • 不,请在此处发布。也会对其他人有益。因为我看不到执行上述代码的任何错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-13
  • 2012-02-17
  • 2018-03-28
  • 2016-03-23
  • 2020-06-28
  • 2021-10-13
相关资源
最近更新 更多