forked from hacktoberfest17/programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request hacktoberfest17#77 from swimsystems/list
Add linked list implementation in Go
- Loading branch information
Showing
1 changed file
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 characters
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() | ||
} |