Print_list()如何通过Python中的链接列表迭代?

发布于 2025-02-07 19:46:44 字数 1028 浏览 1 评论 0原文

代码可以工作并输出1,5和6。

在print_list()之后,打印第一个值(1),temp = temp.next如何导致temp.value在下一个通过中为5。

我错过了一些东西,但我一直在这个工作了几天。我只是不明白temp.next如何指向5个或下一个节点。

我也明白,临时,头和尾巴是指针。请帮助,我正在尝试了解每条代码在做什么。

class Node:

    def __init__(self, value):
        self.value = value
        self.next = None

class LinkedList:

    def __init__ (self, value):
        new_node = Node(value)
        self.head = new_node
        self.tail = new_node
        self.lenth = 1

    def print_list(self):
        temp = self.head 
        while temp is not None:
            print(temp.value)
            temp = temp.next

    def append(self, value):   
        new_node = Node(value)
        if self.head is None:
            self.head = new_node
            self.tail = new_node
        else: 
            self.tail.next = new_node
            self.tail = new_node
        self.lenth =+ 1
        return True


my_linked_list = LinkedList(1)
my_linked_list.append(5)
my_linked_list.append(6)
my_linked_list.print_list()

The code works and outputs 1,5, and 6.

After print_list(), prints the first value ( 1 ), how does temp = temp.next result in temp.value being 5 on the next pass.

I am missing something but I've been at this for days. I just don't understand how temp.next is pointing at 5 or the next node.

I also understand that temp, head, and tail are pointers. Please help, I am trying to learn what every line of code is doing.

class Node:

    def __init__(self, value):
        self.value = value
        self.next = None

class LinkedList:

    def __init__ (self, value):
        new_node = Node(value)
        self.head = new_node
        self.tail = new_node
        self.lenth = 1

    def print_list(self):
        temp = self.head 
        while temp is not None:
            print(temp.value)
            temp = temp.next

    def append(self, value):   
        new_node = Node(value)
        if self.head is None:
            self.head = new_node
            self.tail = new_node
        else: 
            self.tail.next = new_node
            self.tail = new_node
        self.lenth =+ 1
        return True


my_linked_list = LinkedList(1)
my_linked_list.append(5)
my_linked_list.append(6)
my_linked_list.print_list()

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

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

发布评论

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

评论(1

苹果你个爱泡泡 2025-02-14 19:46:44

当思考链接列表数据结构时,您可以将结构概念化为链条列表,将第一个框连接到下一个框,该框连接到下一个,依此类推,直到您到达最后一个盒子什么都没有束缚。这是一个粗略的视觉演示:

[5] - > [7] - > [9] - > [3] - >无

通过在链接列表的第一个节点(aka the head )中表示,框之间的链接表示,请参考列表中的下一个框。在您的代码示例中,此引用存储在Next属性中。下一个节点具有存储在 it 中的参考,该参考指向下一个节点。而且这个过程继续进行,创造了我上面提到的类似链的行为。

print_list正在做什么数据,然后使用此下一个框引用以获取列表中的下一个框。此过程一直持续到达到最后一个框,该过程没有进一步的参考,因此代码终止(这是,而温度不是没有:条件检查)。

我评论了下面的每一行,以将我所说的内容更具体地连接到实际代码:

def print_list(self):
#将温度设置为等于列表中的第一个框
temp = self.head

# temp will be set to none when we try to set it to be equal to 
# the next box the last box in the list is pointing to. So we
# know we've reached the end of the list when temp is none, 
# meaning we should break out of the loop and finish.
while temp is not None:
    # Print the data in the current box, starting with head...
    print(temp.value)
    # And then get the next box in the list so we can print it's data.
    temp = temp.next

When thinking about a linked list data structure, you can conceptualize the structure as a list of boxes chained together, with the first box connected to the next, that one connected to the next, and so on, until you reach the last box and there's nothing it's chained to. Here's a rough visual demonstration:

[5] -> [7] -> [9] -> [3] -> None

This linking between boxes is denoted by having the first node of the linked list (a.k.a the head), have a reference to the next box in the list. In your code example this reference is stored in the next attribute. And this next node has a reference stored in it, which points to the next, next node. And this process continues on, creating the chain-like behavior I mentioned above.

What print_list is doing is starting at the head of the linked list, printing the head's data, and then using the reference the head has to the next box in the linked list to get that next box, print it's data, and then use this next boxes reference to get the next box in the list. This process continues on until the last box is reached, which has no further reference, so the code terminates (this is what the while temp is not None: condition checks for).

I commented each line below to connect what I said a bit more concretely to the actual code:

def print_list(self):
# Set temp to be equal to the first box in the list
temp = self.head

# temp will be set to none when we try to set it to be equal to 
# the next box the last box in the list is pointing to. So we
# know we've reached the end of the list when temp is none, 
# meaning we should break out of the loop and finish.
while temp is not None:
    # Print the data in the current box, starting with head...
    print(temp.value)
    # And then get the next box in the list so we can print it's data.
    temp = temp.next
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文