英文:
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 "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, // pointer to a pointer to a 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) }
}
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 = &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)
}
}
英文:
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("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)
}
}
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论