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
|
+ Creating a list
|
||||||
+ Pushing into a list
|
+ Pushing into a list
|
||||||
+ Printing a list
|
+ Printing a list
|
||||||
|
+ Popping the last value of a list
|
||||||
+ Cleanup of a list
|
+ Cleanup of a list
|
||||||
|
|
||||||
#### TODO:
|
#### TODO:
|
||||||
@@ -46,7 +47,6 @@ I got bored with having to use complicated checking and stuff when working with
|
|||||||
+ Inserting at a given index
|
+ Inserting at a given index
|
||||||
+ Verify multi-dimensional lists working
|
+ Verify multi-dimensional lists working
|
||||||
+ Get/set at a given index
|
+ Get/set at a given index
|
||||||
+ Popping
|
|
||||||
+ Remove at a given index
|
+ Remove at a given index
|
||||||
+ Verify vectors working inside of lists
|
+ Verify vectors working inside of lists
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
#include <odweta/list.h>
|
#include <odweta/list.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
list_t *l = listInit();
|
list_t *l = listInit();
|
||||||
@@ -9,6 +10,27 @@ int main(int argc, char **argv) {
|
|||||||
|
|
||||||
listPrint(l);
|
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);
|
listCleanup(l);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
+6
-4
@@ -17,10 +17,12 @@ typedef struct {
|
|||||||
vector_t* data; // vector of vectors
|
vector_t* data; // vector of vectors
|
||||||
} list_t;
|
} 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
@@ -106,3 +106,15 @@ listCleanup(list_t* l) {
|
|||||||
vectorCleanup((vector_t *)l->data);
|
vectorCleanup((vector_t *)l->data);
|
||||||
free(l);
|
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
@@ -216,30 +216,27 @@ vectorPop(vector_t* v) {
|
|||||||
int *iretval = (int *)(v->data) + v->size-1;
|
int *iretval = (int *)(v->data) + v->size-1;
|
||||||
v->size -= 1;
|
v->size -= 1;
|
||||||
if (v->size <= v->capacity / 2) v->capacity /= 2;
|
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;
|
||||||
if (v->capacity == 0) v->capacity = 1;
|
return iretval;
|
||||||
return iretval;
|
|
||||||
case FLOAT:
|
case FLOAT:
|
||||||
float *fretval = (float *)(v->data) + v->size-1;
|
float *fretval = (float *)(v->data) + v->size-1;
|
||||||
v->size -= 1;
|
v->size -= 1;
|
||||||
if (v->size <= v->capacity / 2) v->capacity /= 2;
|
if (v->size <= v->capacity / 2) v->capacity /= 2;
|
||||||
if (v->capacity == 0) v->capacity = 1;
|
if (v->capacity == 0) v->capacity = 1;
|
||||||
v->data = realloc(v->data, v->elementSize * v->capacity);
|
return fretval;
|
||||||
return fretval;
|
|
||||||
case CHAR:
|
case CHAR:
|
||||||
char *cretval = (char *)(v->data) + v->size-1;
|
char *cretval = (char *)(v->data) + v->size-1;
|
||||||
v->size -= 1;
|
v->size -= 1;
|
||||||
if (v->size <= v->capacity / 2) v->capacity /= 2;
|
if (v->size <= v->capacity / 2) v->capacity /= 2;
|
||||||
if (v->capacity == 0) v->capacity = 1;
|
if (v->capacity == 0) v->capacity = 1;
|
||||||
v->data = realloc(v->data, v->elementSize * v->capacity);
|
return cretval;
|
||||||
return cretval;
|
|
||||||
case VECTOR:
|
case VECTOR:
|
||||||
vector_t *vretval = (vector_t *)(v->data) + v->size-1;
|
vector_t *vretval = (vector_t *)(v->data) + v->size-1;
|
||||||
v->size -= 1;
|
v->size -= 1;
|
||||||
if (v->size <= v->capacity / 2) v->capacity /= 2;
|
if (v->size <= v->capacity / 2) v->capacity /= 2;
|
||||||
if (v->capacity == 0) v->capacity = 1;
|
if (v->capacity == 0) v->capacity = 1;
|
||||||
v->data = realloc(v->data, v->elementSize * v->capacity);
|
|
||||||
return vretval;
|
return vretval;
|
||||||
default:
|
default:
|
||||||
perror("specify a valid vector type\n");
|
perror("specify a valid vector type\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
|
|||||||
Reference in New Issue
Block a user