-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.c
More file actions
45 lines (42 loc) · 991 Bytes
/
vector.c
File metadata and controls
45 lines (42 loc) · 991 Bytes
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
#include "vector.h"
vector initVector() {
vector v;
for (int i = 0; i < 256; i++) {
v.array[i] = 0;
}
v.SIZE = 0;
return v;
}
void pushbackVector(vector* vec, int32_t val) {
if ((*vec).SIZE < 256) {
(*vec).array[(*vec).SIZE] = val;
(*vec).SIZE += 1;
}else{
printf("Max is 256!\n");
}
}
void popbackVector(vector* vec) {
if ((*vec).SIZE > 0) {
(*vec).SIZE -= 1;
(*vec).array[(*vec).SIZE] = 0;
}else{
printf("Min is 0!\n");
}
}
void clearVector(vector* vec) {
while((*vec).SIZE!=0){
(*vec).array[(*vec).SIZE-1] = 0;
(*vec).SIZE -=1;
}
}
void eraseVector(vector* vec, int index) {
if (index < 0 || index >= (*vec).SIZE) return;
for (int i = index; i < (*vec).SIZE - 1; i++) {
(*vec).array[i] = (*vec).array[i + 1];
}
(*vec).SIZE -= 1;
(*vec).array[(*vec).SIZE] = 0;
}
int32_t BottomVector(vector* vec){
return vec->array[vec->SIZE-1];
}