Generic implementation of data structures in C.
For the moment we have implemented:
This API is open source and made by me, José Pereira and Henrique Faria.
- make
- ./main
src/ (contains .c files)
include/ (contains .h files)
void printPEOPLE(TAD_ARRAY_LIST people) {
int size = getArraySize(people);
for(int i=0; i<size; i++) {
TAD_PERSON p = (TAD_PERSON) getElem(people, i);
printPERSON(p);
}
}
void freePEOPLE(TAD_ARRAY_LIST people) {
int size = getArraySize(people);
for(int i=0; i<size; i++) {
TAD_PERSON p = (TAD_PERSON) getElem(people, i);
freePERSON(p);
}
free_ARRAY_LIST(people);
}
void ArrayListUsage() {
TAD_PERSON musk = PERSON(0, "Elon Musk", 34);
TAD_PERSON turing = PERSON(1, "Alon Turing", 41);
// Create ArrayList named people
TAD_ARRAY_LIST people = ARRAY_LIST(INITIAL_ARRAYLIST_DIM);
// add elements to ArrayList
addElem(people, musk);
addElem(people, turing);
// print all elements inside ArrayList
printPEOPLE(people);
// free all elements and ArrayList structure
freePEOPLE(people);
}
Because this implementation is generic we can not guess what data type is going to be stored in each data structure so it's user task to allocate and free memory created by him. The free() method for each data structure only frees the structure itself not the elements added by user.
This is demonstrated in API usage above if you notice the user had to implement printPEOPLE() and freePEOPLE() because it's user specific code.
The freePEOPLE() frees user created data one by one and after calls free() function from ArrayList structure.
To sum up you should (must) implement bellow interface for each data:
- print(StructureName)()
- free(StructureName)()
Any data type (int* , float*, char*, strucutres implemented by user, etc.) can be stored.
This is guaranteed because we use void* data type wich means any pointer can be stored.