From b68e6c4d2bd40ad98d5af3220b590b6bcd91905c Mon Sep 17 00:00:00 2001 From: Odweta Date: Fri, 22 Dec 2023 16:19:47 +0100 Subject: [PATCH] made vectors in lists work --- README.md | 2 +- demo.c | 4 ++++ src/list.c | 21 ++++----------------- 3 files changed, 9 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index fb6df59..fb98dbe 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,7 @@ I got bored with having to use complicated checking and stuff when working with + Printing a list + Popping the last value of a list + Cleanup of a list ++ Vectors inside of a list #### TODO: @@ -48,7 +49,6 @@ I got bored with having to use complicated checking and stuff when working with + Verify multi-dimensional lists working + Get/set at a given index + Remove at a given index -+ Verify vectors working inside of lists ## Installation diff --git a/demo.c b/demo.c index d091de5..fc8e7ee 100644 --- a/demo.c +++ b/demo.c @@ -1,9 +1,13 @@ #include +#include #include int main(int argc, char **argv) { list_t *l = listInit(); + vector_t *to_be_pushed_vec = vectorInit(INT); + vectorPush(to_be_pushed_vec, &(int){1620}); + listPush(l, VECTOR, (vector_t *)to_be_pushed_vec); listPush(l, FLOAT, &(float){3.14}); listPush(l, CHAR, &(char){'x'}); listPush(l, INT, &(int){123}); diff --git a/src/list.c b/src/list.c index 30d1168..76f17e7 100644 --- a/src/list.c +++ b/src/list.c @@ -44,22 +44,9 @@ listPush(list_t* l, dataType_e type, void* element) { vectorPush((vector_t *)l->data, newVec); break; case VECTOR: - // Instead of reallocating memory for vector_t pointers, allocate memory for the vector data - if (l->size == 0) { - // Allocate memory for the vector data for the first time - l->data = malloc(sizeof(vector_t) * l->capacity); - } else { - // Reallocate memory for the vector data - l->data = realloc((vector_t *)l->data, sizeof(vector_t) * l->capacity); - } - if (!l->data) { - perror("failed to allocate memory for vector data\n"); - exit(EXIT_FAILURE); - } - - // Copy the vector data from the element to the vector - memcpy((vector_t *)(l->data) + l->size, (vector_t *)element, sizeof(vector_t)); - + newVec = vectorInit(VECTOR); + vectorPush(newVec, element); + vectorPush((vector_t *)l->data, newVec); break; default: perror("specify a valid vector type\n"); @@ -90,7 +77,7 @@ listPrint(list_t* l) { printf("'%c'", *((char *)vectorGetAt(ithVec, 0))); break; case VECTOR: - _printVectorRecursive((vector_t *)l->data, 0); + _printVectorRecursive(ithVec->data, 0); break; }