python链接列表在打印时接收内存地址的问题,除非双调用

发布于 2025-02-12 18:14:20 字数 947 浏览 1 评论 0原文

我正在创建一个链接的列表实现,我无法修复必须double Call node.val.val.val.val打印数据而不是内存地址的错误。

这是我的实现:

class LinkedNode:
    def __init__(self, val, nxt=None):
        self.val = val
        self.nxt = nxt

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

    def append(self, new_val):
        node = LinkedNode(new_val, None)
        if self.head:
            curr = self.head
            while curr.nxt:
                curr = curr.nxt
            curr.nxt = node
        else:
            self.head = node

    def print(self):
        curr = self.head
        while curr:
            **print(curr.val)**
            curr = curr.nxt


l1 = LinkedList()
l1.append(LinkedNode(2))
l1.append(LinkedNode(3))
l1.append(LinkedNode(4))
l1.print()

当打印功能中的 line 是“ print(curr.val)”时,该功能会打印内存地址。当线是“ print(curr.val.val)”时,该函数将打印2,3,4。

有人有可能的解决方案吗?

I am creating a Linked List implementation and I cannot fix this error of having to double call node.val.val to print the data instead of the memory address.

Here is my implementation:

class LinkedNode:
    def __init__(self, val, nxt=None):
        self.val = val
        self.nxt = nxt

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

    def append(self, new_val):
        node = LinkedNode(new_val, None)
        if self.head:
            curr = self.head
            while curr.nxt:
                curr = curr.nxt
            curr.nxt = node
        else:
            self.head = node

    def print(self):
        curr = self.head
        while curr:
            **print(curr.val)**
            curr = curr.nxt


l1 = LinkedList()
l1.append(LinkedNode(2))
l1.append(LinkedNode(3))
l1.append(LinkedNode(4))
l1.print()

When the line in the print function is "print(curr.val)", the function prints memory addresses. When the line is "print(curr.val.val)", the function prints 2,3,4.

Does anyone have a possible solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不打扰别人 2025-02-19 18:14:20

您正在将linkedNode()对象作为参数传递给.append()函数:

class LinkedNode:
    def __init__(self, value, nxt=None):
        self.val = value
        self.nxt = nxt

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

    def append(self, new_val):
        node = LinkedNode(new_val, None) #Creation of LinkedNode from integer
        if self.head:
            curr = self.head
            while curr.nxt:
                curr = curr.nxt
            curr.nxt = node
        else:
            self.head = node

    def print(self):
        curr = self.head
        while curr:
            print(curr.val)
            curr = curr.nxt


l1 = LinkedList()
l1.append(2) #Argument must be integer, not LinkedNode(integer)
l1.append(3) #Because you are already converting integer to LinkedNode on append function
l1.append(4)
l1.print()

输出:

2
3
4

You were passing a LinkedNode() object as an argument to .append() function:

class LinkedNode:
    def __init__(self, value, nxt=None):
        self.val = value
        self.nxt = nxt

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

    def append(self, new_val):
        node = LinkedNode(new_val, None) #Creation of LinkedNode from integer
        if self.head:
            curr = self.head
            while curr.nxt:
                curr = curr.nxt
            curr.nxt = node
        else:
            self.head = node

    def print(self):
        curr = self.head
        while curr:
            print(curr.val)
            curr = curr.nxt


l1 = LinkedList()
l1.append(2) #Argument must be integer, not LinkedNode(integer)
l1.append(3) #Because you are already converting integer to LinkedNode on append function
l1.append(4)
l1.print()

Output:

2
3
4
百思不得你姐 2025-02-19 18:14:20

因为在这些行中,您正在创建linkedNode对象而不是值!

l1.append(LinkedNode(2))
l1.append(LinkedNode(3))
l1.append(LinkedNode(4))

之后,您在Append函数的范围内创建了一个新的linkedNode(linkedNode(2),无)

将其更改为:

l1.append(2)
l1.append(3)
l1.append(4)

Because in these lines you are creating LinkedNode objects not values!

l1.append(LinkedNode(2))
l1.append(LinkedNode(3))
l1.append(LinkedNode(4))

After that, you created a new LinkedNode(LinkedNode(2), None) within the scope of the append function.

Change it to:

l1.append(2)
l1.append(3)
l1.append(4)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文