-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvector.c
More file actions
38 lines (32 loc) · 681 Bytes
/
vector.c
File metadata and controls
38 lines (32 loc) · 681 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
#include "vector.h"
void init_vector(struct vector* v)
{
v->data = malloc(sizeof(int) * INIT_VECTOR_SIZE);
v->size = 0;
v->capacity = INIT_VECTOR_SIZE;
}
int access_element_vector(struct vector* v, size_t index)
{
if(index >= v->size)
exit(OUT_OF_BOUNDS);
return v->data[index];
}
void insert_element_vector(struct vector* v, int element_to_insert)
{
if(v->capacity == v->size)
{
v->capacity *= 2;
v->data = realloc(v->data, sizeof(int) * v->capacity);
}
v->data[v->size] = element_to_insert;
v->size += 1;
}
int* vector_get_ptr(struct vector* v)
{
return v->data;
}
void free_vector(struct vector* v)
{
free(v->data);
v->size = 0;
}