made work: get/set at index in list

This commit is contained in:
Odweta
2023-12-25 02:05:26 +01:00
parent b00858c9be
commit 27068b0381
5 changed files with 105 additions and 34 deletions
+79
View File
@@ -223,3 +223,82 @@ listRemoveAt(list_t* l, int index) {
l->capacity /= 2;
}
}
void
listSetAt(list_t* l, int index, dataType_e type, void* element) {
if (l->size == 0) {
listPush(l, type, element);
} else if (index >= l->size || index < 0) {
fprintf(stderr, "index out of range\nmust be between 0 and %d, is %d\n", l->size-1, index);
exit(EXIT_FAILURE);
}
switch (type) {
case INT:
listRemoveAt(l, index);
if (index == l->size) {
l->data->size += 1;
if (l->data->size == l->data->capacity) {
l->data->capacity *= 2;
}
}
listInsertAt(l, index, INT, element);
break;
case FLOAT:
listRemoveAt(l, index);
if (index == l->size) {
l->data->size += 1;
if (l->data->size == l->data->capacity) {
l->data->capacity *= 2;
}
}
listInsertAt(l, index, FLOAT, element);
break;
case CHAR:
listRemoveAt(l, index);
if (index == l->size) {
l->data->size += 1;
if (l->data->size == l->data->capacity) {
l->data->capacity *= 2;
}
}
listInsertAt(l, index, CHAR, element);
break;
case VECTOR:
listRemoveAt(l, index);
if (index == l->size) {
l->data->size += 1;
if (l->data->size == l->data->capacity) {
l->data->capacity *= 2;
}
}
listInsertAt(l, index, VECTOR, element);
break;
case LIST:
listRemoveAt(l, index);
if (index == l->size) {
l->data->size += 1;
if (l->data->size == l->data->capacity) {
l->data->capacity *= 2;
}
}
listInsertAt(l, index, LIST, element);
break;
default:
fprintf(stderr, "specify a valid vector type\n");
exit(EXIT_FAILURE);
}
}
void*
listGetAt(list_t* l, int index) {
if (l->size == 0) {
fprintf(stderr, "list is empty\n");
exit(EXIT_FAILURE);
} else if (index >= l->size || index < 0) {
fprintf(stderr, "index out of range\n");
exit(EXIT_FAILURE);
}
return vectorGetAt((vector_t*)vectorGetAt(l->data, index), 0);
}