Skip to content
Snippets Groups Projects
Commit 78428385 authored by abivarma.kandiah's avatar abivarma.kandiah
Browse files

Add pop function

parent 17b14223
No related branches found
No related tags found
No related merge requests found
...@@ -22,5 +22,6 @@ typedef struct vector_* vector; ...@@ -22,5 +22,6 @@ typedef struct vector_* vector;
vector vector_create(); vector vector_create();
int vector_length(vector vec); int vector_length(vector vec);
void vector_push(vector vec, type element); void vector_push(vector vec, type element);
type vector_pop(vector vec);
#endif #endif
\ No newline at end of file
...@@ -19,5 +19,12 @@ int main() ...@@ -19,5 +19,12 @@ int main()
vector_push(test, 15); vector_push(test, 15);
printf("Modified Vector lenght : %d \n", vector_length(test)); printf("Modified Vector lenght : %d \n", vector_length(test));
type val = vector_pop(test);
printf("Modified Vector lenght : %d and the value we popped : %d \n", vector_length(test), val);
return 0; return 0;
} }
\ No newline at end of file
...@@ -47,3 +47,20 @@ void vector_push(vector vec, type element) ...@@ -47,3 +47,20 @@ void vector_push(vector vec, type element)
vec->length++; vec->length++;
} }
type vector_pop(vector vec)
{
assert(vec->length > 0);
vec->length--;
type tmp = vec->content[vec->length];
if(vec->length < (vec->capacity / 4))
{
vec->content = realloc(vec->content, sizeof(type) * (vec->capacity / 2));
assert(vec->content != NULL);
vec->capacity = vec->capacity / 2;
}
return tmp;
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment