-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathll.go
More file actions
42 lines (37 loc) · 705 Bytes
/
ll.go
File metadata and controls
42 lines (37 loc) · 705 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package main
import "fmt"
type node struct {
data int
next *node
}
type linkedlist struct {
head *node
}
func (linkedlist *linkedlist) append(newnode *node) {
if linkedlist.head == nil {
linkedlist.head = newnode
newnode.next = nil
} else {
currentnode := linkedlist.head
for currentnode.next != nil {
currentnode = currentnode.next
}
currentnode.next = newnode
}
}
func (li *linkedlist) printlist() {
nn := li.head
for nn != nil {
fmt.Printf("value %d at position %v", nn.data, nn.next)
fmt.Println()
nn = nn.next
}
}
func main() {
l := &linkedlist{}
l.append(&node{1, nil})
l.append(&node{2, nil})
l.append(&node{3, nil})
l.append(&node{4, nil})
l.printlist()
}