From 0ed98ea39c370345826a7d56df2f6c4f1573614c Mon Sep 17 00:00:00 2001 From: Abivarman <abivarman.kandiah@etu.hesge.ch> Date: Wed, 23 Feb 2022 13:21:33 +0100 Subject: [PATCH] Add push function --- header/vectors.h | 4 ++-- src/test_vectors.c | 6 ++++++ src/vectors.c | 15 ++++++++++++++- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/header/vectors.h b/header/vectors.h index 5347543..ad1d322 100644 --- a/header/vectors.h +++ b/header/vectors.h @@ -19,7 +19,7 @@ typedef int type; typedef struct vector_* vector; vector vector_create(); -vector vector_length(vector vec); - +int vector_length(vector vec); +void vector_push(vector vec, type element); #endif \ No newline at end of file diff --git a/src/test_vectors.c b/src/test_vectors.c index 31579d2..2605805 100644 --- a/src/test_vectors.c +++ b/src/test_vectors.c @@ -13,5 +13,11 @@ int main() { vector test = vector_create(); + + printf("%d \n", vector_length(test)); + + vector_push(test, 15); + + printf("%d \n", vector_length(test)); return 0; } \ No newline at end of file diff --git a/src/vectors.c b/src/vectors.c index 4941d2b..c7a85b3 100644 --- a/src/vectors.c +++ b/src/vectors.c @@ -24,7 +24,20 @@ vector vector_create() return vec; } -vector vector_length(vector vec) +int vector_length(vector vec) { return vec->length; } + +void vector_push(vector vec, type element) +{ + if(vec->length >= vec->capacity) + { + vec->content = realloc(vec->content, sizeof(type) * vec->capacity * 2); + vec->capacity = vec->capacity * 2; + } + + vec->content[vec->length] = element; + vec->length++; +} + -- GitLab