made work: removeAtIndex(x);
This commit is contained in:
@@ -8,12 +8,34 @@ int main() {
|
||||
listPush(another_list, CHAR, &(char){'x'});
|
||||
listPush(another_list, INT, &(int){123});
|
||||
listPush(another_list, FLOAT, &(float){3.14});
|
||||
listPush(another_list, LIST, another_list);
|
||||
listPush(another_list, LIST, another_list);
|
||||
printf("do not put the list into itself, it does weird stuff\n");
|
||||
// BIG NONO -> listPush(another_list, LIST, another_list);
|
||||
|
||||
listRemoveAt(another_list, 1);
|
||||
|
||||
list_t* l = listInit();
|
||||
|
||||
listPush(l, INT, &(int){5+2});
|
||||
listPush(l, INT, &(int){5*2});
|
||||
listPush(l, FLOAT, &(float){5/2});
|
||||
|
||||
listPush(another_list, LIST, l);
|
||||
|
||||
listPrint(another_list);
|
||||
|
||||
listCleanup(another_list);
|
||||
|
||||
// vector_t* v = vectorInit(INT);
|
||||
//
|
||||
// vectorPush(v, &(int){123});
|
||||
// vectorPush(v, &(int){456});
|
||||
// vectorPush(v, &(int){789});
|
||||
//
|
||||
// vectorRemoveAt(v, 1);
|
||||
//
|
||||
// vectorPrint(v);
|
||||
//
|
||||
// vectorCleanup(v);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -28,3 +28,5 @@ void listCleanup(list_t* l); // f
|
||||
void* listPop(list_t* l); // popping the last value of the list and returning it
|
||||
|
||||
void listInsertAt(list_t* l, int index, dataType_e type, void* element); // inserts an element at a given index
|
||||
|
||||
void listRemoveAt(list_t* l, int index);
|
||||
|
||||
+2
-1
@@ -44,7 +44,8 @@ vectorToString(vector_t* v); // convert the vector into a regular string
|
||||
void*
|
||||
vectorGetAt(vector_t* v, int index); // get an element at the given index
|
||||
|
||||
|
||||
void
|
||||
vectorRemoveAt(vector_t* v, int index);
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -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