Skip to content

Commit

Permalink
Merge pull request hacktoberfest17#77 from swimsystems/list
Browse files Browse the repository at this point in the history
Add linked list implementation in Go
  • Loading branch information
t2013anurag authored Oct 15, 2017
2 parents aec254d + 4f24c10 commit 1836625
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions linked_list/linked_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package main

import "fmt"

type Node struct {
prev *Node
next *Node
key interface{}
}

type LinkedList struct {
head *Node
tail *Node
}

func (L *LinkedList) 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 *LinkedList) Show() {
list := l.head
for list != nil {
fmt.Printf("%+v", list.key)
if list.next != nil {
fmt.Printf(" --> ")
}
list = list.next
}
fmt.Println()
}

func main() {
l := LinkedList{}
l.Insert(1)
l.Insert(2)
l.Insert(3)
l.Insert(4)
l.Insert(5)
l.Show()
}

0 comments on commit 1836625

Please sign in to comment.