Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef struct elem_t elem_t;
- typedef struct list_t list_t;
- struct elem_t {
- void * key;
- struct elem_t * next;
- };
- struct list_t{
- struct elem_t * head;
- int (*compare) (void*,void*);
- void * (*copyk) (void*);
- void (*print_list) (struct elem_t*);
- };
- int compare_int(void *a, void *b){
- int *a1, *b1;
- a1 = (int*) a;
- b1 = (int*) b;
- return ((*a1)-(*b1));
- }
- int compare_string(void *a, void *b){
- char *a1, *b1;
- a1 = (char*) a;
- b1 = (char*) b;
- return (strcmp(a1,b1));
- }
- void * copyk_int(void *a){
- int *a1;
- if((a1 = (int*) malloc(sizeof(int)))==NULL)
- return NULL;
- *a1= * (int*) a;
- return (void*) a1;
- }
- void * copyk_string(void *a){
- char* a1;
- int len;
- if(((char*)a)==NULL)
- return NULL;
- len=strlen((char*)a);
- if((a1 =(char*) malloc((len+1)*sizeof(char)))==NULL)
- return NULL;
- strcpy(a1,(char*)a);
- return (void*) a1;
- }
- void print_int_list(elem_t *list){
- elem_t *iter;
- if(list == NULL)
- printf("[WARNING @ print_int_list] empty list\n");
- else{
- iter = list;
- while(iter != NULL){
- printf("%d ->",(int) iter->key);
- iter = iter->next;
- }
- printf("# end of list\n");
- }
- }
- void print_string_list(elem_t *list){
- elem_t *iter;
- if(list == NULL)
- printf("[WARNING @ print_string_list] empty list\n");
- else{
- iter = list;
- while(iter != NULL){
- printf("%s ->",(char*) iter->key);
- iter = iter->next;
- }
- printf("# end of list\n");
- }
- }
- list_t* create_int_list(){
- list_t *new_list;
- if((new_list = (list_t*) malloc(sizeof(list_t)))==NULL){
- printf("[ERROR @ create_int_list] couldn't allocate heap\n");
- return NULL;
- }
- new_list->head = NULL;
- new_list->compare = compare_int;
- new_list->copyk = copyk_int;
- new_list->print_list = print_int_list;
- return new_list;
- }
- list_t* create_string_list(){
- list_t *new_list;
- if((new_list = (list_t*) malloc(sizeof(list_t)))==NULL){
- printf("[ERROR @ create_int_list] couldn't allocate heap\n");
- return NULL;
- }
- new_list->head = NULL;
- new_list->compare = compare_string;
- new_list->copyk = copyk_string;
- new_list->print_list = print_string_list;
- return new_list;
- }
- void printList(list_t *list){
- if(list == NULL)
- printf("[WARNING @ printList] empty list\n");
- else
- list->print_list(list->head);
- }
- void insert_inList(list_t* list, void * new_key){
- if(list->head == NULL){
- if((list->head = (elem_t*) malloc(sizeof(elem_t)))==NULL){
- printf("[ERROR @ insert_inList] couldn't allocate memory into the heap\n");
- exit(1);
- }
- list->head->key = list->copyk(new_key);
- list->head->next = NULL;
- }
- else{
- elem_t *curr = list->head, *succ = list->head->next, *new_element;
- if((list->compare(new_key,list->head->key))<0){
- if((new_element = (elem_t*) malloc((sizeof(elem_t))))==NULL){
- printf("[ERROR @ insert_inList] couldn't allocate memory into the heap\n");
- exit(1);
- }
- new_element->key = list->copyk(new_key);
- new_element->next = curr;
- list->head = new_element;
- }
- else{
- while((succ != NULL)&&((list->compare(new_key,succ->key))>=0)){
- curr = succ;
- succ = succ->next;
- }
- if((new_element = (elem_t*) malloc((sizeof(elem_t))))==NULL){
- printf("[ERROR @ insert_inList] couldn't allocate memory into the heap\n");
- exit(1);
- }
- new_element->key = list->copyk(new_key);
- new_element->next = succ;
- curr->next = new_element;
- }
- }
- }
- list_t* free_list(list_t* list){
- if(list->head != NULL){
- elem_t *curr = list->head, *succ = list->head->next;
- while(curr != NULL){
- free(curr->key);
- free(curr);
- curr = succ;
- if(succ != NULL) succ = succ->next;
- }
- }
- free(list);
- return NULL;
- }
- void extract_fromList(list_t *list, void *x_key){
- if(list == NULL)
- printf("[WARNING @ extract_fromList] empty list\n");
- else{
- if(list->head == NULL)
- printf("[WARNING @ extract_fromList] empty list\n");
- else{
- elem_t *curr = list->head, *succ = list->head->next;
- if(!(list->compare(x_key,list->head->key))){
- list->head = succ;
- free(curr->key);
- free(curr);
- }
- else{
- while((succ != NULL)&&((list->compare(x_key,succ->key))>0)){
- curr = succ;
- succ = succ->next;
- }
- if(succ == NULL)
- printf("[WARNING @ extract_fromList] element not found\n");
- else{
- if((list->compare(x_key,succ->key))<0)
- printf("[WARNING @ extract_fromList] element not found\n");
- else{
- curr->next = succ->next;
- free(succ->key);
- free(succ);
- }
- }
- }
- }
- }
- }
- int search_inList(list_t *list, void *x_key){
- if(list == NULL){
- printf("[WARNING @ search_inList] empty list\n");
- return 0;
- }
- else{
- if(list->head == NULL){
- printf("[WARNING @ search_inList] empty list\n");
- return 0;
- }
- else{
- elem_t *iter = list->head;
- int flag = 0;
- while((iter != NULL) && ((list->compare(x_key,iter->key))>=0) && (!flag)){
- if(!(list->compare(x_key,iter->key)))
- flag = 1;
- iter = iter->next;
- }
- return flag;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement