-
Notifications
You must be signed in to change notification settings - Fork 0
/
SinglyLinkedList.go
159 lines (125 loc) · 2.69 KB
/
SinglyLinkedList.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
package linked_lists
import "errors"
type Node[T comparable] struct {
Value T
Next *Node[T]
Prev *Node[T]
}
type SinglyLinkedList[T comparable] struct {
Head *Node[T]
}
func NewSinglyLinkedList[T comparable]() *SinglyLinkedList[T] {
return &SinglyLinkedList[T]{Head: nil}
}
func (list *SinglyLinkedList[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]
}
node := &Node[T]{
Value: val,
}
// Case: The list is empty
if list.Head == nil {
list.Head = node
return nil
}
// Case: Insert at the beginning
if position == 1 {
node.Next = list.Head
list.Head = node
return nil
}
// Case: Insert at the end
if position == -1 {
current := list.Head
for current.Next != nil {
current = current.Next
}
current.Next = node
return nil
}
// Case: Insert at the specified position
current := list.Head
currentPos := 1
for current != nil && currentPos < position-1 {
current = current.Next
currentPos++
}
// If current is nil, position is out of bounds
if current == nil {
return errors.New("invalid position")
}
node.Next = current.Next
current.Next = node
return nil
}
func (list *SinglyLinkedList[T]) GetItems() []T {
var listItems []T
item := list.Head
for item != nil {
listItems = append(listItems, item.Value)
item = item.Next
}
return listItems
}
func (list *SinglyLinkedList[T]) RemoveItemByValue(val T) error {
if list.Head == nil {
return errors.New("empty list")
}
// Special case: removing the head node
if list.Head.Value == val {
list.Head = list.Head.Next
return nil
}
item := list.Head
for item.Next != nil {
if item.Next.Value == val {
item.Next = item.Next.Next
return nil
}
item = item.Next
}
return errors.New("list doesn't contain provided value")
}
func (list *SinglyLinkedList[T]) RemoveItemByPosition(pos int) error {
if list.Head == nil {
return errors.New("empty list")
}
if pos < 1 {
return errors.New("invalid position")
}
if pos == 1 {
list.Head = list.Head.Next
return nil
}
item := list.Head
position := 1
for item != nil && position < pos-1 {
item = item.Next
position++
}
if item == nil || item.Next == nil {
return errors.New("invalid position")
}
// Remove the node by skipping it
item.Next = item.Next.Next
return nil
}
func (list *SinglyLinkedList[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")
}