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

Popular posts from this blog

2. FreeCodeCamp - Dynamic Programming - Learn to Solve Algorithmic Problems & Coding Challenges

20. Data Analytics - Analyze Data to Answer Questions - Week 1

3. Algorithms - Selection Sort