-
Notifications
You must be signed in to change notification settings - Fork 0
/
doublylinkedlist.go
221 lines (207 loc) · 4.01 KB
/
doublylinkedlist.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
218
219
220
221
package algorithm
import (
"fmt"
)
// 双向链表
type DoublyLinkedList struct {
head *Node
tail *Node
length uint
}
type Node struct {
data interface{}
previous *Node
next *Node
}
func NewDoublyLinkedList() *DoublyLinkedList {
return &DoublyLinkedList{
length: 0,
}
}
// 新增头节点
func (list *DoublyLinkedList) InsertToHead(v interface{}) error {
newNode := &Node{
data: v,
}
// 头节点
head := list.head
if head == nil {
// 空链表
list.head = newNode
list.tail = newNode
} else {
// 非空链表
newNode.next = head
head.previous = newNode
list.head = newNode
}
list.length++
return nil
}
// 删除头节点
func (list *DoublyLinkedList) RemoveHead() error {
if list.head == nil {
return ErrRemoveFromEmptyList
}
// 新头节点
next := list.head.next
next.previous = nil
list.head = next
list.length--
return nil
}
// 新增尾节点
func (list *DoublyLinkedList) InsertToTail(v interface{}) error {
if list.head == nil {
return list.InsertToHead(v)
}
tail := list.tail
newNode := &Node{
data: v,
previous: tail,
}
tail.next = newNode
list.tail = newNode
list.length++
return nil
}
// 删除尾节点
func (list *DoublyLinkedList) RemoveTail() error {
if list.head == nil {
return ErrRemoveFromEmptyList
}
tail := list.tail
// 新的尾节点
current := tail.previous
current.next = nil
list.tail = current
list.length--
return nil
}
// 在某个节点后面插入节点
func (list *DoublyLinkedList) InsertAfter(node *Node, v interface{}) error {
if node == nil {
return ErrEmptyNode
}
newNode := &Node{
data: v,
}
tail := list.tail
if node == tail {
// 在尾节点后插入
newNode.previous = tail
tail.next = newNode
list.tail = newNode
} else {
// 不是尾节点后插入
newNode.previous = node
newNode.next = node.next
node.next = newNode
}
list.length++
return nil
}
// 在某个节点前面插入节点
func (list *DoublyLinkedList) InsertBefore(node *Node, v interface{}) error {
if node == nil {
return ErrEmptyNode
}
// 空链表或者在头节点前插入
if list.head == nil || node == list.head {
return list.InsertToHead(v)
}
previous := node.previous
newNode := &Node{
data: v,
next: node,
previous: previous,
}
// 前一节点的next
previous.next = newNode
// 当前节点的previous
node.previous = newNode
list.length++
return nil
}
// 删除节点
func (list *DoublyLinkedList) RemoveNode(node *Node) error {
if node == nil {
return ErrEmptyNode
}
if list.head == nil {
return ErrRemoveFromEmptyList
}
// 前一节点的next
current := node.previous
current.next = node.next
// 后一节点的previous
next := node.next
next.previous = current
// avoid memory leaks
node.next = nil
node.previous = nil
node.data = nil
list.length--
return nil
}
// 打印链表
func (list *DoublyLinkedList) Print() string {
if list.head == nil {
return ""
}
current := list.head
var message string
for current != nil {
message += fmt.Sprintf("%+v", current.data)
current = current.next
if current != nil {
message += "->"
}
}
return message
}
// 打印链表结构
func (list *DoublyLinkedList) PrintAddress() string {
if list.head == nil {
return ""
}
current := list.head
var address string
for current != nil {
address += fmt.Sprintf("%+v", current)
current = current.next
if current != nil {
address += "->"
}
}
return address
}
// 反转链表
func (list *DoublyLinkedList) Reverse() {
if list.head == nil {
return
}
if list.head.next == nil {
return
}
fmt.Println("--- before reverse ---")
fmt.Println(list.PrintAddress())
var previous *Node
var current *Node = list.head
for current != nil {
// 当前节点下一个节点
temp := current.next
current.next = previous
current.previous = temp
if previous == nil {
list.tail = current
}
// 更新上一个节点
previous = current
// 更新当前节点
current = temp
}
list.head = previous
fmt.Println("--- after reverse ---")
fmt.Println(list.PrintAddress())
}