-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoubly_linked_list.go
162 lines (141 loc) · 2.98 KB
/
doubly_linked_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
package dll
import (
"fmt"
"strings"
)
// DoublyLinkedNode 双向链表的节点
type DoublyLinkedNode struct {
data interface{}
prev *DoublyLinkedNode
next *DoublyLinkedNode
}
// DoublyLinkedList 双向链表的实现
type DoublyLinkedList struct {
head *DoublyLinkedNode // 双向链表的头部节点
tail *DoublyLinkedNode // 双向链表的尾部节点
size int // 链表的长度
}
// NewDll 返回一个空的双向链表
func NewDll() *DoublyLinkedList {
return &DoublyLinkedList{}
}
func (l *DoublyLinkedList) Add(data interface{}) {
l.Insert(0, data)
}
func (l *DoublyLinkedList) Append(data interface{}) {
l.Insert(l.size, data)
}
func (l *DoublyLinkedList) Insert(pos int, data interface{}) {
if pos < 0 || pos > l.size {
panic("Insert failed, position out of range.")
}
node := &DoublyLinkedNode{data: data}
if l.size == 0 {
l.head = node
l.tail = node
} else if pos == 0 {
node.next = l.head
l.head.prev = node
l.head = node
} else if pos == l.size {
l.tail.next = node
node.prev = l.tail
l.tail = node
} else {
pre := l.head
for i := 0; i < pos-1; i++ {
pre = pre.next
}
node.next = pre.next
pre.next.prev = node
pre.next = node
node.prev = pre
}
l.size++
}
func (l *DoublyLinkedList) Get(pos int) interface{} {
if pos < 0 || pos >= l.size {
panic("Get failed, position out of range.")
}
cur := l.head
for i := 0; i < pos; i, cur = i+1, cur.next {
}
return cur.data
}
func (l *DoublyLinkedList) Remove(pos int) interface{} {
if pos < 0 || pos >= l.size {
panic("Remove failed, position out of range.")
}
if l.size == 1 {
data := l.head.data
l.head = nil
l.tail = nil
l.size = 0
return data
}
cur := l.head
for i := 0; i < pos; i++ {
cur = cur.next
}
if cur == l.head {
l.head = l.head.next
}
if cur == l.tail {
l.tail = l.tail.prev
}
if cur.prev != nil {
cur.prev.next = cur.next
}
if cur.next != nil {
cur.next.prev = cur.prev
}
data := cur.data
cur = nil
l.size--
return data
}
func (l *DoublyLinkedList) Delete(data interface{}) {
for cur := l.head; cur != nil; cur = cur.next {
if cur.data == data {
if cur == l.head {
l.head = l.head.next
cur.next.prev = nil
cur.next = nil
} else if cur == l.tail {
l.tail = l.tail.prev
cur.prev.next = nil
cur.prev = nil
} else {
cur.prev.next = cur.next
cur.next.prev = cur.prev
cur.prev = nil
cur.next = nil
}
l.size--
return
}
}
}
func (l *DoublyLinkedList) Contains(data interface{}) bool {
for cur := l.head; cur != nil; cur = cur.next {
if cur.data == data {
return true
}
}
return false
}
func (l *DoublyLinkedList) IsEmpty() bool {
return l.size == 0
}
func (l *DoublyLinkedList) Size() int {
return l.size
}
func (l *DoublyLinkedList) String() string {
var builder strings.Builder
builder.WriteString("Head: ")
for cur := l.head; cur != nil; cur = cur.next {
builder.WriteString(fmt.Sprintf("%v<--->", cur.data))
}
builder.WriteString("Null")
return builder.String()
}