forked from kelvins/algorithms-and-data-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
doubly_linked_list.go
184 lines (137 loc) · 2.79 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package main
import "fmt"
type Node struct {
Value int
Next *Node
Prev *Node
}
type DoublyLinkedList struct {
Head *Node
Tail *Node
}
func (list *DoublyLinkedList) AddLeft(value int) {
node := Node{Value: value}
if list.Head == nil {
list.Head = &node
list.Tail = &node
return
}
node.Next = list.Head
list.Head.Prev = &node
list.Head = &node
}
func (list *DoublyLinkedList) AddRight(value int) {
node := Node{Value: value}
if list.Tail == nil {
list.Tail = &node
list.Head = &node
return
}
node.Prev = list.Tail
list.Tail.Next = &node
list.Tail = &node
}
func (list *DoublyLinkedList) RemoveLeft() {
if list.Head == nil {
return
}
list.Head = list.Head.Next
if list.Head != nil {
list.Head.Prev = nil
} else {
list.Tail = nil
}
}
func (list *DoublyLinkedList) RemoveRight() {
if list.Tail == nil {
return
}
list.Tail = list.Tail.Prev
if list.Tail != nil {
list.Tail.Next = nil
} else {
list.Head = nil
}
}
func (list *DoublyLinkedList) FindAt(idx int) *Node {
pointer := list.Head
for i := 0; i < idx; i++ {
pointer = pointer.Next
if pointer == nil {
return nil
}
}
return pointer
}
func (list *DoublyLinkedList) AddAt(idx, value int) {
if idx == 0 {
list.AddLeft(value)
return
}
foundNode := list.FindAt(idx)
if foundNode == nil {
return
}
prevNode := foundNode.Prev
if prevNode == nil {
return
}
newNode := Node{Value: value}
newNode.Prev = prevNode
newNode.Next = foundNode
prevNode.Next = &newNode
foundNode.Prev = &newNode
}
func (list *DoublyLinkedList) RemoveAt(idx int) {
if idx == 0 {
list.RemoveLeft()
return
}
foundNode := list.FindAt(idx)
if foundNode == nil {
return
}
prevNode := foundNode.Prev
if prevNode == nil {
return
}
prevNode.Next = foundNode.Next
if foundNode.Next == nil {
list.Tail = prevNode
return
}
foundNode.Next.Prev = prevNode
}
func (list *DoublyLinkedList) ToSlice() []int {
var slice []int
for pointer := list.Head; pointer != nil; pointer = pointer.Next {
slice = append(slice, pointer.Value)
}
return slice
}
func main() {
list := DoublyLinkedList{}
fmt.Println("AddLeft(1, 2)")
list.AddLeft(1)
list.AddLeft(2)
fmt.Println("list.toSlice(): ", list.ToSlice())
fmt.Println("AddRight(3, 4)")
list.AddRight(3)
list.AddRight(4)
fmt.Println("list.toSlice(): ", list.ToSlice())
fmt.Println("RemoveLeft()")
list.RemoveLeft()
fmt.Println("list.toSlice(): ", list.ToSlice())
fmt.Println("RemoveRight()")
list.RemoveRight()
fmt.Println("list.toSlice(): ", list.ToSlice())
fmt.Println("AddAt(1, 5)")
list.AddAt(1, 5)
fmt.Println("list.toSlice(): ", list.ToSlice())
fmt.Println("RemoveAt(2)")
list.RemoveAt(2)
fmt.Println("list.toSlice(): ", list.ToSlice())
fmt.Println("FindAt(0)")
node := list.FindAt(0)
fmt.Println("node.Value: ", node.Value)
}