forked from voiciphil/gitflow-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.c
More file actions
37 lines (29 loc) · 608 Bytes
/
vector.c
File metadata and controls
37 lines (29 loc) · 608 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
#include <stdlib.h>
#include "vector.h"
void init(struct vector *v) {
v->arr = malloc(sizeof(int));
v->cur = 0;
v->cap = 1;
}
void push_back(struct vector *v, int val) {
if (v->cur == v->cap) {
v->arr = realloc(v->arr, sizeof(int) * v->cap * 2);
v->cap *= 2;
}
v->arr[v->cur++] = val;
}
void pop_back(struct vector *v) {
if (empty(v) == 1) {
return;
}
v->cur--;
}
int *at(struct vector *v, int pos) {
if (v->cur <= pos) {
return NULL;
}
return v->arr + pos;
}
int empty(struct vector *v) {
return v->cur == 0;
}