简体   繁体   中英

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. 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. Therefore we first assign the current root as the next_node of our new node. Afterwards we make our new node the new root.

The linked_list.root attribute actually acts like a tail. 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).

Also, the Node class suggests a doubly linked list but its previous_node value is not properly assigned by linked_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 --+ 
              

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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