Skip to content

Instantly share code, notes, and snippets.

@blinkinglight
Forked from maksadbek/golang-linked-list.go
Created July 11, 2018 04:31
Show Gist options
  • Save blinkinglight/d0147a2ac7fc969ae85e084ce701555d to your computer and use it in GitHub Desktop.
Save blinkinglight/d0147a2ac7fc969ae85e084ce701555d to your computer and use it in GitHub Desktop.
golang linked list implementation
package main
import "fmt"
type Node struct {
prev *Node
next *Node
key interface{}
}
type List struct {
head *Node
tail *Node
}
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) Show() {
list := l.head
for list != nil {
fmt.Printf("%+v ->", list.key)
list = list.next
}
fmt.Println()
}
func Show(list *Node) {
for list != nil {
fmt.Printf("%v ->", list.key)
list = list.next
}
fmt.Println()
}
func ShowBackwards(list *Node) {
for list != nil {
fmt.Printf("%v <-", list.key)
list = list.prev
}
fmt.Println()
}
// 1 -> 2 -> 3 -> 4 -> nil
// nil <- 1 <- 2 <- 3 <- 4
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
Show(l.head)
}
func main() {
l := List{}
l.Insert(1)
l.Insert(2)
l.Insert(3)
l.Insert(4)
l.Insert(5)
l.Insert(6)
fmt.Printf("head: %v\n", l.head.key)
fmt.Printf("tail: %v\n", l.tail.key)
l.Show()
l.Reverse()
fmt.Printf("head: %v\n", l.head.key)
fmt.Printf("tail: %v\n", l.tail.key)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment