尾部元素在Golang链表中未能插入到最后位置。

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

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.nextlist.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

huangapple
  • 本文由 发表于 2022年9月17日 15:00:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/73752834.html
匿名

发表评论

匿名网友

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

确定