made vectors in lists work
This commit is contained in:
+4
-17
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user