-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.go
75 lines (64 loc) · 1.11 KB
/
list.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
75
package util
import (
"fmt"
)
type item struct {
value interface{}
next *item
}
type List struct {
head *item
size int
next *item
}
//Len returns the size of stack
func (l *List) Len() int {
return l.size
}
func (l *List) First() (value interface{}) {
if l.size > 0 {
value = l.head.value
l.next = l.head.next
}
return
}
func (l *List) Last() (value interface{}) {
if l.size == 0 {
return
}
var i *item
for i = l.head; i.next != nil; i = i.next {}
value = i.value
return
}
func (l *List) Next() (value interface{}) {
if l.next != nil {
value = l.next.value
l.next = l.next.next
}
return
}
func (l *List) Add(value interface{}) {
if l.head == nil {
l.head = &item {
value: value,
next: nil,
}
l.size = 1
return
}
var temp *item
for temp = l.head; temp.next != nil; temp = temp.next {}
temp.next = &item {
value: value,
next: nil,
}
l.size += 1
}
func (l *List) String() (string) {
result := ""
for temp := l.First(); temp != nil; temp = l.Next() {
result += "-> " + fmt.Sprintf("%v", temp)
}
return result
}