【问题标题】:My insert node at tail function doesn't change the value of the tail我在尾部函数中的插入节点不会改变尾部的值
【发布时间】:2018-12-18 22:22:02
【问题描述】:

所以我试图编写一个函数,它将链表的头部和数据作为参数,并在链表的尾部附加一个节点。当它从函数内部打印“新”头时,它看起来工作正常(请注意,链表的头最初不是节点,它实际上等于 None)(head 成为 head.data = 0 and head.next = None),但是当我在调用函数后打印链表的头部时它不返回节点,实际上它返回None,这意味着函数没有在尾部插入节点最初是头部。有人可以解释为什么头部的价值没有改变吗?

class Node:

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

class LinkedList:

    def __init__(self):
        self.head = None

def InsertNode(head, data):

    if head == None:
        print("head == None, so head will be a node now")
        head = Node(data)
        print("now, head is a node where head.data = {} and head.next = {}".format(head.data, head.next))

    else: 
        tail = head
        while tail.next != None:
            tail = tail.next
            tail.next = Node(data)

def PrintLL(linkedList):

    node = linkedList.head

    while node != None:
        print (node.data)

    node = node.next

llist = LinkedList()
##print("llist.head == {}".format(llist.head))

InsertNode(llist.head, 0)
print("after InsertNode with data = 0, llist.head == {}".format(llist.head))

【问题讨论】:

    标签: python-3.x linked-list nodes


    【解决方案1】:

    以下是固定代码:

    class Node:
    
        def __init__(self, data):
            self.data = data
            self.next = None
    
    class LinkedList:
    
        def __init__(self):
            self.head = None
    
    def InsertNode(node, data):
    
        if node.head == None:
            print("head == None, so head will be a node now")
            node.head = Node(data)
            print("now, head is a node where head.data = {} and head.next = {}".format(node.head.data, node.head.next))
    
        else: 
            tail = node.head
            while tail.next != None:
                tail = tail.next
                tail.next = Node(data)
    
    def PrintLL(linkedList):
    
        node = linkedList.head
    
        while node != None:
            print (node.data)
    
        node = node.next
    
    llist = LinkedList()
    ##print("llist.head == {}".format(llist.head))
    
    InsertNode(llist, 0)
    print("after InsertNode with data = 0, llist.head == {}".format(llist.head))
    

    发生的事情是你传递了一个指向头部的指针:

    InsertNode(llist.head, 0)
    

    InsertNode 然后会为该变量分配一个新的节点实例:

    head = Node(data)
    

    由于头指针是通过赋值传递的,函数InsertNode本地的head的版本发生了变化,但原来的llist.head不受影响。

    【讨论】:

      【解决方案2】:

      您需要将LinkedList 对象本身传递给InsertNode,而不是其head 属性。通过将head 属性传递给InsertNode,对其进行的任何新分配都不会反映在原始对象上。

      class Node:
      
          def __init__(self, data):
              self.data = data
              self.next = None
      
      class LinkedList:
      
          def __init__(self):
              self.head = None
      
      def InsertNode(linkedList, data):
      
          if linkedList.head == None:
              print("head == None, so head will be a node now")
              linkedList.head = Node(data)
              print("now, head is a node where head.data = {} and head.next = {}".format(linkedList.head.data, linkedList.head.next))
      
          else:
              tail = linkedList.head
              while tail.next != None:
                  tail = tail.next
                  tail.next = Node(data)
      
      def PrintLL(linkedList):
      
          node = linkedList.head
      
          while node != None:
              print (node.data)
      
          node = node.next
      
      llist = LinkedList()
      
      InsertNode(llist, 0)
      print("after InsertNode with data = 0, llist.head == {}".format(llist.head.data))
      

      【讨论】:

      • 是的,这有帮助,但在最初的问题上,函数中传递的是链接列表的 head 属性,我无法更改这部分代码(不知道为什么他们这样做了)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-03
      • 2020-04-10
      • 2021-08-13
      • 1970-01-01
      • 2015-12-14
      相关资源
      最近更新 更多