<p>I am trying to implement a stack using a list stucture, and iterate through a list to copy the list,
However I am not sure how to make a pointer to point to the first element on the list (and it seems there is not a very good documentation on lists in Golang) </p>
<p>This is the function that I have, which produces an error that
undefined: pntr</p>
<p>func (s StackLinked) copy() Stacker {</p>
<pre><code>ss := makeStackLinked()
*pntr = s.lists.Front().Value.(int)
for i := 0; i < s.lists.Len() ; i++ {
ss.push(pntr)
pntr = s.lists.Next()
</code></pre>
<p>}
return ss
}</p>
<p>any help would be appreciated !!! </p>
<hr/>**评论:**<br/><br/>TheMerovius: <pre><p>You might ask <a href="/u/JasonLiuENGR" rel="nofollow">/u/JasonLiuENGR</a>, it seems they are <a href="https://www.reddit.com/r/golang/comments/7qwuu5/golang_comparing_two_structs/" rel="nofollow">in the same course</a> as you ;)</p></pre>TheMerovius: <pre><p>Oh and to answer your question (see the last paragraph <a href="https://www.reddit.com/r/golang/comments/7qwuu5/golang_comparing_two_structs/dst23n9/" rel="nofollow">here</a> for what I think about cheating yourself out of an education): The same way, you iterate in any other language.</p>
<pre><code>func (s *StackLinked) copy() Stacker {
if s == nil {
return s
}
return &StackLinked{ value: s.value, next: s.next.copy().(*StackLinked) }
}
</code></pre>
<p>Or, without recursion</p>
<pre><code>func (s *StackLinked) flip() *StackLinked {
tmp := makeStackLinked()
for !s.isEmpty() {
tmp.push(s.pop())
}
return tmp
}
func (s *StackLinked) copy() Stacker {
return s.flip().flip()
}
</code></pre>
<p>Which of these is best is left as an exercise to the person doing the assignment ;)</p>
<p>[edit] I just realized that all of these are bad. There is an O(1) implementation, which is probably the point of the assignment. But it depends too much on the other details of your code.</p></pre>OrangeIsCute: <pre><p>thanks for all the help, but sadly I can't change the function header to a pointer :/ </p>
<p>I somehow used the builtin functions of list and made it work </p>
<p>i was just curious if it was possible to create a pointer to the first element of the linked list </p></pre>TheMerovius: <pre><blockquote>
<p>thanks for all the help, but sadly I can't change the function header to a pointer :/</p>
</blockquote>
<p>Because it's an assignment, I assume?</p>
<blockquote>
<p>i was just curious if it was possible to create a pointer to the first element of the linked list</p>
</blockquote>
<p>You stumbled upon the point of the exercise :)</p>
<p>Yes, it is possible to do so: <code>&firstElementOfTheList</code>. But that's probably not what you want.</p></pre>OrangeIsCute: <pre><p>It's a function as part of a longer project, but finally got all my tests to pass </p></pre>slewiskelly: <pre><p>The documentation is plentiful: <a href="https://golang.org/doc/effective_go.html#for" rel="nofollow">https://golang.org/doc/effective_go.html#for</a>. </p>
<p>Your problem is not with ranging over a slice, it's the declaration of "pntr". </p></pre>whaevsimdrunk: <pre><p>a few notes, may help but im a beginner.</p>
<p>1) you still need <code>:=</code> the first time you assign a variable... </p>
<p>or.. use<code>var pntr *int</code> as the first line in your func</p>
<p>this would help you! the compiler will print a clearer error! so try that.</p>
<p>2)im not sure, but go is new and might not have reverse declarations or forward declarations... whatever theyre called <code>*ptr = somevalue</code>..... <a href="https://play.golang.org/p/MPv-wmRhxFJ" rel="nofollow">https://play.golang.org/p/MPv-wmRhxFJ</a></p>
<p>but it does work when i do <code>pntr = &somevalue; ss=append(ss, *pntr)</code> </p>
<p>not sure i just managed to confuse myself.</p>
<p>if it helps i like to think of it like this & is <code>address-of</code> * is <code>pointer-to</code>. so *bar is 'referencing bar via address bar (ptr bar)', basically everything is either a byte slice or a pointer to a byte slice, (a pointer being a 8 bytes that contain a memory address to another byte slice)</p>
<p>3) you probably just want <code>ss.push(*myvar)</code></p></pre>OrangeIsCute: <pre><p>thanks, i fixed it :) </p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传