// for testing GC; taken from internet package main import "fmt" type Node struct { prev *Node next *Node key interface{} } type List struct { head *Node tail *Node t1 *Node t2 *Node } var glist = List{} func (L *List) Insert(key interface{}) { list := &Node{ next: L.head, key: key, } if L.head != nil { L.head.prev = list } L.head = list l := L.head for l.next != nil { l = l.next } L.tail = l } func (l *List) Display() { list := l.head for list != nil { fmt.Println("%+v ->", list.key) list = list.next } //fmt.Println() } func Display(list *Node) { for list != nil { fmt.Println("%v ->", list.key) list = list.next } //fmt.Println() } func ShowBackwards(list *Node) { for list != nil { fmt.Println("%v <-", list.key) list = list.prev } //fmt.Println() } func (l *List) Reverse() { curr := l.head var prev *Node l.tail = l.head for curr != nil { next := curr.next curr.next = prev prev = curr curr = next } l.head = prev Display(l.head) } func main() { link := List{} link.Insert(1) link.Insert(3) link.Insert(5) link.Insert(7) link.Insert(9) fmt.Println("\n==============================\n") fmt.Println("Head: %v\n", link.head.key) fmt.Println("Tail: %v\n", link.tail.key) //fmt.Println("ttt: %v\n", link.ttt.key) link.Display() fmt.Println("\n==============================\n") fmt.Println("head: %v\n", link.head.key) fmt.Println("tail: %v\n", link.tail.key) link.Reverse() fmt.Println("\n==============================\n") // for testing GC link2 := List{} link2.Insert(10) link2.Insert(30) link2.Insert(50) link2.Insert(70) link2.Insert(90) link2.head.prev = link2.tail link2.tail.next = link2.head glist.Insert(2) glist.Insert(4) glist.Insert(6) glist.Insert(8) glist.Insert(10) glist.t1 = glist.head glist.t2 = glist.head }