英文:
Tail element not getting inserted at the last in Golang Linked List
问题
我在Golang中有一个双向链表,代码如下:
type node struct {
value string
next *node
prev *node
}
type list struct {
head *node
tail *node
length int
}
我想在链表的末尾插入元素。所以我需要做三件事:
- 改变当前最后一个节点的next指针
- 改变新节点的prev指针
- 将新节点指向nil
我正在按照这个步骤进行操作,但是prev指针似乎没有指向前一个最后一个节点,因为从后面打印出来的结果不正确。你能找出问题所在吗?我在最后添加了单词"Pink"。以下是插入函数的代码:
func (listReceiver *list) insertLast(incomingValue string) {
printNewLine := fmt.Println
newNode := node{value: incomingValue}
currentNode := listReceiver.head
if listReceiver.head == nil {
listReceiver.head = &newNode
listReceiver.tail = &newNode
fmt.Printf("New head -- %s", listReceiver.head.value)
printNewLine()
listReceiver.length++
} else {
for currentNode.next != nil {
printNewLine(currentNode.value)
currentNode = currentNode.next
}
currentNode.next = &newNode
newNode.next = nil
newNode.prev = currentNode
fmt.Printf("New Tail -- %s ", newNode.value)
printNewLine()
listReceiver.length++
}
}
这是打印语句的结果:
Linked List From Front --
->R->Kanak->Z->Zubin->A->Nani->US->Arjun->Pink
Linked List From Tail --
->Arjun->US->Nani->A->Zubin->Z->Kanak->R
英文:
I have a doubly linked list in Golang. this is it
type node struct {
value string
next *node
prev *node
}
type list struct {
head *node
tail *node
length int
}
I want to insert element at the last of the list. So I need to do 3 things :
-- Change the next pointer of the current Last
-- Change the prev pointer of the new Node
-- point new node to nil
I am doing exactly the same but the prev pointers seems to not point to the previous last node because its not getting printed from the back. Can you spot the issue. I added the word "Pink" in the last. This is the function to it.
func (listReceiver *list) insertLast(incomingValue string) {
printNewLine := fmt.Println
newNode := node{value: incomingValue}
currentNode := listReceiver.head
if listReceiver.head == nil {
listReceiver.head = &newNode
listReceiver.tail = &newNode
fmt.Printf("New head -- %s", listReceiver.head.value)
printNewLine()
listReceiver.length++
} else {
for currentNode.next != nil {
printNewLine(currentNode.value)
currentNode = currentNode.next
}
currentNode.next = &newNode
newNode.next = nil
newNode.prev = currentNode
fmt.Printf("New Tail -- %s ", newNode.value)
printNewLine()
listReceiver.length++
}
}
These are the print statements
Linked List From Front --
->R->Kanak->Z->Zubin->A->Nani->US->Arjun->Pink
Linked List From Tail --
->Arjun->US->Nani->A->Zubin->Z->Kanak->R
答案1
得分: 3
你已经拥有了列表的尾部,不需要使用for currentNode.next != nil {}
进行迭代。只需将tail.next
和list.tail
指向newNode
。
在else块中,你需要设置listReceiver.tail = &newNode
。
无论如何,都应该设置listReceiver.tail = &newNode
,所以可以放在if-else
块之外。
英文:
EDIT----
You already have the tail of the list, no need to itreate with for currentNode.next != nil {}
. Just point the tail.next
and list.tail
to newNode.
In else block you need to set listReceiver.tail = &newNode
In any case listReceiver.tail = &newNode
should be set so can be outside the if-else
block
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论