Skip to content
Snippets Groups Projects
Commit ce536fcd authored by iliya.saroukha's avatar iliya.saroukha
Browse files

fix: vec_pop returns the value to the user instead of simply removing it

parent d9dd6580
No related branches found
No related tags found
No related merge requests found
...@@ -192,15 +192,23 @@ bool is_in_vec(vec_t *vec, int64_t data) { ...@@ -192,15 +192,23 @@ bool is_in_vec(vec_t *vec, int64_t data) {
} }
/** /**
* @brief Function that removes the last element in a vector * @brief Function that removes the last element in the vector and returns it
* to the user
* *
* @param vec Vector from which to pop the value * @param vec Vector from which the value will be removed
* @return Last element in the vector
*/ */
void vec_pop(vec_t *vec) { int64_t vec_pop(vec_t *vec) {
err_init(vec); if (vec == NULL) {
fprintf(stderr, "Vector not initialised\n");
return -1;
}
err_len_zero(vec); if (vec->len == 0) {
fprintf(stderr, "Length of the vector is 0!\n");
return -1;
}
// needs resizing // needs resizing
// should be extracted into separate function // should be extracted into separate function
if (vec->len <= vec->capacity / 2) { if (vec->len <= vec->capacity / 2) {
...@@ -212,6 +220,7 @@ void vec_pop(vec_t *vec) { ...@@ -212,6 +220,7 @@ void vec_pop(vec_t *vec) {
} }
vec->len--; vec->len--;
return vec->data[vec->len];
} }
/** /**
...@@ -292,6 +301,6 @@ void vec_destroy(vec_t **vec) { ...@@ -292,6 +301,6 @@ void vec_destroy(vec_t **vec) {
free((*vec)->data); free((*vec)->data);
free((*vec)); free((*vec));
(*vec) = NULL; *vec = NULL;
} }
} }
...@@ -20,7 +20,7 @@ extern bool is_in_vec(vec_t *vec, int64_t data); ...@@ -20,7 +20,7 @@ extern bool is_in_vec(vec_t *vec, int64_t data);
extern void vec_remove(vec_t *vec, size_t idx); extern void vec_remove(vec_t *vec, size_t idx);
extern void vec_pop(vec_t *vec); extern int64_t vec_pop(vec_t *vec);
extern void vec_print(vec_t *vec); extern void vec_print(vec_t *vec);
......
...@@ -56,6 +56,7 @@ int main(int argc, char **argv) { ...@@ -56,6 +56,7 @@ int main(int argc, char **argv) {
} }
int64_t to_push; int64_t to_push;
int64_t item_poped;
size_t idx_to_drop; size_t idx_to_drop;
size_t idx_to_insert; size_t idx_to_insert;
...@@ -79,7 +80,9 @@ int main(int argc, char **argv) { ...@@ -79,7 +80,9 @@ int main(int argc, char **argv) {
vec_remove(new_vec, idx_to_drop); vec_remove(new_vec, idx_to_drop);
break; break;
case 3: case 3:
vec_pop(new_vec); item_poped = vec_pop(new_vec);
fprintf(stdout, "Last value in the vector: %ld was removed\n",
item_poped);
break; break;
case 4: case 4:
vec_print(new_vec); vec_print(new_vec);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment