-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.go
217 lines (197 loc) · 4.15 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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package linkedlist
import (
"errors"
"fmt"
"github.com/hopeio/cherry/utils/log"
)
// 链表结点
type Node[T any] struct {
data T
next, prev *Node[T]
}
// 链表
type LinkedList[T comparable] struct {
head, tail *Node[T]
size int
}
// 新建空链表,即创建Node指针head,用来指向链表第一个结点,初始为空
func New[T comparable]() LinkedList[T] {
l := LinkedList[T]{}
return l
}
// 是否为空链表
func (l *LinkedList[T]) IsEmpty() bool {
return l.size == 0
}
// 获取链表长度
func (l *LinkedList[T]) GetLength() int {
return l.size
}
// 是否含有指定结点
func (l *LinkedList[T]) Exist(node *Node[T]) bool {
var p = l.head
for p != nil {
if p == node {
return true
} else {
p = p.next
}
}
return false
}
// 获取含有指定数据的第一个结点
func (l *LinkedList[T]) GetNode(e T) *Node[T] {
var p = l.head
for p != nil {
//找到该数据所在结点
if e == p.data {
return p
} else {
p = p.next
}
}
return nil
}
// 在链表尾部添加数据
func (l *LinkedList[T]) Append(e T) {
//为数据创建新结点
newNode := Node[T]{}
newNode.data = e
newNode.next = nil
if l.size == 0 {
l.head = &newNode
l.tail = &newNode
} else {
l.tail.next = &newNode
l.tail = &newNode
}
l.size++
}
// 在链表头部插入数据
func (l *LinkedList[T]) InsertHead(e T) {
newNode := Node[T]{}
newNode.data = e
newNode.next = l.head
l.head = &newNode
if l.size == 0 {
l.tail = &newNode
}
l.size++
}
// 在指定结点后面插入数据
func (l *LinkedList[T]) InsertAfterNode(pre *Node[T], e T) error {
//如果链表中存在该结点,才进行插入
if l.Exist(pre) {
newNode := Node[T]{}
newNode.data = e
if pre.next == nil {
l.Append(e)
} else {
newNode.next = pre.next
pre.next = &newNode
}
l.size++
return nil
}
return errors.New("链表中不存在该结点")
}
// 在第一次出现指定数据的结点后插入数据,若链表中无该数据,返回false
func (l *LinkedList[T]) InsertAfterData(preData T, e T) error {
var p = l.head
for p != nil {
//找到该数据所在结点
if p.data == preData {
l.InsertAfterNode(p, e)
return nil
} else {
p = p.next
}
}
//没有找到该数据
return errors.New("链表中没有该数据,插入失败")
}
// 在指定下标处插入数据
func (l *LinkedList[T]) Insert(position int, e T) error {
if position < 0 {
return errors.New("下标不能为负数")
} else if position == 0 {
//在头部插入
l.InsertHead(e)
return nil
} else if position == l.size {
//在尾部插入
l.Append(e)
return nil
} else if position > l.size {
return errors.New("指定下标超出链表长度")
} else {
//在中间插入
var index int
var p = l.head
//逐个移动指针
//position是插入后新结点的下标,position-1时需要定位到的其前一个结点的下标
for index = 0; index < position-1; index++ {
p = p.next
}
//找到
l.InsertAfterNode(p, e)
return nil
}
}
// 删除指定结点
func (l *LinkedList[T]) DeleteNode(node *Node[T]) {
//存在该结点
if l.Exist(node) {
//如果是头部结点
if node == l.head {
l.head = l.head.next
//如果是尾部结点
} else if node == l.tail {
//寻找指向其前一个结点的指针
var p = l.head
for p.next != l.tail {
p = p.next
}
p.next = nil
l.tail = p
//中间结点
} else {
var p = l.head
for p.next != node {
p = p.next
}
p.next = node.next
}
l.size--
}
}
// 删除第一个含指定数据的结点
func (l *LinkedList[T]) Delete(e T) {
p := l.GetNode(e)
if p == nil {
return
}
l.DeleteNode(p)
}
// 遍历链表
func (l *LinkedList[T]) traverse(f func(T)) {
var p = l.head
if l.IsEmpty() {
return
}
for p != nil {
if f != nil {
f(p.data)
}
p = p.next
}
}
// 打印链表信息
func (l *LinkedList[T]) PrintInfo() {
fmt.Println("###############################################")
fmt.Println("链表长度为:", l.GetLength())
fmt.Println("链表是否为空:", l.IsEmpty())
fmt.Print("遍历链表:")
l.traverse(func(data T) { log.Info(data, " ") })
fmt.Println("###############################################")
}