-
Notifications
You must be signed in to change notification settings - Fork 0
/
03LinkDoubleList.go
194 lines (163 loc) · 3.02 KB
/
03LinkDoubleList.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
package main
import (
"fmt"
)
//双向链表结点
type LinkDoubleNode struct {
Data interface{}
Prev *LinkDoubleNode
Next *LinkDoubleNode
}
//创建双向链表
func (node *LinkDoubleNode) Create(Data ...interface{}) {
if node == nil || Data == nil {
return
}
if len(Data) == 0 {
return
}
//保存头结点
head := node
//循环Data
for _, v := range Data {
newNode := new(LinkDoubleNode)
newNode.Data = v
newNode.Prev = node
newNode.Next = nil
//当前结点的下一个结点赋值为新节点
node.Next = newNode
//将当前结点赋值为下一个结点
node = node.Next
}
//还原头结点
node = head
}
//打印双向链表 --->正向递归
func (node *LinkDoubleNode) Print1() {
if node == nil { //容错 递归出口
return
}
if node.Data != nil {
fmt.Print(node.Data, " ")
}
node.Next.Print1()
}
//打印双向链表 --->逆向循环打印
func (node *LinkDoubleNode) Print2() {
if node == nil {
return
}
//找到尾节点
for node.Next != nil {
node = node.Next
}
//循环 prev 倒叙 打印
for node.Prev != nil {
if node.Data != nil {
fmt.Print(node.Data, " ")
}
//node前移
node = node.Prev
}
}
//获取长度
func (node *LinkDoubleNode) Length() int {
if node == nil {
return -1
}
//定义计数器
i := 0
for node.Next != nil {
i++
node = node.Next
}
return i
}
//插入==》按位置插入
func (node *LinkDoubleNode) InsertByIndex(Data interface{}, index int) {
if node == nil || Data == nil {
return
}
if index < 0 || index > node.Length() {
return
}
//定义index前结点
perNode := node
//循环找到index位置的node
for i := 0; i < index; i++ {
perNode = node
node = node.Next
}
//定义新结点
newNode := new(LinkDoubleNode)
newNode.Data = Data
//正序添加
perNode.Next = newNode
newNode.Next = node
//反序添加
node.Prev = newNode
newNode.Prev = perNode
}
//删除结点==>按index删除
func (node *LinkDoubleNode) DeleteByIndex(index int) {
if node == nil {
return
}
if index < 0 || index > node.Length() {
return
}
//定义变量,存储链表长度
l := node.Length()
//定义前驱结点
preNode := node
//寻找位置结点
for i := 0; i < index; i++ {
preNode = node
node = node.Next
}
//如果index是最后一个结点
if index == l {
preNode.Next = nil
node.Data = nil
node.Prev = nil
node.Next = nil
node = nil
} else {
//赋值,删除结点
preNode.Next = node.Next
node.Next.Prev = preNode
//置空,结点
node.Data = nil
node.Prev = nil
node.Next = nil
node = nil
}
}
//销毁链表
func (node *LinkDoubleNode) Destroy() {
if node == nil {
return
}
node.Next.Destroy()
node.Next = nil
node.Prev = nil
node.Data = nil
node = nil
}
func main() {
list := new(LinkDoubleNode)
list.Create(1, 2, 3, 4, 5)
//list.Print1()
//fmt.Println()
//list.Print2()
//len := list.Length()
//fmt.Println(len)
//list.InsertByIndex(777, 6)
list.DeleteByIndex(5)
list.Print1()
fmt.Println()
list.Print2()
list.Destroy()
list.Print1()
list.Print2()
}