Created
July 13, 2011 13:35
-
-
Save karlseguin/1080295 to your computer and use it in GitHub Desktop.
Forward-only linked list in go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package hatch | |
type Element struct { | |
next *Element | |
Value interface{} | |
} | |
func (e *Element) Next() (*Element) { return e.next } | |
type LinkedList struct { | |
head, tail *Element | |
} | |
//default implementation only comes with a doubly linked-list | |
func (l *LinkedList) Add(value interface{}) { | |
e := &Element{nil, value} | |
if (l.head == nil){ | |
l.head = e | |
l.tail = e | |
} | |
l.tail.next = e | |
l.tail = e | |
} | |
func (l *LinkedList) AddList(list *LinkedList) { | |
for e := list.First(); e != nil ; e = e.Next() { | |
l.Add(e.Value) | |
} | |
} | |
func (l *LinkedList) First() (e *Element) { return l.head } | |
func (l *LinkedList) Last() (e *Element) { return l.tail } | |
func (l *LinkedList) IsEmpty() (bool) { return l.head == nil } |
Go handles indirection. So there's no ->
Seems like the pointers are purely for pass by ref vs pass by value semantics....almost just for that...
I miss pointers in C# ;[ ^^ (ye i can use them but am ... :))
On Wed, Jul 13, 2011 at 3:49 PM, karlseguin ***@***.*** wrote:
Go handles indirection. So there's no ->
Seems like the pointers are purely for pass by ref vs pass by value semantics....almost just for that...
##
Reply to this email directly or view it on GitHub:
https://gist.github.com/1080295
##
All The Best
Michal Franc
www.mfranc.com
Follow me on Twitter!
http://twitter.com/francmichal
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yey pointers. Me like them ! :]