Advertisement
DanFloyd

VoidList - file .c

Mar 15th, 2014
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.03 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef struct elem_t elem_t;
  6. typedef struct list_t list_t;
  7.  
  8. struct elem_t {
  9.     void * key;
  10.     struct elem_t * next;
  11. };
  12.  
  13. struct list_t{
  14.     struct elem_t * head;
  15.     int (*compare) (void*,void*);
  16.     void * (*copyk) (void*);
  17.     void (*print_list) (struct elem_t*);
  18. };
  19.  
  20. int compare_int(void *a, void *b){
  21.     int *a1, *b1;
  22.     a1 = (int*) a;
  23.     b1 = (int*) b;
  24.     return ((*a1)-(*b1));
  25. }
  26.  
  27. int compare_string(void *a, void *b){
  28.     char *a1, *b1;
  29.     a1 = (char*) a;
  30.     b1 = (char*) b;
  31.     return (strcmp(a1,b1));
  32. }
  33.  
  34. void * copyk_int(void *a){
  35.     int *a1;
  36.     if((a1 =  (int*) malloc(sizeof(int)))==NULL)
  37.         return NULL;
  38.     *a1= * (int*) a;
  39.     return (void*) a1;
  40. }
  41.  
  42. void * copyk_string(void *a){
  43.     char* a1;
  44.     int len;
  45.     if(((char*)a)==NULL)
  46.         return NULL;
  47.     len=strlen((char*)a);
  48.     if((a1 =(char*) malloc((len+1)*sizeof(char)))==NULL)
  49.         return NULL;
  50.     strcpy(a1,(char*)a);
  51.     return (void*) a1;
  52. }
  53.  
  54. void print_int_list(elem_t *list){
  55.     elem_t *iter;
  56.     if(list == NULL)
  57.         printf("[WARNING @ print_int_list] empty list\n");
  58.     else{
  59.         iter = list;
  60.         while(iter != NULL){
  61.             printf("%d ->",(int) iter->key);
  62.             iter = iter->next;
  63.         }
  64.         printf("# end of list\n");
  65.     }
  66. }
  67.  
  68. void print_string_list(elem_t *list){
  69.     elem_t *iter;
  70.     if(list == NULL)
  71.         printf("[WARNING @ print_string_list] empty list\n");
  72.     else{
  73.        
  74.         iter = list;
  75.         while(iter != NULL){
  76.             printf("%s ->",(char*) iter->key);
  77.             iter = iter->next;
  78.         }
  79.         printf("# end of list\n");
  80.     }
  81. }
  82.  
  83. list_t* create_int_list(){
  84.     list_t *new_list;
  85.     if((new_list = (list_t*) malloc(sizeof(list_t)))==NULL){
  86.         printf("[ERROR @ create_int_list] couldn't allocate heap\n");
  87.         return NULL;
  88.     }
  89.     new_list->head = NULL;
  90.     new_list->compare = compare_int;
  91.     new_list->copyk = copyk_int;
  92.     new_list->print_list = print_int_list;
  93.     return new_list;
  94. }
  95.  
  96. list_t* create_string_list(){
  97.     list_t *new_list;
  98.     if((new_list = (list_t*) malloc(sizeof(list_t)))==NULL){
  99.         printf("[ERROR @ create_int_list] couldn't allocate heap\n");
  100.         return NULL;
  101.     }
  102.     new_list->head = NULL;
  103.     new_list->compare = compare_string;
  104.     new_list->copyk = copyk_string;
  105.     new_list->print_list = print_string_list;
  106.     return new_list;
  107. }
  108.  
  109. void printList(list_t *list){
  110.     if(list == NULL)
  111.         printf("[WARNING @ printList] empty list\n");
  112.     else
  113.         list->print_list(list->head);
  114. }
  115.  
  116. void insert_inList(list_t* list, void * new_key){
  117.     if(list->head == NULL){
  118.         if((list->head = (elem_t*) malloc(sizeof(elem_t)))==NULL){
  119.             printf("[ERROR @ insert_inList] couldn't allocate memory into the heap\n");
  120.             exit(1);
  121.         }
  122.         list->head->key = list->copyk(new_key);
  123.         list->head->next = NULL;
  124.     }
  125.     else{
  126.         elem_t *curr = list->head, *succ = list->head->next, *new_element;
  127.         if((list->compare(new_key,list->head->key))<0){
  128.             if((new_element = (elem_t*) malloc((sizeof(elem_t))))==NULL){
  129.                 printf("[ERROR @ insert_inList] couldn't allocate memory into the heap\n");
  130.                 exit(1);
  131.             }
  132.             new_element->key = list->copyk(new_key);
  133.             new_element->next = curr;
  134.             list->head = new_element;
  135.         }
  136.         else{
  137.             while((succ != NULL)&&((list->compare(new_key,succ->key))>=0)){
  138.                 curr = succ;
  139.                 succ = succ->next;
  140.             }
  141.             if((new_element = (elem_t*) malloc((sizeof(elem_t))))==NULL){
  142.                 printf("[ERROR @ insert_inList] couldn't allocate memory into the heap\n");
  143.                 exit(1);
  144.             }
  145.             new_element->key = list->copyk(new_key);
  146.             new_element->next = succ;
  147.             curr->next = new_element;
  148.         }
  149.     }      
  150. }
  151.  
  152. list_t* free_list(list_t* list){
  153.     if(list->head != NULL){
  154.         elem_t *curr = list->head, *succ = list->head->next;
  155.         while(curr != NULL){
  156.             free(curr->key);
  157.             free(curr);
  158.             curr = succ;
  159.             if(succ != NULL) succ = succ->next;
  160.         }
  161.     }
  162.     free(list);
  163.     return NULL;
  164. }
  165.  
  166. void extract_fromList(list_t *list, void *x_key){
  167.     if(list == NULL)
  168.         printf("[WARNING @ extract_fromList] empty list\n");
  169.     else{
  170.         if(list->head == NULL)
  171.             printf("[WARNING @ extract_fromList] empty list\n");
  172.         else{
  173.             elem_t *curr = list->head, *succ = list->head->next;
  174.             if(!(list->compare(x_key,list->head->key))){
  175.                 list->head = succ;
  176.                 free(curr->key);
  177.                 free(curr);
  178.             }
  179.             else{
  180.                 while((succ != NULL)&&((list->compare(x_key,succ->key))>0)){
  181.                     curr = succ;
  182.                     succ = succ->next;
  183.                 }
  184.                 if(succ == NULL)
  185.                     printf("[WARNING @ extract_fromList] element not found\n");
  186.                 else{
  187.                     if((list->compare(x_key,succ->key))<0)
  188.                         printf("[WARNING @ extract_fromList] element not found\n");
  189.                     else{
  190.                         curr->next = succ->next;
  191.                         free(succ->key);
  192.                         free(succ);        
  193.                     }
  194.                 }
  195.             }
  196.         }
  197.     }
  198. }
  199.  
  200. int search_inList(list_t *list, void *x_key){
  201.     if(list == NULL){
  202.         printf("[WARNING @ search_inList] empty list\n");
  203.         return 0;
  204.     }
  205.     else{
  206.         if(list->head == NULL){
  207.             printf("[WARNING @ search_inList] empty list\n");
  208.             return 0;
  209.         }
  210.         else{
  211.             elem_t *iter = list->head;
  212.             int flag = 0;
  213.             while((iter != NULL) && ((list->compare(x_key,iter->key))>=0) && (!flag)){
  214.                 if(!(list->compare(x_key,iter->key)))
  215.                     flag = 1;
  216.                 iter = iter->next;         
  217.             }
  218.             return flag;
  219.         }
  220.     }
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement