Golang LinkedList 移除第一个元素

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

Golang LinkedList remove first element

问题

我正在尝试从头开始在Golang中实现链表操作。但是在处理删除第一个元素时遇到了问题。我的方法是使用面向对象的风格,但似乎第一个元素没有被删除。这是我编写的代码:

type LinkedList struct {
	Value int
	next  *LinkedList
}

func (ll *LinkedList) Remove(index int) error {
	pointer := ll
	var pointerPrev *LinkedList = nil
	current := 0

	for current < index {
		pointerPrev = pointer
		pointer = pointer.next
		current++
	}

	if pointer == ll {
		ll = ll.next // 这一行有问题
		pointer = nil
	} else {
		if pointer.next == nil {
			pointerPrev.next = nil
		} else {
			pointerPrev.next = pointer.next
			pointer = nil
		}
	}

	return nil
}

有没有建议如何在不返回新的LinkedList指针的情况下实现这种删除方式?

英文:

I'm trying to implement LinkedList operation in Golang from scratch. But I found a problem when dealing with removing first element. My approach is using OOP style, but it seems the first element is not removed. This is the code I write,

type LinkedList struct {
	Value int
	next  *LinkedList
}

func (ll *LinkedList) Remove(index int) error {
	pointer := ll
	var pointerPrev *LinkedList = nil
	current := 0

	for current &lt; index {
		pointerPrev = pointer
		pointer = pointer.next
		current++
	}

	if pointer == ll {
		ll = ll.next // this line is problematic
		pointer = nil
	} else {
		if pointer.next == nil {
			pointerPrev.next = nil
		} else {
			pointerPrev.next = pointer.next
			pointer = nil
		}
	}

	return nil
}

Any suggestion how I implement this way of removing without returning new LinkedList pointer?

答案1

得分: 3

一切都是通过复制传递的,所以只有在传递指向它的指针并修改指向的值时,你才能改变某个值。

所以,如果没有返回新的链表头(你必须在调用者那里进行赋值),你不能做你想做的事情。

另一种选择是传递头指针的地址(类型为**LinkedList),这样做很丑陋(必须始终传递头指针的地址)。你还可以添加一个单独的方法来删除第一个元素,类似于RemoveFirst(),这样你只需要传递给这个方法即可。这个RemoveFirst()方法也可以返回新的头指针,调用者必须进行赋值。这个RemoveFirst()方法也可以是一个“常规”函数,而不是一个方法。

另一种选择是创建一个包装器,它持有对头部的指针。然后在包装器上实现方法,而不是在节点类型上实现。包装器的一个方法可以改变持有头指针的字段。

参考链接:https://stackoverflow.com/questions/35421495/can-the-pointer-in-a-struct-pointer-method-be-reassigned-to-another-instance/35426997#35426997

英文:

Everything is passed as a copy, so you can only change something if a pointer to it is passed, and you modify the pointed value.

So you can't do what you want without having to return the new list head (which you have to assign at the caller).

An alternative could be to pass the address of the head pointer (type of **LinkedList), which is ugly (having to always pass the head pointer's address). You could also add a separate method for removing the first element, something like RemoveFirst(), so you only have to pass to this method only. This RemoveFirst() could return the new head too, which the caller has to assign. This RemoveFirst() could also be a "regular" function instead of a method.

Another alternative is to create a wrapper for the list, which holds a pointer to the head. And you implement methods on the wrapper, not on the node type. And a method of the wrapper could change the field holding the head pointer.

See related: https://stackoverflow.com/questions/35421495/can-the-pointer-in-a-struct-pointer-method-be-reassigned-to-another-instance/35426997#35426997

huangapple
  • 本文由 发表于 2022年3月12日 21:27:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/71450096.html
匿名

发表评论

匿名网友

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

确定