-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_linked_list.go
185 lines (165 loc) · 3.38 KB
/
my_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
185
package main
import "errors"
type MyLinkedList struct {
currSize int
head *Node
tail *Node
}
type Node struct {
value int
prev *Node
next *Node
}
func (list *MyLinkedList) size() int {
return list.currSize
}
func (list *MyLinkedList) isEmpty() bool {
return list.currSize == 0;
}
func (list *MyLinkedList) contains(val int) bool {
curr := list.head
for curr != nil {
if curr.value == val {
return true
}
curr = curr.next
}
return false
}
func (list *MyLinkedList) add(val int) {
newNode := Node{value: val}
if list.tail == nil {
list.head = &newNode
list.tail = &newNode
} else {
list.tail.next = &newNode
newNode.prev = list.tail
list.tail = &newNode
}
list.currSize ++
}
func (list *MyLinkedList) remove(val int) bool{
curr := list.head
for curr != nil {
if curr.value == val {
if curr.prev == nil {
list.head = curr.next
} else {
curr.prev.next = curr.next
}
if curr.next == nil {
list.tail = curr.prev
} else {
curr.next.prev = curr.prev
}
list.currSize--
return true
}
curr = curr.next
}
return false
}
func (list *MyLinkedList) containsAll(collection []int) bool {
for _,v := range collection {
if list.contains(v) == false {
return false
}
}
return true
}
func (list *MyLinkedList) addAll(collection []int) bool {
for _,v := range collection {
list.add(v)
}
return true
}
func (list *MyLinkedList) clear() {
list.head = nil
list.tail = nil
list.currSize = 0
}
func (list *MyLinkedList) get(idx int) (int, error) {
if idx >= list.currSize {
return 0, errors.New("index out of bounds")
}
curr := list.head
for p := 0; p < idx; p++ {
curr = curr.next
}
return curr.value, nil
}
func (list *MyLinkedList) set(idx int, value int) (int, error) {
if idx >= list.currSize {
return 0, errors.New("index out of bounds")
}
curr := list.head
for p := 0; p < idx; p++ {
curr = curr.next
}
oldVal := curr.value
curr.value = value
return oldVal, nil
}
// Inserts val so it is the element at index idx
func (list *MyLinkedList) addAtIndex(idx int, val int) (int, error) {
if idx > list.currSize {
return 0, errors.New("index out of bounds")
}
newNode := Node{value: val}
curr := list.head
for pos := 0; pos < idx; pos ++ {
curr = curr.next
}
//curr being null means that EITHER the list is currently empty OR I'm inserting at the end of the list
if curr == nil {
if list.currSize > 0 {
newNode.prev = list.tail
list.tail.next = &newNode
}
list.tail = &newNode //Either way this is the new tail
} else {
newNode.prev = curr.prev
newNode.next = curr
if newNode.prev != nil {
newNode.prev.next = &newNode
}
}
if idx == 0 {
list.head = &newNode
}
list.currSize ++
return val, nil
}
//removes the value at index idx
func (list *MyLinkedList) removeAtIndex(idx int) (int, error) {
if idx >= list.currSize {
return 0, errors.New("index out of bounds")
}
curr := list.head
for pos := 0; pos < idx ; pos ++ {
curr = curr.next
}
//curr is now the element that we want to remove.
if curr.prev == nil {
list.head = curr.next
} else {
curr.prev.next = curr.next
}
if curr.next == nil {
list.tail = curr.prev
} else {
curr.next.prev = curr.prev
}
list.currSize--
return curr.value, nil
}
func (list *MyLinkedList) indexOf(val int) int {
curr := list.head
for i:= 0; curr != nil; i++ {
if (curr.value == val) {
return i
}
curr = curr.next
}
return -1
}