#include #include #include #include #include "linkedlist.h" int main() { //keys and values for first list double keyarr[4] = {1.,3.,5.,3.}; double valarr[4] = {2.,4.,6.,7.}; //values for second list, string keys can be passed directly to list_insert double val2arr[4] = {2.,4.,6.,7.}; //keys we search for in the first list double searcharr[10] = {1.,3.,5.,8.,3.}; //variable to store the results of our searches void *result; list_head_t *doublekeys = list_init(cmp_double); list_head_t *stringkeys = list_init(cmp_string); printf("%i : 0 if equal, Non-Zero if inequal\n",cmp_string("eecs","eecs")); printf("%i : 0 if equal, Non-Zero if inequal\n",cmp_string("eecs","EECS")); printf("%i : 0 if equal, Non-Zero if inequal\n",cmp_string("eecs","eec")); //printf("%i : 0 if equal, Non-Zero if inequal\n",cmp_string("eecs",NULL)); //insert (1,2),(3,4),(5,6) printf("%i\n",list_insert(doublekeys,keyarr,valarr)); printf("%i\n",list_insert(doublekeys,keyarr+1,valarr+1)); printf("%i\n",list_insert(doublekeys,keyarr+2,valarr+2)); //search 1 result = list_search(doublekeys,searcharr); result == NULL ? printf("Key not found\n") : printf("Value: %f\n",*(double *)result); result = NULL; //search 3 result = list_search(doublekeys,searcharr+1); result == NULL ? printf("Key not found\n") : printf("Value: %f\n",*(double *)result); result = NULL; //search 5 result = list_search(doublekeys,searcharr+2); result == NULL ? printf("Key not found\n") : printf("Value: %f\n",*(double *)result); //insert (3,7) printf("%i\n",list_insert(doublekeys,keyarr+3,valarr+3)); //search 8 result = list_search(doublekeys,searcharr+3); result == NULL ? printf("Key not found\n") : printf("Value: %f\n",*(double *)result); //search 3 result = list_search(doublekeys,searcharr+4); result == NULL ? printf("Key not found\n") : printf("Value: %f\n",*(double *)result); //insert ("one",2),("two",4),("three",6) printf("%i\n",list_insert(stringkeys,"one",val2arr)); printf("%i\n",list_insert(stringkeys,"two",val2arr+1)); printf("%i\n",list_insert(stringkeys,"three",val2arr+2)); //search "one" result = list_search(stringkeys,"one"); result == NULL ? printf("Key not found\n") : printf("Value: %f\n",*(double *)result); //search "three" result = list_search(stringkeys,"three"); result == NULL ? printf("Key not found\n") : printf("Value: %f\n",*(double *)result); //search "five" result = list_search(stringkeys,"five"); result == NULL ? printf("Key not found\n") : printf("Value: %f\n",*(double *)result); //insert ("three",7) printf("%i\n",list_insert(stringkeys,"three",val2arr+3)); //search "eight" result = list_search(stringkeys,"eight"); result == NULL ? printf("Key not found\n") : printf("Value: %f\n",*(double *)result); //search "three" result = list_search(stringkeys,"three"); result == NULL ? printf("Key not found\n") : printf("Value: %f\n",*(double *)result); list_delete(stringkeys); list_delete(doublekeys); } list_head_t *list_init(int (*compf)(const void *, const void *)) { list_head_t *head; head = NULL; head = (list_head_t *)malloc(sizeof(list_head_t)); if(head == NULL) { printf("Could not initialize list.\n"); return NULL; } head->next = NULL; head->compare = compf; return head; } // inserts elements at the beginning of the list int list_insert(list_head_t *head, void *key, void *value) { list_entry_t *node; node = NULL; node = (list_entry_t *)malloc(sizeof(list_entry_t)); if(node == NULL) { printf("Insertion Failed\n"); return -1; } node->key = (void *)key; node->value = (void *)value; node->next = head->next; head->next = node; if(head->next == NULL) { printf("Adding to list failed.\n"); return -1; } return 0; } void *list_search(list_head_t *head, void *key) { void *search; list_entry_t *node; search = key; node = head->next; while(node != NULL) { if(head->compare(node->key,search) == 0) { return node->value; } node = node->next; } return node; } void list_delete(list_head_t *head) { list_entry_t *node; list_entry_t *temp; node = head->next; temp = head->next; while(node) { node = node->next; free(temp); temp = node; } free(head); return; } int cmp_int(const void *a, const void *b) { assert((a != NULL)&&(b != NULL)); if(*(int *)a == *(int *)b) return 0; return 1; } int cmp_string(const void *a, const void *b) { assert((a != NULL)&&(b != NULL)); return strcmp((const char *)a, (const char *)b); } int cmp_double(const void *a, const void *b) { double *double1 = (double *)a; double *double2 = (double *)b; assert((double1 != NULL) && (double2 != NULL)); if(*double1 == *double2) { return 0; } return 1; }