made work: removeAtIndex(x);
This commit is contained in:
@@ -214,3 +214,12 @@ listInsertAt(list_t* l, int index, dataType_e type, void* element) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
listRemoveAt(list_t* l, int index) {
|
||||
vectorRemoveAt(l->data, index);
|
||||
l->size -= 1;
|
||||
if (l->size == l->capacity) {
|
||||
l->capacity /= 2;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -430,3 +430,52 @@ vectorInsertAt(vector_t* v, int index, void* element) {
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
vectorRemoveAt(vector_t* v, int index) {
|
||||
if (v->size == 0) {
|
||||
fprintf(stderr, "cannot remove from empty vector\n");
|
||||
exit(EXIT_FAILURE);
|
||||
} else if (index >= v->size || index < 0) {
|
||||
fprintf(stderr, "index out of range\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
switch (v->type) {
|
||||
case INT:
|
||||
// move all elements to the right by one
|
||||
for (int i = index; i < v->size; i++) {
|
||||
*((int *)(v->data) + i) = *((int *)(v->data) + i+1);
|
||||
}
|
||||
break;
|
||||
case FLOAT:
|
||||
for (int i = index; i < v->size; i++) {
|
||||
*((float *)(v->data) + i) = *((float *)(v->data) + i+1);
|
||||
}
|
||||
break;
|
||||
case CHAR:
|
||||
for (int i = index; i < v->size; i++) {
|
||||
*((char *)(v->data) + i) = *((char *)(v->data) + i+1);
|
||||
}
|
||||
break;
|
||||
case VECTOR:
|
||||
for (int i = index; i < v->size; i++) {
|
||||
*((vector_t*)(v->data) + i) = *((vector_t*)(v->data) + i+1);
|
||||
}
|
||||
break;
|
||||
case LIST:
|
||||
for (int i = index; i < v->size; i++) {
|
||||
*((list_t*)(v->data) + i) = *((list_t*)(v->data) + i+1);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, "specify a valid vector type\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
v->size -= 1;
|
||||
if (v->size < v->capacity/2) {
|
||||
v->capacity /= 2;
|
||||
v->data = realloc(v->data, v->elementSize * v->capacity);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user