made popping with lists work. yay! (also new demo to show the thing)
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -24,3 +24,5 @@ void listPush(list_t* l, dataType_e type, void* element); // appends an eleme
|
||||
void listPrint(list_t* l); // prints a list
|
||||
|
||||
void listCleanup(list_t* l); // freeing pointers
|
||||
|
||||
void* listPop(list_t* l); // popping the last value of the list and returning it
|
||||
|
||||
+12
@@ -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;
|
||||
}
|
||||
|
||||
+1
-4
@@ -216,7 +216,6 @@ 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;
|
||||
case FLOAT:
|
||||
@@ -224,21 +223,19 @@ vectorPop(vector_t* v) {
|
||||
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;
|
||||
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;
|
||||
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;
|
||||
default:
|
||||
perror("specify a valid vector type\n");
|
||||
|
||||
Reference in New Issue
Block a user