Created
March 10, 2022 05:55
-
-
Save rms1000watt/b47b66fc5746768a77ee5478ce78ba55 to your computer and use it in GitHub Desktop.
just a linked list in go
This file contains hidden or 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 | |
| import "fmt" | |
| type Node struct { | |
| Value string | |
| Next *Node | |
| } | |
| type LinkedList struct { | |
| Current *Node | |
| First *Node | |
| } | |
| func (ll *LinkedList) Insert(Value string) { | |
| if ll.Current == nil { | |
| first := &Node{ | |
| Value: Value, | |
| } | |
| ll.Current = first | |
| ll.First = first | |
| return | |
| } | |
| next := &Node{ | |
| Value: Value, | |
| } | |
| ll.Current.Next = next | |
| ll.Current = next | |
| } | |
| func (ll *LinkedList) Print() { | |
| curr := ll.First | |
| for { | |
| if curr == nil { | |
| return | |
| } | |
| fmt.Println(curr.Value) | |
| curr = curr.Next | |
| } | |
| } | |
| func main() { | |
| ll := LinkedList{} | |
| ll.Insert("hi1") | |
| ll.Insert("hi3") | |
| ll.Insert("hi2") | |
| ll.Print() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment