diff options
Diffstat (limited to 'main.c')
| -rw-r--r-- | main.c | 30 |
1 files changed, 14 insertions, 16 deletions
| @@ -78,7 +78,13 @@ void insert(vector_t *vec, int index, int val){ | |||
| 78 | 78 | ||
| 79 | void prepend(vector_t *vec, int value){ | 79 | void prepend(vector_t *vec, int value){ |
| 80 | insert(vec, 0, value); | 80 | insert(vec, 0, value); |
| 81 | } | ||
| 81 | 82 | ||
| 83 | int pop(vector_t *vec){ | ||
| 84 | // remove item at the end, return value | ||
| 85 | int ret = vec->arr[vec->cur_size-1]; | ||
| 86 | vec->cur_size--; | ||
| 87 | return ret; | ||
| 82 | } | 88 | } |
| 83 | 89 | ||
| 84 | void print_vec(vector_t vec){ | 90 | void print_vec(vector_t vec){ |
| @@ -101,25 +107,17 @@ int main(void){ | |||
| 101 | printf("The capacity of the vector is %d.\n", capacity(vec)); | 107 | printf("The capacity of the vector is %d.\n", capacity(vec)); |
| 102 | printf("is_empty returns: %d.\n", is_empty(vec)); | 108 | printf("is_empty returns: %d.\n", is_empty(vec)); |
| 103 | 109 | ||
| 104 | push(&vec, 1); | ||
| 105 | push(&vec, 2); | ||
| 106 | push(&vec, 3); | ||
| 107 | push(&vec, 4); | ||
| 108 | push(&vec, 5); | ||
| 109 | push(&vec, 6); | ||
| 110 | push(&vec, 7); | ||
| 111 | push(&vec, 8); | ||
| 112 | push(&vec, 9); | ||
| 113 | push(&vec, 10); | ||
| 114 | push(&vec, 11); | ||
| 115 | push(&vec, 12); | ||
| 116 | push(&vec, 13); | ||
| 117 | push(&vec, 14); | ||
| 118 | push(&vec, 15); | ||
| 119 | prepend(&vec, 0); | ||
| 120 | 110 | ||
| 121 | printf("-----\n"); | 111 | printf("-----\n"); |
| 122 | 112 | ||
| 113 | for (int i = 0; i < vec.max_size; i++){ | ||
| 114 | push(&vec, i); | ||
| 115 | } | ||
| 116 | |||
| 117 | for (int i = 0; i < vec.max_size; i++){ | ||
| 118 | printf("pop: %d\n", pop(&vec)); | ||
| 119 | } | ||
| 120 | |||
| 123 | printf("The size of the vector is %d.\n", size(vec)); | 121 | printf("The size of the vector is %d.\n", size(vec)); |
| 124 | printf("The first element of the vector is %d.\n", at(vec, 0)); | 122 | printf("The first element of the vector is %d.\n", at(vec, 0)); |
| 125 | printf("is_empty returns: %d.\n", is_empty(vec)); | 123 | printf("is_empty returns: %d.\n", is_empty(vec)); |
