-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathllist.go
74 lines (60 loc) · 887 Bytes
/
llist.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
64
65
66
67
68
69
70
71
72
73
74
package llist
import (
"strings"
"fmt"
"math/rand"
)
// START OMIT
type item struct {
x int
next *item
}
// END OMIT
type List struct {
items *item
}
func (s *List) Insert(x int) {
var p **item
for p = &(s.items); *p != nil; p = &((*p).next) {
if (*p).x >= x {
break
}
}
it := &item{x, *p}
*p = it
}
func (s *List)String()string {
w := new(strings.Builder)
for p := s.items; p != nil; p = p.next {
fmt.Fprintf(w, "%d ", p.x)
}
return w.String()
}
// START2 OMIT
func (s *List)Sum()int {
var t int
for p := s.items; p != nil; p = p.next {
t += p.x
}
return t
}
// END2 OMIT
func (s *List)Fill(n int) {
for i:= 0; i < n; i++ {
s.Insert(rand.Int())
}
}
// STARTS OMIT
func sumV(v []int) int {
t := 0
for _, x := range v {
t += x
}
return t
}
// ENDS OMIT
func fillV(v []int) {
for i := 0; i < cap(v); i++ {
v[i] = rand.Int()
}
}