ccousas

estruturas de datos e algoritmos en C
Log | Files | Refs

main.c (1721B)


      1 #include <stdio.h>
      2 #include <string.h>
      3 #include <stdlib.h>
      4 #include "lista_nodos.h"
      5 
      6 
      7 struct TEST_STRUCT{
      8     int id;
      9     char* name;
     10 };
     11 
     12 struct TEST_STRUCT* init_test_struct(const char* name){
     13     struct TEST_STRUCT* test_struct = calloc(1, sizeof(struct TEST_STRUCT));
     14     test_struct->name = calloc(strlen(name)+1, sizeof(char) );
     15     strcpy(test_struct->name, name);
     16     test_struct->id = 1;
     17     return test_struct;
     18 }
     19 
     20 
     21 void test_free(void* x){
     22     struct TEST_STRUCT* test = (struct TEST_STRUCT*) x;
     23     free(test->name);
     24     free(test);
     25 }
     26 
     27 
     28 // 1: Son iguais, 0: Son diferentes
     29 int test_equals(void* x, void* y){
     30     if ( !strcmp(((struct TEST_STRUCT*) x)->name, ((struct TEST_STRUCT*) y)->name) ){
     31         if( ((struct TEST_STRUCT*) x)->id == ((struct TEST_STRUCT*) y)->id )
     32             return 1;
     33     }
     34     return 0;
     35 }
     36 
     37 void test_print(void* item, int index){
     38     printf("Elemento %d:  %s\n", index, ((struct TEST_STRUCT*)(item))->name );
     39 }
     40 
     41 
     42 int main(){
     43     NODE_LIST_T* list = init_node_list();
     44     struct TEST_STRUCT* item1 = init_test_struct("test1");
     45 
     46     printf("list size: %d\n", list->length);
     47 
     48     node_list_append(item1, list);
     49     node_list_append(init_test_struct("test2"), list);
     50     node_list_append(init_test_struct("test3"), list);
     51     node_list_append(init_test_struct("test4"), list);
     52     node_list_append(init_test_struct("test5"), list);
     53     node_list_append(init_test_struct("test6"), list);
     54 
     55     node_list_delete(2, list, test_free);
     56 
     57     node_list_print(list, test_print);
     58     printf("list size: %d\n", list->length);
     59 
     60     int indexf = node_list_find(list, init_test_struct("test6"), test_equals);
     61     printf("Index found: %d", indexf);
     62 
     63     node_list_free(list, test_free);
     64     return 0;
     65 
     66 }