简体   繁体   English

链表如何到第二个节点

[英]how does the linked list goes to the second node

so I am learning linked lists in python, but I didn't understand how does the linked list goes to the second node as it assigns the next node as the root node, please explain it to me, thanks.所以我在python中学习链表,但我不明白链表如何进入第二个节点,因为它将下一个节点指定为根节点,请向我解释,谢谢。 the code编码

class Node
    def __init__(self,d,n=None,p=None):
        self.data=d
        self.next_node=n
        self.previous_node=p
    def __str__(self):
        return ('(' + str(self.data) + ')')

class linked_list:
    def __init__(self,r=None):
        self.root=r
        self.size=0
    def add(self,d):
        new_node=Node(d,self.root)#here i didn't understand how we assign the next node
        self.root=new_node
        self.size +=1
        
    def find(self,d):
        this_node=self.root
        while this_node is not None:
            if this_node.data==d:
                print(this_node.next_node)
                return this_node.data
            else:
                this_node = this_node.next_node
        return None
    def remove(self,d):
        this_node=self.root
        previouse_node=None
        while this_node is not None:
            if this_node.data==d:
                if previouse_node is not None:
                    previouse_node.next_node=this_node.next_node
                else:
                    self.root=this_node.next_node
                    self.size -=1
            else:
                previouse_node=this_node
                this_node=this_node.next_node
        return False
    def print_list(self):
        this_node = self.root
        while this_node is not None:
            print(this_node, end='->')
            this_node = this_node.next_node
        print('None')
l_list=linked_list()
l_list.add('4')
l_list.add('40')
l_list.add('5')
l_list.print_list()

#//////////////////////////////////////////////////////////////////////////////////// #///////////////////////////////////////////////// /////////////////////////////////////

In the add function we create a new node that will become our new root aka our new first element.在 add 函数中,我们创建了一个新节点,它将成为我们的新根,也就是我们的新第一个元素。 Therefore we first assign the current root as the next_node of our new node.因此,我们首先将当前根分配为新节点的 next_node。 Afterwards we make our new node the new root.之后,我们将新节点设为新的根节点。

The linked_list.root attribute actually acts like a tail. linked_list.root属性实际上就像一条尾巴。 When a new node is added, it becomes the next_node of the original root and the root is assigned with that new node (making its content a tail, not a root).当添加一个新节点时,它成为原始rootnext_node并且root被分配给新节点(使其内容成为尾,而不是根)。

Also, the Node class suggests a doubly linked list but its previous_node value is not properly assigned by linked_list.add.此外,Node 类建议使用双向链表,但它的previous_node值没有由 links_list.add 正确分配。

If that is not your code, it is not a good example to learn from.如果这不是您的代码,那么它就不是一个值得学习的好例子。 If it is, then it needs more work and you should probably draw on paper the links that should result from adding a node to the list.如果是,那么它需要更多的工作,您可能应该在纸上画出将节点添加到列表中应该产生的链接。

For example:例如:

linked_list.root
               |
               v
    None <-- node1 --> None

linked_list.add(node2) ...

What should happen:应该发生什么:

linked_list.root
               |
               v
    None <-- node1 --+
               ^     |
               |     v 
               +--- node2 --> None
              

What actually happens:实际发生的情况:

    linked_list.root        # points to the last node added (i.e. tail)
                   |
                   v
        None <-- node2 --> None  # missing link to node1
                   ^       
                   |        
  None <-- node1 --+ 
              

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

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