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

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

Insert item at the end of a linked list

问题

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

  1. package main
  2. import "fmt"
  3. type Node struct {
  4. value int
  5. next *Node
  6. }
  7. func main() {
  8. var head *Node
  9. for i := 1; i <= 10; i++ {
  10. insert(&head, i)
  11. }
  12. print_list(head)
  13. }
  14. func print_list(node_pointer *Node) {
  15. if node_pointer == nil {
  16. return
  17. } else {
  18. node := *node_pointer
  19. fmt.Printf("%d\n", node.value)
  20. print_list(node.next)
  21. }
  22. }
  23. func insert(node_pointer **Node, value int) {
  24. var new_node Node
  25. new_node.value = value
  26. if *node_pointer == nil {
  27. fmt.Printf("Empty list\n")
  28. *node_pointer = &new_node
  29. } else {
  30. var cur_node Node = **node_pointer
  31. for cur_node.next != nil {
  32. cur_node = *cur_node.next
  33. }
  34. cur_node.next = &new_node
  35. fmt.Printf("Add %d\n", (*cur_node.next).value)
  36. }
  37. }

输出结果为:

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

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

英文:

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

  1. package main
  2. import &quot;fmt&quot;
  3. type Node struct {
  4. value int
  5. next *Node
  6. }
  7. func main() {
  8. var head *Node
  9. for i := 1; i &lt;= 10; i++ {
  10. insert(&amp;head, i)
  11. }
  12. print_list(head)
  13. }
  14. func print_list(node_pointer *Node) {
  15. if (node_pointer == nil) {
  16. return
  17. } else {
  18. node := *node_pointer
  19. fmt.Printf(&quot;%d\n&quot;, node.value)
  20. print_list(node.next)
  21. }
  22. }
  23. func insert(node_pointer **Node, // pointer to a pointer to a Node
  24. value int) {
  25. var new_node Node
  26. new_node.value = value
  27. if (*node_pointer == nil) {
  28. fmt.Printf(&quot;Empty list\n&quot;)
  29. *node_pointer = &amp;new_node
  30. } else {
  31. var cur_node Node = **node_pointer
  32. for cur_node.next != nil {
  33. cur_node = *cur_node.next
  34. }
  35. cur_node.next = &amp;new_node
  36. fmt.Printf(&quot;Add %d\n&quot;, (*cur_node.next).value) }
  37. }

The output is:

  1. Empty list
  2. Add 2
  3. Add 3
  4. Add 4
  5. Add 5
  6. Add 6
  7. Add 7
  8. Add 8
  9. Add 9
  10. Add 10
  11. 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

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

  1. func insert(node_pointer **Node, // 指向指针的指针
  2. value int) {
  3. var new_node Node
  4. new_node.value = value
  5. if *node_pointer == nil {
  6. fmt.Printf("空列表\n")
  7. *node_pointer = &new_node
  8. } else {
  9. var cur_node *Node = *node_pointer
  10. for cur_node.next != nil {
  11. cur_node = cur_node.next
  12. }
  13. cur_node.next = &new_node
  14. fmt.Printf("添加 %d\n", (*cur_node.next).value)
  15. }
  16. }

Playground

英文:

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

  1. func insert(node_pointer **Node, // pointer to a pointer to a Node
  2. value int) {
  3. var new_node Node
  4. new_node.value = value
  5. if *node_pointer == nil {
  6. fmt.Printf(&quot;Empty list\n&quot;)
  7. *node_pointer = &amp;new_node
  8. } else {
  9. var cur_node *Node = *node_pointer
  10. for cur_node.next != nil {
  11. cur_node = cur_node.next
  12. }
  13. cur_node.next = &amp;new_node
  14. fmt.Printf(&quot;Add %d\n&quot;, (*cur_node.next).value)
  15. }
  16. }

Playground

答案2

得分: 1

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

  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:

  1. 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:

确定