如何在Go语言中就地更新链表的下一个指针?

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

How to update the next pointer in place in go list?

问题

我有一个来自列表的节点指针。我想用列表中的下一个节点更新值和下一个指针。这实际上就是在给定只有该指针的情况下进行原地删除。

例如:3 -> 5 -> 8 -> 9
要删除的节点:5(只给出5的访问权限,假设不知道前一个节点)

在这种情况下,可以将节点[8]的值和下一个指针复制到节点[5]。我有以下代码。它没有删除元素。如果我尝试使用'next'关键字访问下一个指针,它会抛出错误。

package main
import (
	"container/list"
	"fmt"
)

func main() {

	l := list.New()
	l.PushFront(4)
	l.PushFront(5)
	e4 := l.PushFront(7)
	l.PushFront(6)
	l.PushBack(9)
	res := deleteNode(e4)
	fmt.Println(res)

	for e:=l.Front(); e!=nil;e=e.Next() {
		fmt.Println(e.Value)
	}

}

//ERROR
func deleteNode(node *list.Element) bool {

	if node == nil || node.Next() == nil {
		return false
	}
	var nextNode *list.Element 
	nextNode := node.next.(*list.Element)
	node.Value = node.Next().Value.(int)
	nextNode = nextNode.next.(*Element)
	return true
}

有人可以帮我解决这个问题吗?

英文:

I have a node pointer from the list. I want update the value and next pointer with the next node in the list. This is nothing but deletion in place given access to only that pointer.

For example 3 -> 5 -> 8 -> 9
Node to be deleted : 5 (given access to only 5. Assuming previous node is not known)

In this case the value and next pointer of node[8] can be copied to node[5]. I have the following code. It is not removing the element. If I try to access the next pointer using 'next' keyword it is throwing error.

package main
import (
	"container/list"
	"fmt"
)

func main() {

	l := list.New()
	l.PushFront(4)
	l.PushFront(5)
	e4 := l.PushFront(7)
	l.PushFront(6)
	l.PushBack(9)
	res := deleteNode(e4)
	fmt.Println(res)

	for e:=l.Front(); e!=nil;e=e.Next() {
		fmt.Println(e.Value)
	}

}
//ERROR
func deleteNode(node *list.Element) bool {

	if node == nil || node.Next() == nil {
		return false
	}
	var nextNode *list.Element 
	nextNode := node.next.(*list.Element)
	node.Value = node.Next().Value.(int)
	nextNode = nextNode.next.(*Element)
	return true
}

Could anyone help me with this?

答案1

得分: 1

你得到的第一个错误是:

:= 左边没有新的变量

因为 nextNode 已经存在。问题出在这两行代码上:

var nextNode *list.Element 
nextNode := node.next.(*list.Element)

在第一行中,你创建了变量 nextNode。在第二行中,你使用了短赋值 :=,它会创建一个新的变量(由左边指定),并将右边的值赋给它。只需省略第一行,你只需要其中一个:

nextNode := node.Next()

此外,你不能读取或更改节点的 next 指针,因为 Element 结构体中的 next 指针没有被导出(它以小写字母开头)。所以你想要实现的目标是无法完成的。但是你可以使用一个已定义的函数来实现:Remove(e *Element) interface{}

英文:

The (first) error you get is:

no new variables on left side of :=

Because nextNode already exists. The problem is with these 2 lines:

var nextNode *list.Element 
nextNode := node.next.(*list.Element)

In the first line you create the variable nextNode. In the second line you use the short assignment := which creates a new variable (specified by the left side) and assigns to it the value on the right side. Just leave out the first line, you only need one of those:

nextNode := node.Next()

Moreover you cannot read or change the next pointer of a node because the next pointer is not exported in the Element struct (it starts with lowercased letter). So what you try to achieve cannot be done. But you have a function defined for this: Remove(e *Element) interface{}

huangapple
  • 本文由 发表于 2015年1月13日 15:11:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/27916682.html
匿名

发表评论

匿名网友

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

确定