Created
August 14, 2021 03:56
-
-
Save hellojukay/1cdc675939d8fcf920dabeb9c4e11637 to your computer and use it in GitHub Desktop.
Use golang to implement linked list in functional style
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
package main | |
type Node struct { | |
Val int | |
Next interface{} | |
} | |
func create() interface{} { | |
return nil | |
} | |
func insert(list interface{}, n int) Node { | |
return Node{ | |
Val: n, | |
Next: list, | |
} | |
} | |
func loop(list interface{}) { | |
if list == nil { | |
return | |
} | |
var node = list.(Node) | |
println(node.Val) | |
loop((node.Next)) | |
} | |
func main() { | |
var list = create() | |
for i := 0; i < 10; i++ { | |
list = insert(list, i+2) | |
} | |
loop(list) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment