snippets:golang:containers:doublelist
**This is an old revision of the document!**
double linked list
- snippet.go
package main
import "fmt"
type MyListElement struct {
Name string
Age int
next *MyListElement
prev *MyListElement
}
func (i *MyListElement) Next() *MyListElement {
return i.next
}
type MyList struct {
Head *MyListElement
Tail *MyListElement
}
func (list *MyList) Append(item *MyListElement) {
if list.Head == nil {
list.Head = item
list.Tail = item
item.next = nil
item.prev = nil
return
}
list.Tail.next = item
item.prev = list.Tail
item.next = nil
list.Tail = item
}
func (list *MyList) Count() int {
count := 0
for i := list.Head; i != nil; i = i.next {
count += 1
}
return count
}
func main() {
list := MyList{}
{
a := &MyListElement{Name: "Max", Age: 23}
list.Append(a)
}
{
b := &MyListElement{Name: "Hans", Age: 42}
list.Append(b)
}
fmt.Println(list.Count())
}
snippets/golang/containers/doublelist.1521544582.txt.gz · Last modified: by allspark