/* * listad.c * * Created on: 18-09-2008 * Author: crosvera */ #include #include #include #include "./listad.h" /* Implementation of the functions */ /* * The 'listInit' function will initialize the Linked-List. */ LIST *listInit(void){ LIST *list = NULL; list = (LIST *) malloc( sizeof(LIST) ); if(list != NULL){ /* Initialize the attributes */ list->length = 0; list->my_first = NULL; list->my_last = NULL; /* Initialize the pointers to functions, become * * them to methods in the LIST */ list->is_empty = is_empty; list->append = append; list->insert_first = insert_first; list->drop = drop; list->show = show; list->count = count; list->delete = delete; list->delete_first = delete_first; list->delete_last = delete_last; list->list2array = list2array; return list; /* It will return a list ready to use */ }else return list; /* It will return NULL */ } /* * The 'is_empty' function returns true if LIST is empty, * and returns false if isn't. */ bool is_empty(LIST *list){ if( (list->my_first == NULL) || (list->length == 0) ) return true; else return false; } /* * The 'drop' function will erase all the elements in the * Linked-List and it will returns true if this task was * successful. */ void drop(LIST *list){ if( !is_empty(list) ){ _node *tmp = NULL; _node *tmp_next = NULL; tmp = list->my_first; while(tmp != NULL){ tmp_next = tmp->next; free(tmp); tmp = tmp_next; } free(tmp); free(tmp_next); list->my_first = NULL; list->my_last = NULL; list->length = 0; } } /* * The 'count' function will return the number of * occurrences of ELEMENT. If returns 0, means that * ELEMENT doesn't exist in the list. */ int count(LIST *list, _list_DT element){ if( !is_empty(list) ){ _node *tmp; int counter = 0; for(tmp = list->my_first; (tmp->next != NULL) && (tmp != NULL); tmp = tmp->next){ if(tmp->value == element) counter += 1; } return counter; }else return 0; } /* * The 'insert' function inserts ELEMENT to the LIST, * if the list isn't empty ELEMET takes the last place. */ void append(LIST *list, _list_DT element){ if( is_empty(list) ){ _node *tmp = NULL; tmp = (_node *) malloc( sizeof(_node) ); tmp->value = element; tmp->next = NULL; list->my_first = tmp; list->my_last = list->my_first; list->length += 1; }else{ insert_last(list, element); } } /* * The (private) 'insert_last' function will * puts ELEMENT into the list in the last place. */ void insert_last(LIST *list, _list_DT element){ if( !is_empty(list) ){ _node *tmp = NULL; tmp = (_node *) malloc( sizeof(_node) ); tmp->value = element; tmp->next = NULL; list->my_last->next = tmp; list->my_last = list->my_last->next; list->length += 1; }else append(list, element); } /* * The 'insert_first' function will insert * ELEMENT in the first place. */ void insert_first(LIST *list, _list_DT element){ if( !is_empty(list) ){ _node *tmp = NULL; tmp = (_node *) malloc( sizeof(_node) ); tmp->value = element; tmp->next = list->my_first; list->my_first = tmp; list->length += 1; }else append(list, element); } /* * The 'delete' function will delete ELEMEN * from the LIST, if the Linked-List is empty * or if the ELEMENT doesn't exist the * function do nothing. */ void delete(LIST *list, _list_DT element){ if( !is_empty(list) && (count(list, element) > 0) ){ _node *tmp_before = NULL; _node *tmp = NULL; _node *tmp_after = NULL; if(list->my_first->value == element) delete_first(list); else{ if(list->my_last->value == element) delete_last(list); else{ tmp_before = list->my_first; while( tmp_before->next->value != element ) tmp_before = tmp->next; tmp = tmp_before->next; tmp_after = tmp_before->next->next; tmp_before->next = tmp_after; list->length -= 1; free(tmp); } } } } /* * The 'delete_first' function will delete the * first element in LIST. */ void delete_first(LIST *list){ if( !is_empty(list) ){ _node *tmp = NULL; tmp = list->my_first->next; free(list->my_first); list->my_first = tmp; list->length -= 1; } } /* * The 'delete_last' function will delete * the last element in LIST. */ void delete_last(LIST *list){ if( !is_empty(list) ){ _node *tmp = NULL; tmp = list->my_first; while(tmp->next != NULL) tmp = tmp->next; free(list->my_last); list->my_last = tmp; list->length -= 1; } } /* * The 'show' function will print all the elements * in LIST. */ void show(LIST *list){ if( !is_empty(list) ){ _node *tmp = NULL; printf("\n["); for(tmp = list->my_first; tmp != NULL; tmp = tmp->next){ if(tmp->next == NULL) printf("'%d'", tmp->value); else printf(" '%d',", tmp->value); } printf("]\n"); }else printf("\n[]\n"); } /* * The 'list2array' function will returns a pointer which contains * an array with the elements of LIST. */ _list_DT *list2array(LIST *list){ _list_DT *array = NULL; _node *tmp = NULL; int dimension = 0; for(tmp = list->my_first; tmp != NULL; tmp = tmp->next){ dimension++; array = (_list_DT *) realloc(array, sizeof(_list_DT) * dimension); array[dimension - 1] = tmp->value; if(tmp->next == NULL){ array = (_list_DT *) realloc(array, sizeof(_list_DT) * (dimension + 1)); array[dimension] = '\n'; } } return array; }