main.c (1614B)
1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #include "lista_dinamica.h" 5 6 7 struct TEST_STRUCT{ 8 int id; 9 char* name; 10 }; 11 12 13 14 // Funcion para inicializar o struct test (constructor) 15 struct TEST_STRUCT* init_test_struct(const char* name){ 16 struct TEST_STRUCT* test_struct = calloc(1, sizeof(struct TEST_STRUCT)); 17 test_struct->name = calloc(strlen(name)+1, sizeof(char) ); 18 strcpy(test_struct->name, name); 19 test_struct->id = 1; 20 return test_struct; 21 } 22 23 void test_free(void* x){ 24 struct TEST_STRUCT* test = (struct TEST_STRUCT*) x; 25 free(test->name); 26 free(test); 27 } 28 29 // 1: Son iguais, 0: Son diferentes 30 int test_equals(void* x, void* y){ 31 if ( !strcmp(((struct TEST_STRUCT*) x)->name, ((struct TEST_STRUCT*) y)->name) ){ 32 if( ((struct TEST_STRUCT*) x)->id == ((struct TEST_STRUCT*) y)->id ) 33 return 1; 34 } 35 return 0; 36 } 37 38 int main(){ 39 dynamic_list_T* list = init_dynamic_list(sizeof(struct TEST_STRUCT*)); 40 41 //struct TEST_STRUCT* un = init_test_struct("test1"); 42 43 dynamic_list_append(list, init_test_struct("test1")); 44 dynamic_list_append(list, init_test_struct("test2")); 45 dynamic_list_append(list, init_test_struct("test3")); 46 47 //dynamic_list_delete(list, 0); 48 //dynamic_list_remove(list, un, test_free); 49 50 int index = dynamic_list_find(list, init_test_struct("test3"), test_equals); 51 printf("Posicion a borrar: %d\n", index); 52 53 54 for (int i = 0; i < list->length; i++){ 55 printf("Elemento: %s\n", ((struct TEST_STRUCT*)(list->items[i]))->name); 56 } 57 58 dynamic_list_free(list, test_free); 59 60 return 0; 61 }