made popping with lists work. yay! (also new demo to show the thing)

This commit is contained in:
Odweta
2023-12-22 14:27:07 +01:00
parent ea527405e5
commit 45732c4b22
5 changed files with 47 additions and 14 deletions
+1 -1
View File
@@ -39,6 +39,7 @@ I got bored with having to use complicated checking and stuff when working with
+ Creating a list
+ Pushing into a list
+ Printing a list
+ Popping the last value of a list
+ Cleanup of a list
#### TODO:
@@ -46,7 +47,6 @@ I got bored with having to use complicated checking and stuff when working with
+ Inserting at a given index
+ Verify multi-dimensional lists working
+ Get/set at a given index
+ Popping
+ Remove at a given index
+ Verify vectors working inside of lists
+22
View File
@@ -1,4 +1,5 @@
#include <odweta/list.h>
#include <stdio.h>
int main(int argc, char **argv) {
list_t *l = listInit();
@@ -9,6 +10,27 @@ int main(int argc, char **argv) {
listPrint(l);
printf("popping for the 1st time\n");
int popped = *((int *)listPop(l));
printf("popped: %d\n", popped);
listPrint(l);
printf("popping for the 2nd time\n");
char cpopped = *((char *)listPop(l));
printf("popped: '%c'\n", cpopped);
listPrint(l);
printf("popping for the 3rd time\n");
float fpopped = *((float *)listPop(l));
printf("popped: %f\n", fpopped);
listPrint(l);
listCleanup(l);
return 0;
+6 -4
View File
@@ -17,10 +17,12 @@ typedef struct {
vector_t* data; // vector of vectors
} list_t;
list_t* listInit(); // returns a new list
list_t* listInit(); // returns a new list
void listPush(list_t* l, dataType_e type, void* element); // appends an element at the end of a list
void listPush(list_t* l, dataType_e type, void* element); // appends an element at the end of a list
void listPrint(list_t* l); // prints a list
void listPrint(list_t* l); // prints a list
void listCleanup(list_t* l); // freeing pointers
void listCleanup(list_t* l); // freeing pointers
void* listPop(list_t* l); // popping the last value of the list and returning it
+12
View File
@@ -106,3 +106,15 @@ listCleanup(list_t* l) {
vectorCleanup((vector_t *)l->data);
free(l);
}
void*
listPop(list_t* l) {
vector_t *first_ptr = (vector_t *)vectorPop((vector_t *)l->data);
void *ptr = vectorPop(first_ptr);
l->size -= 1;
if (l->size <= l->capacity / 2) l->capacity /= 2;
if (l->capacity == 0) l->capacity = 1;
return ptr;
}
+6 -9
View File
@@ -216,30 +216,27 @@ vectorPop(vector_t* v) {
int *iretval = (int *)(v->data) + v->size-1;
v->size -= 1;
if (v->size <= v->capacity / 2) v->capacity /= 2;
v->data = realloc(v->data, v->elementSize * v->capacity);
if (v->capacity == 0) v->capacity = 1;
return iretval;
if (v->capacity == 0) v->capacity = 1;
return iretval;
case FLOAT:
float *fretval = (float *)(v->data) + v->size-1;
v->size -= 1;
if (v->size <= v->capacity / 2) v->capacity /= 2;
if (v->capacity == 0) v->capacity = 1;
v->data = realloc(v->data, v->elementSize * v->capacity);
return fretval;
return fretval;
case CHAR:
char *cretval = (char *)(v->data) + v->size-1;
v->size -= 1;
if (v->size <= v->capacity / 2) v->capacity /= 2;
if (v->capacity == 0) v->capacity = 1;
v->data = realloc(v->data, v->elementSize * v->capacity);
return cretval;
return cretval;
case VECTOR:
vector_t *vretval = (vector_t *)(v->data) + v->size-1;
v->size -= 1;
if (v->size <= v->capacity / 2) v->capacity /= 2;
if (v->capacity == 0) v->capacity = 1;
v->data = realloc(v->data, v->elementSize * v->capacity);
return vretval;
return vretval;
default:
perror("specify a valid vector type\n");
exit(EXIT_FAILURE);