Skip to content

Instantly share code, notes, and snippets.

@enahs
Last active August 29, 2015 14:16
Show Gist options
  • Save enahs/1d6ec40d9251a32b32d9 to your computer and use it in GitHub Desktop.
Save enahs/1d6ec40d9251a32b32d9 to your computer and use it in GitHub Desktop.
Go Linked List Example
package main
import "fmt"
type Node struct {
Data interface{}
Next *Node
}
type list struct {
Len int
Head, Tail *Node
}
func (L *list) Push(n *Node) {
if L.Len == 0 {
L.Head = n
} else {
L.Tail.Next = n
}
L.Tail = n
L.Len++
}
func main() {
L := &list{}
n1, n2, n3 := &Node{1, nil}, &Node{2, nil}, &Node{3, nil}
L.Push(n1)
L.Push(n2)
L.Push(n3)
for curr := L.Head; curr != nil; curr = curr.Next {
fmt.Printf("%#v", curr.Data)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment