-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedList.go
208 lines (173 loc) · 3.23 KB
/
LinkedList.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package linkedlist
import (
"errors"
"fmt"
)
type LinkedList struct {
head *node[any]
}
// Add node
func (L *LinkedList) Add(value any) {
var newnode *node[any] = new(node[any])
newnode.data = value
newnode.next = nil
if L.head == nil {
L.head = newnode
} else {
var temp *node[any] = L.head
for temp.next != nil {
temp = temp.next
}
temp.next = newnode
}
}
// First Index Add node
func (L *LinkedList) AddFirst(value any) {
var newnode *node[any] = new(node[any])
newnode.data = value
newnode.next = nil
if L.head == nil {
L.head = newnode
} else {
newnode.next = L.head
L.head = newnode
}
}
// Linked List Traversal
func (L *LinkedList) Traversal() {
if L.head == nil {
fmt.Println("Your linked list is empty")
} else {
var temp *node[any] = L.head
for temp != nil {
fmt.Printf("[%v] -> ", temp.data)
temp = temp.next
}
fmt.Println("nil")
}
}
// Includes
func (L *LinkedList) Includes(value any) bool {
var temp *node[any] = L.head
for temp != nil {
if temp.data == value {
return true
}
temp = temp.next
}
return false
}
// Element Count
func (L *LinkedList) Count() uint {
var temp *node[any] = L.head
var count uint = 0
for temp != nil {
count++
temp = temp.next
}
return count
}
// Get Value
func (L *LinkedList) Get(index uint) any {
if L.Count() > index {
var temp *node[any] = L.head
for i := 0; i != int(index); i++ {
temp = temp.next
}
return temp.data
} else {
return nil
}
}
// Get First Value
func (L *LinkedList) GetFirst() any {
if L.head != nil {
return L.head.data
} else {
return nil
}
}
// Get Last Value
func (L *LinkedList) GetLast() any {
if L.head != nil {
var temp *node[any] = L.head
for temp.next != nil {
temp = temp.next
}
return temp.data
} else {
return nil
}
}
// Update Index Value
func (L *LinkedList) Update(index uint, value any) error {
if L.Count() > index {
var temp *node[any] = L.head
for i := 0; i != int(index); i++ {
temp = temp.next
}
temp.data = value
return nil
} else {
return errors.New("index is out of range")
}
}
// Remove Index
func (L *LinkedList) RemoveIndex(index uint) error {
if L.Count() > index {
if index == 0 {
L.head = L.head.next
} else {
var temp *node[any] = L.head
for i := 1; i != int(index); i++ {
temp = temp.next
}
temp.next = temp.next.next
}
return nil
} else {
return errors.New("Index out of range")
}
}
// Remove First Value
func (L *LinkedList) Remove(value any) {
if L.head == nil {
return
}
if L.head.data == value {
L.head = L.head.next
} else {
var temp *node[any] = L.head
for temp.next != nil {
if temp.next.data == value {
temp.next = temp.next.next
return
}
temp = temp.next
}
}
}
// Revome All Value
func (L *LinkedList) RemoveAll(value any) {
for L.head != nil && L.head.data == value {
L.head = L.head.next
}
var temp *node[any] = L.head
if temp != nil {
for temp.next != nil {
if temp.next.data == value {
temp.next = temp.next.next
} else {
temp = temp.next
}
}
}
}
// Linked List Foreach
func (L *LinkedList) ForEach(f func(index uint, value any)) {
var temp *node[any] = L.head
for i := 0; temp != nil; i++ {
f(uint(i), temp.data)
temp = temp.next
}
}