16. Go - Linked Lists
package main
import "fmt"
type node struct { // nodes
data int
next *node
}
type linkedList struct { // linked list
head *node
length int
}
func (l *linkedList) prepend(n *node) { // method
second := l.head
l.head = n
l.head.next = second
l.length++
}
func main() {
mylist := linkedList{}
node1 := &node{data: 50}
node2 := &node{data: 60}
node3 := &node{data: 70}
mylist.prepend(node1)
mylist.prepend(node2)
mylist.prepend(node3)
fmt.Println(mylist)
fmt.Println(mylist.head.next.next.data)
}
Comments
Post a Comment