-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDoublyLinkedList.go
188 lines (151 loc) · 3.36 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
package linked_lists
import "errors"
type DoublyLinkedList[T comparable] struct {
Head *Node[T]
Tail *Node[T]
}
func NewDoublyLinkedList[T comparable]() *DoublyLinkedList[T] {
return &DoublyLinkedList[T]{Head: nil}
}
func (list *DoublyLinkedList[T]) GetItems() []T {
var listItems []T
item := list.Head
for item != nil {
listItems = append(listItems, item.Value)
item = item.Next
}
return listItems
}
func (list *DoublyLinkedList[T]) AddItem(val T, pos ...int) error {
// Determine the position to insert
position := -1
if len(pos) > 0 {
if pos[0] < 1 {
return errors.New("invalid position")
}
position = pos[0]
}
newNode := &Node[T]{
Value: val,
}
// Case: The list is empty
if list.Head == nil {
list.Head = newNode
return nil
}
// Case: Insert at the beginning
if position == 1 {
newNode.Next = list.Head
list.Head.Prev = newNode
list.Head = newNode
return nil
}
// Case: Insert at the specified position or at the end
current := list.Head
currentPos := 1
// Traverse the list and find the correct positional node
for current.Next != nil && (position == -1 || currentPos < position-1) {
current = current.Next
currentPos++
}
if position != -1 && currentPos < position-1 {
return errors.New("invalid position")
}
newNode.Next = current.Next
newNode.Prev = current
if current.Next != nil {
current.Next.Prev = newNode
} else {
// Update the tail if the node is inserted at the end
list.Tail = newNode
}
current.Next = newNode
return nil
}
func (list *DoublyLinkedList[T]) RemoveItemByValue(val T) error {
if list.Head == nil {
return errors.New("empty list")
}
// Case: remove the first item
if list.Head.Value == val {
if list.Head.Next != nil {
list.Head = list.Head.Next
list.Head.Prev = nil
} else {
// If there's only one node
list.Head = nil
list.Tail = nil
}
return nil
}
item := list.Head
for item != nil {
if item.Value == val {
if item.Next != nil {
item.Next.Prev = item.Prev
} else {
// Update tail if last node is removed
list.Tail = item.Prev
}
if item.Prev != nil {
item.Prev.Next = item.Next
}
return nil
}
item = item.Next
}
return errors.New("list doesn't contain provided value")
}
func (list *DoublyLinkedList[T]) RemoveItemByPosition(pos int) error {
if list.Head == nil {
return errors.New("empty list")
}
if pos < 1 {
return errors.New("invalid position")
}
// Case: remove the first item
if pos == 1 {
if list.Head.Next != nil {
list.Head = list.Head.Next
list.Head.Prev = nil
} else {
// If there's only one node
list.Head = nil
list.Tail = nil
}
return nil
}
item := list.Head
position := 1
// Find the item by position
for item != nil && position < pos-1 {
item = item.Next
position++
}
if item == nil || item.Next == nil {
return errors.New("invalid position")
}
// Remove the node
nodeToRemove := item.Next
item.Next = nodeToRemove.Next
if nodeToRemove.Next != nil {
nodeToRemove.Next.Prev = item
} else {
// Update the tail
list.Tail = item
}
return nil
}
func (list *DoublyLinkedList[T]) FindItem(val T) (*Node[T], error) {
if list.Head == nil {
return nil, errors.New("empty list")
}
item := list.Head
for item != nil {
if item.Value == val {
return item, nil
}
item = item.Next
}
return nil, errors.New("list doesn't contain provided value")
}