-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
82 lines (60 loc) · 1.59 KB
/
main.c
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
#include <stdio.h>
#include <stdlib.h>
#define DEFAULT_CAPACITY 10
typedef struct {
int size, capacity;
int* storage;
} IntVector;
void* new_int_vector() {
IntVector* res = malloc(sizeof(IntVector));
res->size = 0;
res->capacity = DEFAULT_CAPACITY;
// initialise storage
int* storage = malloc(sizeof(int) * DEFAULT_CAPACITY);
res->storage = storage;
return res;
}
int size(IntVector* vector) {
return vector->size;
}
void resize(IntVector* vector, int new_size) {
int* old_storage = vector->storage;
int* new_storage = malloc(sizeof(int) * new_size);
for (int i = 0; i < vector->size; i++) {
printf("old num: %d\n", old_storage[i]);
new_storage[i] = old_storage[i];
}
// prevent memory leak
free(old_storage);
vector->capacity = new_size;
vector->storage = new_storage;
}
void insert(IntVector* vector, int value) {
int size = vector->size;
int capacity = vector->capacity;
// if the size is more than capacity then just resize array
if (size > capacity || size+1 > capacity) {
printf("%s", "capacity exceeded, resizing vector\n");
resize(vector, capacity * 2);
}
int* storage = vector->storage;
storage[size] = value;
vector->size += 1;
}
int get(IntVector* vector, int index) {
if (index > vector->capacity) return -1;
int* storage = vector->storage;
return storage[index];
}
int main() {
IntVector* vector = new_int_vector();
int limit = 15;
for (int i = 0; i < limit; ++i) {
insert(vector, i);
}
for (int i = 0; i < limit; ++i) {
printf("num: %d\n", get(vector, i));
}
free(vector);
return 1;
}