Last active
March 15, 2020 20:52
-
-
Save halkyon/89e78420e558e966e1f03b258d889fc7 to your computer and use it in GitHub Desktop.
Revisions
-
halkyon revised this gist
Mar 15, 2020 . No changes.There are no files selected for viewing
-
halkyon created this gist
Mar 15, 2020 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,53 @@ package main import ( "fmt" ) type list struct { head *item tail *item } type item struct { name string prev *item next *item } func main() { list := &list{} list.add(&item{name: "foo"}) list.add(&item{name: "bar"}) list.add(&item{name: "baz"}) list.show() } func (list *list) add(item *item) { if list.head == nil { list.head = item } else { currentItem := list.tail currentItem.next = item item.prev = list.tail item.next = list.head list.head.prev = item } list.tail = item } func (list *list) show() { currentItem := list.head if currentItem == nil { return } fmt.Printf("%+v\n", currentItem) for currentItem.next != nil { currentItem = currentItem.next if currentItem == list.head { break } fmt.Printf("%+v\n", currentItem) } }