diff --git a/README.md b/README.md index 960e726..fb6df59 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/demo.c b/demo.c index 905112e..d091de5 100644 --- a/demo.c +++ b/demo.c @@ -1,4 +1,5 @@ #include +#include 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; diff --git a/include/list.h b/include/list.h index efa7897..b4f9674 100644 --- a/include/list.h +++ b/include/list.h @@ -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 diff --git a/src/list.c b/src/list.c index 5fc1e85..30d1168 100644 --- a/src/list.c +++ b/src/list.c @@ -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; +} diff --git a/src/vector.c b/src/vector.c index 3504845..1fe87f8 100644 --- a/src/vector.c +++ b/src/vector.c @@ -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);