在链表的末尾插入一个元素。

huangapple go评论95阅读模式
英文:

Insert item at the end of a linked list

问题

我对Go语言非常陌生,但我可以帮你翻译这段代码。以下是翻译好的代码:

package main

import "fmt"

type Node struct {
  value int
  next *Node
}

func main() {

  var head *Node
  for i := 1; i <= 10; i++ {
    insert(&head, i)
  }

  print_list(head)
}

func print_list(node_pointer *Node) {
  if node_pointer == nil {
    return
  } else {
    node := *node_pointer
    fmt.Printf("%d\n", node.value)
    print_list(node.next)
  }
}

func insert(node_pointer **Node, value int) {
  var new_node Node
  new_node.value = value

  if *node_pointer == nil {
    fmt.Printf("Empty list\n")
    *node_pointer = &new_node
  } else {
    var cur_node Node = **node_pointer
    for cur_node.next != nil {
      cur_node = *cur_node.next
    }
    cur_node.next = &new_node
    fmt.Printf("Add %d\n", (*cur_node.next).value)
  }
}

输出结果为:

Empty list
Add 2
Add 3
Add 4
Add 5
Add 6
Add 7
Add 8
Add 9
Add 10
1

换句话说,我无法将新的节点插入到链表的末尾。我认为问题出在cur_node.next = &new_node这一行,它只在本地进行更新,但我不知道如何修复这个问题。

英文:

I am very new to go and deicide to implement a linked list. Here is my source code

package main

import &quot;fmt&quot;

type Node struct {
  value int
  next *Node
}

func main() {

  var head *Node
  for i := 1; i &lt;= 10; i++ {
    insert(&amp;head, i)
  }

  print_list(head)
}

func print_list(node_pointer *Node) {
  if (node_pointer == nil) {
    return
  } else {
    node := *node_pointer
    fmt.Printf(&quot;%d\n&quot;, node.value)
    print_list(node.next)
  }
}

func insert(node_pointer **Node, // pointer to a pointer to a Node
    value int) {
  var new_node Node
  new_node.value = value

  if (*node_pointer == nil) {
    fmt.Printf(&quot;Empty list\n&quot;)
    *node_pointer = &amp;new_node
  } else {
    var cur_node Node = **node_pointer
    for cur_node.next != nil {
      cur_node = *cur_node.next
    }
    cur_node.next = &amp;new_node
    fmt.Printf(&quot;Add %d\n&quot;, (*cur_node.next).value)      }
}

The output is:

Empty list
Add 2
Add 3
Add 4
Add 5
Add 6
Add 7
Add 8
Add 9
Add 10
1

In other words, I cannot insert a new Node at the end of the linked list. I believe that it is caused by cur_node.next = &amp;new_node which only makes the update locally, but don't know how to fix this.

答案1

得分: 2

问题出在你的插入函数中 - 这是一个修正后的版本

func insert(node_pointer **Node, // 指向指针的指针
	value int) {
	var new_node Node
	new_node.value = value

	if *node_pointer == nil {
		fmt.Printf("空列表\n")
		*node_pointer = &new_node
	} else {
		var cur_node *Node = *node_pointer
		for cur_node.next != nil {
			cur_node = cur_node.next
		}
		cur_node.next = &new_node
		fmt.Printf("添加 %d\n", (*cur_node.next).value)
	}
}

Playground

英文:

The problem is in your insert function - here is a corrected version

func insert(node_pointer **Node, // pointer to a pointer to a Node
	value int) {
	var new_node Node
	new_node.value = value

	if *node_pointer == nil {
		fmt.Printf(&quot;Empty list\n&quot;)
		*node_pointer = &amp;new_node
	} else {
		var cur_node *Node = *node_pointer
		for cur_node.next != nil {
			cur_node = cur_node.next
		}
		cur_node.next = &amp;new_node
		fmt.Printf(&quot;Add %d\n&quot;, (*cur_node.next).value)
	}
}

Playground

答案2

得分: 1

你的错误是因为你获取了节点的值,而不是指针:

    var cur_node Node = **node_pointer

*note_pointer 是指向链表头部的指针,但你使用了 **node_pointer,然后获取了值,将该值复制给了 cur_node,然后新的节点将附加到 cur_node,与头节点无关。所以当你从头节点打印时,你只能获取到头节点的值。

修复的解决方案就像 @Nick Craig-Wood 所说的那样。

英文:

Your error is because you get the node value,not the pointer here:

    var cur_node Node = **node_pointer

*note_pointer is a pointer pointed to head of list,but you are using **node_pointer,then you get the value,then copy this value to cur_node, afterwards new node will be append to cur_node,no relevant to head node.So when your print form head node,you can only get the value of the head node.

The fixed solution is like @Nick Craig-Wood said.

huangapple
  • 本文由 发表于 2016年3月8日 06:11:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/35855130.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定