-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkedlist.go
63 lines (46 loc) · 1 KB
/
linkedlist.go
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main
import "fmt"
type node struct {
value int
next *node
}
type LinkedList struct {
head *node
}
func (ll *LinkedList) insert (value int) bool {
if ll.head == nil || ll.head.value > value {
new_node := new(node)
new_node.value = value
new_node.next = ll.head
ll.head = new_node
return true
}
curr_node := ll.head
for curr_node.next != nil && curr_node.next.value < value {
if curr_node.next.value == value {
return false
}
curr_node = curr_node.next
}
new_node := new(node)
new_node.value = value
new_node.next = curr_node.next
curr_node.next = new_node
return true
}
func (ll *LinkedList) show () {
curr_node := ll.head
for curr_node != nil {
fmt.Println(curr_node.value)
curr_node = curr_node.next
}
}
func main() {
ll := new(LinkedList)
ll.insert(1)
ll.insert(3)
ll.insert(5)
ll.insert(2)
ll.insert(4)
ll.show()
}