How to iterate lists in Golang?

xuanbao · · 417 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<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 &lt; 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 &amp;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&#39;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&#39;t change the function header to a pointer :/</p> </blockquote> <p>Because it&#39;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>&amp;firstElementOfTheList</code>. But that&#39;s probably not what you want.</p></pre>OrangeIsCute: <pre><p>It&#39;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&#39;s the declaration of &#34;pntr&#34;. </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 = &amp;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 &amp; is <code>address-of</code> * is <code>pointer-to</code>. so *bar is &#39;referencing bar via address bar (ptr bar)&#39;, 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

417 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传