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)
This is the function that I have, which produces an error that undefined: pntr
func (s StackLinked) copy() Stacker {
ss := makeStackLinked()
*pntr = s.lists.Front().Value.(int)
for i := 0; i < s.lists.Len() ; i++ {
ss.push(pntr)
pntr = s.lists.Next()
} return ss }
any help would be appreciated !!!
评论:
TheMerovius:
TheMerovius:You might ask /u/JasonLiuENGR, it seems they are in the same course as you ;)
OrangeIsCute:Oh and to answer your question (see the last paragraph here for what I think about cheating yourself out of an education): The same way, you iterate in any other language.
func (s *StackLinked) copy() Stacker { if s == nil { return s } return &StackLinked{ value: s.value, next: s.next.copy().(*StackLinked) } }
Or, without recursion
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() }
Which of these is best is left as an exercise to the person doing the assignment ;)
[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.
TheMerovius:thanks for all the help, but sadly I can't change the function header to a pointer :/
I somehow used the builtin functions of list and made it work
i was just curious if it was possible to create a pointer to the first element of the linked list
OrangeIsCute:thanks for all the help, but sadly I can't change the function header to a pointer :/
Because it's an assignment, I assume?
i was just curious if it was possible to create a pointer to the first element of the linked list
You stumbled upon the point of the exercise :)
Yes, it is possible to do so:
&firstElementOfTheList
. But that's probably not what you want.
slewiskelly:It's a function as part of a longer project, but finally got all my tests to pass
whaevsimdrunk:The documentation is plentiful: https://golang.org/doc/effective_go.html#for.
Your problem is not with ranging over a slice, it's the declaration of "pntr".
OrangeIsCute:a few notes, may help but im a beginner.
1) you still need
:=
the first time you assign a variable...or.. use
var pntr *int
as the first line in your functhis would help you! the compiler will print a clearer error! so try that.
2)im not sure, but go is new and might not have reverse declarations or forward declarations... whatever theyre called
*ptr = somevalue
..... https://play.golang.org/p/MPv-wmRhxFJbut it does work when i do
pntr = &somevalue; ss=append(ss, *pntr)
not sure i just managed to confuse myself.
if it helps i like to think of it like this & is
address-of
* ispointer-to
. 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)3) you probably just want
ss.push(*myvar)
thanks, i fixed it :)
