diff options
Diffstat (limited to 'main.c')
| -rw-r--r-- | main.c | 73 |
1 files changed, 73 insertions, 0 deletions
| @@ -0,0 +1,73 @@ | |||
| 1 | #include <stdio.h> | ||
| 2 | #include <stdlib.h> | ||
| 3 | |||
| 4 | |||
| 5 | typedef struct { | ||
| 6 | int *arr; | ||
| 7 | int max_size; | ||
| 8 | int cur_size; | ||
| 9 | } vector_t; | ||
| 10 | |||
| 11 | vector_t vector_init(int size){ | ||
| 12 | vector_t vec; | ||
| 13 | vec.max_size = size; | ||
| 14 | vec.arr = malloc(sizeof(int)*vec.max_size); | ||
| 15 | vec.cur_size = 0; | ||
| 16 | return vec; | ||
| 17 | } | ||
| 18 | |||
| 19 | int size(vector_t vec){ | ||
| 20 | return vec.cur_size; | ||
| 21 | } | ||
| 22 | |||
| 23 | int capacity(vector_t vec){ | ||
| 24 | return vec.max_size; | ||
| 25 | } | ||
| 26 | |||
| 27 | int is_empty(vector_t vec){ | ||
| 28 | if (vec.cur_size == 0){ | ||
| 29 | return 1; | ||
| 30 | } | ||
| 31 | return 0; | ||
| 32 | } | ||
| 33 | |||
| 34 | int at(vector_t vec, int index){ | ||
| 35 | if (index >= vec.max_size){ | ||
| 36 | printf("Index out of bound!!\n"); | ||
| 37 | exit(1); | ||
| 38 | } | ||
| 39 | |||
| 40 | return vec.arr[index]; | ||
| 41 | } | ||
| 42 | |||
| 43 | void push(vector_t *vec, int value){ | ||
| 44 | if (value >= vec->max_size){ | ||
| 45 | printf("Array out of Size!! Not yet implemented resize!!\n"); | ||
| 46 | exit(1); | ||
| 47 | } | ||
| 48 | |||
| 49 | vec->arr[vec->cur_size] = value; | ||
| 50 | vec->cur_size++; | ||
| 51 | } | ||
| 52 | |||
| 53 | void insert(vector_t *vec, int index, int item){ | ||
| 54 | |||
| 55 | } | ||
| 56 | |||
| 57 | int main(void){ | ||
| 58 | vector_t vec = vector_init(16); | ||
| 59 | |||
| 60 | printf("The size of the vector is %d.\n", size(vec)); | ||
| 61 | printf("The capacity of the vector is %d.\n", capacity(vec)); | ||
| 62 | printf("is_empty returns: %d.\n", is_empty(vec)); | ||
| 63 | |||
| 64 | push(&vec, 10); | ||
| 65 | printf("-----\n"); | ||
| 66 | |||
| 67 | printf("The size of the vector is %d.\n", size(vec)); | ||
| 68 | printf("The first element of the vector is %d.\n", at(vec, 0)); | ||
| 69 | printf("is_empty returns: %d.\n", is_empty(vec)); | ||
| 70 | |||
| 71 | |||
| 72 | return 0; | ||
| 73 | } | ||
