Skip to content

Instantly share code, notes, and snippets.

@karlseguin
Created July 13, 2011 13:35
Show Gist options
  • Save karlseguin/1080295 to your computer and use it in GitHub Desktop.
Save karlseguin/1080295 to your computer and use it in GitHub Desktop.
Forward-only linked list in go
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 }
@michal-franc
Copy link

Yey pointers. Me like them ! :]

@karlseguin
Copy link
Author

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...

@michal-franc
Copy link

michal-franc commented Jul 13, 2011 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment