指针问题与链表实现

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

Pointer trouble with linkedlist implementation

问题

尝试使用简单的addToLast函数(将新节点添加到链表的末尾)来实现LinkedList,而不是使用内置的列表。
以下是代码(已删除用于调试的打印语句):

package main

import "fmt"

var first *Link
var last Link

func main() {
    AddToLast(10)
    AddToLast(20)
}

func AddToLast(d int) {
    if first == nil {
        last = Link{d, new(Link)}
        first = &last
    } else {
        last.next = &Link{d, new(Link)}
        last = *last.next
    }
}

type Link struct {
    data int
    next *Link
}

我对上述代码的理解是:

在AddToLast函数内部:
在检查first是否为nil(即是否没有任何元素)之后,创建了一个名为last的Link,其数据为10,下一个为空的Link。现在,使用内存地址将first赋值为相同的值(作为引用——我不确定我是否理解错误)。

现在,当我们尝试插入20(下一个新元素)时,将执行AddToLast函数中的else部分。将last.next赋值为一个值为20且下一个为nil的Link。现在,将last移动到last.next,以确保last始终指向最后一个节点。

然而,由于我将last移动到last.next,last的内存地址发生了变化,这是显而易见的,这也导致first指向新的last,即值为20。

为了避免这种情况,我尝试将first声明为Link而不是*Link。然而,这样做并不会使first.next指向新的节点,即20。我对我哪里想错了感到困惑。

英文:

Trying to implement LinkedList with simple addToLast function (which adds the new node to the end of the linkedlist) instead of using the inbuilt list)
Below is the code (removed print statements which I used for debugging):

package main

import "fmt"

var first *Link
var last Link

func main() {
    AddToLast(10)
    AddToLast(20)
}

func AddToLast(d int) {
    if first == nil {
	    last = Link{d, new(Link)}
	    first = &last
	} else {
    	last.next = &Link{d, new(Link)}
	    last = *last.next
	}
}

type Link struct {
	data int
	next *Link
}

My understanding of the above code:

Inside AddToLast functions -
After checking if 'first' is nil i.e. it doesn't have any element, 'last' is created
with 10 as the data and new empty Link as the next. Now 'first' is assigned the same value
as 'last' but using memory address(as reference - I'm not sure if my understanding is incorrect here)

Now when we try to insert 20 (the next new element) the else part in the 'AddToLast' is executed.
'last.next' is assigned a link with value as 20 and it's next as nil. Now 'last' is moved to 'last.next',
to make sure 'last' always points to the last node.

However since I moved 'last' to 'last.next', 'last'('s) memory address changes which is obvious and this also
causes first to point to the new last i.e. with value as 20.

In order to avoid this, I tried declaring 'first' as Link instead of *Link.
However, doing this doesn't make first.next point to new node i.e. 20. I'm confused as to where I'm not thinking correct.

答案1

得分: 2

不要改变last中元素的值,因为该元素已经创建。创建一个新的last,并将前一个next指针指向它。这是修改后的版本:http://play.golang.org/p/-X5RayC0gU

var first *Link
var last *Link

func AddToLast(d int) {
    next := &Link{d, nil}
    if first == nil {
        first = next
    } else {
        last.next = next
    }
    last = next
}

type Link struct {
    data int
    next *Link
}
英文:

Don't mutate the element value in last, since that element is already created. Make a new last, and set the previous next pointer to point to it. Here is a modified version: http://play.golang.org/p/-X5RayC0gU

var first *Link
var last *Link

func AddToLast(d int) {
	next := &Link{d, nil}
	if first == nil {
		first = next
	} else {
		last.next = next
	}
	last = next
}

type Link struct {
	data int
	next *Link
}

huangapple
  • 本文由 发表于 2015年5月9日 00:22:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/30128531.html
匿名

发表评论

匿名网友

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

确定