-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_array_list.go
131 lines (114 loc) · 2.59 KB
/
my_array_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
package main
import "errors"
type MyArrayList struct {
array []int
currSize int
}
func (list *MyArrayList) size() int {
return list.currSize
}
func (list *MyArrayList) isEmpty() bool {
return list.currSize == 0;
}
func (list *MyArrayList) contains(val int) bool {
for i := 0; i< list.currSize; i++ {
if list.array[i] == val {
return true
}
}
return false
}
func extend(list *MyArrayList) {
newArray := make([] int, list.currSize+10)
for i := 0; i < list.currSize; i++ {
newArray[i] = list.array[i]
}
list.array = newArray
}
func (list *MyArrayList) add(val int) {
//If there is not enough space in the array then we'll extend it, copy the values and then insert
if list.currSize == len(list.array) {
extend(list)
}
list.array[list.currSize] = val
list.currSize++
}
func (list *MyArrayList) remove(val int) bool{
found := false
for i:= 0; i< list.currSize; i++ {
if list.array[i] == val {
found = true
} else if found {
list.array[i-1] = list.array[i] //Shift all by 1 left. AFTER you found the element
}
}
if found {
list.currSize--
}
return found
}
func (list *MyArrayList) containsAll(collection []int) bool {
for _,v := range collection {
if list.contains(v) == false {
return false
}
}
return true
}
func (list *MyArrayList) addAll(collection []int) bool {
for _,v := range collection {
list.add(v)
}
return true
}
func (list *MyArrayList) clear() {
list.currSize = 0
}
func (list *MyArrayList) get(idx int) (int, error) {
if idx >= list.currSize {
return 0, errors.New("index out of bounds")
}
return list.array[idx], nil
}
func (list *MyArrayList) set(idx int, value int) (int, error) {
if idx >= list.currSize {
return 0, errors.New("index out of bounds")
}
list.array[idx] = value
return value, nil
}
// Inserts val so it is the element at index idx
func (list *MyArrayList) addAtIndex(idx int, val int) (int, error) {
if idx > list.currSize {
return 0, errors.New("index out of bounds")
}
if list.currSize == len(list.array) {
extend(list)
}
for i := list.currSize; i > idx ; i-- {
list.array[i] = list.array[i-1]
}
list.array[idx] = val
list.currSize++
return val, nil
}
//removes the value at index idx
func (list *MyArrayList) removeAtIndex(idx int) (int, error) {
if idx >= list.currSize {
return 0, errors.New("index out of bounds")
}
oldVal := list.array[idx]
for i := idx; i< (list.currSize -1); i++ {
list.array[i] = list.array[i+1]
}
list.currSize--
return oldVal, nil
}
func (list *MyArrayList) indexOf(val int) int {
for i := 0; i< list.currSize ; i++ {
if list.array[i] == val {
return i
}
}
return -1
}