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

Add vector remove

parent c59f139c
No related branches found
No related tags found
No related merge requests found
...@@ -25,5 +25,6 @@ void vector_push(vector vec, type element); ...@@ -25,5 +25,6 @@ void vector_push(vector vec, type element);
type vector_pop(vector vec); type vector_pop(vector vec);
void vector_set(vector vec, int index, type element); void vector_set(vector vec, int index, type element);
type vector_get(vector vec, int index); type vector_get(vector vec, int index);
type vector_remove(vector vec, int index);
#endif #endif
\ No newline at end of file
...@@ -12,23 +12,34 @@ ...@@ -12,23 +12,34 @@
int main() int main()
{ {
//Test Vector create
vector test = vector_create(); vector test = vector_create();
printf("Created Vector lenght : %d \n", vector_length(test)); printf("Created Vector lenght : %d \n", vector_length(test));
//Test Vector push
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));
//Test Vector pop
type val = vector_pop(test); type val = vector_pop(test);
printf("Modified Vector lenght : %d and the value we popped : %d \n", vector_length(test), val); printf("Modified Vector lenght : %d and the value we popped : %d \n", vector_length(test), val);
//Test Vector set and get
vector_push(test, 42); vector_push(test, 42);
vector_set(test, 0, 69); vector_set(test, 0, 69);
val = vector_get(test, 0); val = vector_get(test, 0);
printf("Modified Vector lenght : %d and the value we setted : %d \n", vector_length(test), val); printf("Modified Vector lenght : %d and the value we setted : %d \n", vector_length(test), val);
val = vector_pop(test);
//Test Vector remove
for (int i = 0; i < 6; i++)
{
vector_push(test, i);
}
printf("Value at index 3 : %d \n", vector_get(test, 3));
vector_remove(test, 3);
printf("New Value at index 3 : %d \n", vector_get(test, 3));
......
...@@ -78,3 +78,19 @@ type vector_get(vector vec, int index) ...@@ -78,3 +78,19 @@ type vector_get(vector vec, int index)
return vec->content[index]; return vec->content[index];
} }
type vector_remove(vector vec, int index)
{
assert(index < vec->length);
type tmp = vec->content[index];
vec->length--;
for (int i = index; i < vec->length; i++) // Ex : 0 1 2 {3} 4 5 // Index 3 , lenght 6
{
vec->content[i] = vec->content[i+1];
}
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