-
Notifications
You must be signed in to change notification settings - Fork 141
/
singlylinkedlist.go
145 lines (141 loc) · 2.82 KB
/
singlylinkedlist.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package singlylinkedlist
import "fmt"
// A list where each element points to the Next element in the list.
// 顺序是根据每个对象定的
/*
增,删,改,查
AddFirst or Prepend(Value interface{}), O(1)
Append(Value interface{}), O(n) , 后面加,
AddLast(Value interface{}), O(1), 后面加, 添加
RemoveFirst(index int) interface{}, O(1)
RemoveLast(index int) interface{}, O(1)
reverse()
Find(node *Node) (int, error), O(n)
Peek(), O(n)
Contains(values ...interface{}) bool, O(n)
*/
type Node struct {
Value interface{}
Next *Node
}
type LinkedList struct {
Head *Node
tail *Node // 用于addLast
size int
}
func NewNode(value interface{}) *Node {
return &Node{Value: value, Next: nil}
}
func (l *LinkedList) Size() int {
return l.size
}
func (l *LinkedList) Prepend(value interface{}) {
if l.Head == nil {
newNode := NewNode(value)
l.Head = newNode
l.tail = newNode
} else {
newNode := NewNode(value)
newNode.Next = l.Head
l.Head = newNode
}
l.size += 1
}
func (l *LinkedList) Append(value interface{}) {
newNode := NewNode(value)
if l.Head == nil {
l.Head = newNode
l.tail = newNode
} else {
tmp := l.Head
// 因为没有tail, 此时得loop n times查找next是否为最后一个
for tmp.Next != nil {
tmp = tmp.Next
}
tmp.Next = newNode
l.tail = newNode
}
l.size += 1
}
func (l *LinkedList) AddLast(value interface{}) {
if l.Head == nil {
l.Head = NewNode(value)
l.tail = l.Head
} else {
newNode := NewNode(value)
l.tail.Next = newNode
l.tail = newNode
}
l.size += 1
}
func (l *LinkedList) RemoveFirst() interface{} {
// empty
if l.Head == nil {
return nil
}
tmp := l.Head.Value
// single element
if l.Head.Next == nil {
l.Head = nil
l.tail = nil
} else { // multi element
l.Head = l.Head.Next
}
l.size -= 1
return tmp
}
func (l *LinkedList) RemoveLast() interface{} {
if l.Head == nil {
return nil
}
if l.Head.Next == nil {
return l.RemoveFirst()
}
// 找到倒数第二个
current := l.Head
for ; current.Next.Next != nil; current = current.Next {
}
// 取出倒数第一个的value
tmp := current.Next.Value
// 把倒数第二个变成最后一个
current.Next = nil
l.tail = current
return tmp
}
func (l *LinkedList) Reverse() {
var prev, next *Node
cur := l.Head
for cur != nil {
next = cur.Next
cur.Next = prev
prev = cur
cur = next
}
l.Head = prev
}
func (l *LinkedList) peekFirst() interface{} {
if l.Head == nil {
return nil
}
return l.Head.Value
}
func (l *LinkedList) peekLast() interface{} {
if l.tail == nil {
return nil
}
return l.tail.Value
}
func (l *LinkedList) peekLast2() interface{} {
if l.tail == nil {
return nil
}
tmp := l.Head
for ; tmp.Next != nil; tmp = tmp.Next {
}
return tmp.Value
}
func (l *LinkedList) Print() {
for current := l.Head; current != nil; current = current.Next {
fmt.Println(current)
}
}