├── .gitignore ├── README.pdf ├── tests ├── ints.txt ├── inttest.c ├── stringtest.c └── test ├── src ├── test.h ├── test.c ├── main.c_bak ├── sorted-list.h ├── sorted-list.c └── main.c └── Makefile /.gitignore: -------------------------------------------------------------------------------- 1 | sorted-list 2 | -------------------------------------------------------------------------------- /README.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/i/SORTDIS/master/README.pdf -------------------------------------------------------------------------------- /tests/ints.txt: -------------------------------------------------------------------------------- 1 | 39 2 | 1 3 | -12 4 | 1309 5 | 393 6 | 2 7 | 8 8 | -------------------------------------------------------------------------------- /src/test.h: -------------------------------------------------------------------------------- 1 | #ifndef TEST_H 2 | #define TEST_H 3 | 4 | int intTest1(); 5 | int intTest2(); 6 | int intTest3(); 7 | 8 | #endif 9 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | CFLAGS = -Wall -g 3 | CFILES = src/sorted-list.c src/main.c -c 4 | 5 | all: main archive 6 | $(CC) sorted-list.o main.o -o sl 7 | rm *.o 8 | 9 | main: 10 | $(CC) $(CFILES) $(CFLAGS) 11 | 12 | archive: main 13 | ar -r libsl.a sorted-list.o 14 | 15 | clean: 16 | rm *.o sl libsl.a 17 | -------------------------------------------------------------------------------- /src/test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "sorted-list.h" 4 | #include "test.h" 5 | 6 | int intTest1() { 7 | SortedListPtr list = SLCreate(compareInts); 8 | 9 | int a = 12; 10 | int b = 1; 11 | int c = 31; 12 | int d = 15; 13 | int e = 83; 14 | int f = -138; 15 | int g = 1; 16 | 17 | 18 | SLInsert(list, &a); 19 | SLInsert(list, &b); 20 | SLInsert(list, &c); 21 | SLInsert(list, &d); 22 | SLInsert(list, &e); 23 | SLInsert(list, &f); 24 | SLInsert(list, &g); 25 | printList(list); 26 | 27 | 28 | printf("\n\nremoving %d\n", d); 29 | SLRemove(list, &d); 30 | printList(list); 31 | printf("removing %d\n", e); 32 | SLRemove(list, &e); 33 | printList(list); 34 | printf("removing %d\n", b); 35 | SLRemove(list, &b); 36 | printList(list); 37 | printf("removing %d\n", b); 38 | SLRemove(list, &b); 39 | printList(list); 40 | printf("removing %d\n", b); 41 | SLRemove(list, &b); 42 | printList(list); 43 | 44 | SLDestroy(list); 45 | 46 | } 47 | 48 | int intTest2() { 49 | 50 | return 1; 51 | } 52 | 53 | int intTest3() { 54 | 55 | return 1; 56 | } 57 | 58 | int strTest1() { 59 | 60 | return 1; 61 | } 62 | 63 | int strTest2() { 64 | 65 | return 1; 66 | } 67 | 68 | int strTest3() { 69 | 70 | return 1; 71 | } 72 | -------------------------------------------------------------------------------- /tests/inttest.c: -------------------------------------------------------------------------------- 1 | /* 2 | * * sorted-list.c 3 | * */ 4 | 5 | #include 6 | #include 7 | 8 | #include "sorted-list.h" 9 | #include "test.h" 10 | 11 | int compareInts(void *p1, void *p2) 12 | { 13 | int i1 = *(int*)p1; 14 | int i2 = *(int*)p2; 15 | 16 | return i1 - i2; 17 | } 18 | 19 | int compareDoubles(void *p1, void *p2) 20 | { 21 | double d1 = *(double*)p1; 22 | double d2 = *(double*)p2; 23 | 24 | return (d1 < d2) ? -1 : ((d1 > d2) ? 1 : 0); 25 | } 26 | 27 | int compareStrings(void *p1, void *p2) 28 | { 29 | char *s1 = p1; 30 | char *s2 = p2; 31 | 32 | return strcmp(s1, s2); 33 | } 34 | 35 | int main(int argc, char **argv) { 36 | int i7,i5,i3,i4; 37 | /* double d1, d2, d3, d4, d5; */ 38 | /* char *s1, *s2, *s3, *s4; */ 39 | 40 | SortedListPtr sl; 41 | SortedListIteratorPtr slip; 42 | 43 | sl = SLCreate(compareInts); 44 | 45 | i7 = 7; 46 | i5 = 5; 47 | i3 = 3; 48 | i4 = 4; 49 | 50 | SLInsert(sl, &i7); 51 | SLInsert(sl, &i5); 52 | SLInsert(sl, &i3); 53 | 54 | slip = SLCreateIterator(sl); 55 | 56 | printf("%i\n", *(int*)SLNextItem(slip)); 57 | printf("%i\n", *(int*)SLNextItem(slip)); 58 | SLInsert(sl, &i4); 59 | printf("%i\n", *(int*)SLNextItem(slip)); 60 | 61 | SLDestroyIterator(slip); 62 | SLDestroy(sl); 63 | 64 | return 0; 65 | } 66 | -------------------------------------------------------------------------------- /src/main.c_bak: -------------------------------------------------------------------------------- 1 | /* 2 | * * sorted-list.c 3 | * */ 4 | 5 | #include 6 | #include 7 | 8 | #include "sorted-list.h" 9 | 10 | int compareInts(void *p1, void *p2) 11 | { 12 | int i1 = *(int*)p1; 13 | int i2 = *(int*)p2; 14 | 15 | return i1 - i2; 16 | } 17 | 18 | int compareDoubles(void *p1, void *p2) 19 | { 20 | double d1 = *(double*)p1; 21 | double d2 = *(double*)p2; 22 | 23 | return (d1 < d2) ? -1 : ((d1 > d2) ? 1 : 0); 24 | } 25 | 26 | int compareStrings(void *p1, void *p2) 27 | { 28 | char *s1 = p1; 29 | char *s2 = p2; 30 | 31 | return strcmp(s1, s2); 32 | } 33 | 34 | int main(int argc, char **argv) { 35 | /* int i7,i5,i3,i4; */ 36 | /* double d1, d2, d3, d4, d5; */ 37 | char *s1, *s2, *s3, *s4, *s5; 38 | 39 | SortedListPtr sl; 40 | SortedListIteratorPtr slip; 41 | 42 | sl = SLCreate(compareInts); 43 | 44 | s1 = "Hello"; 45 | s2 = "World"; 46 | s3 = "Dawgs"; 47 | s4 = "Tacos"; 48 | s5 = "Zebra"; 49 | 50 | SLInsert(sl, s1); // => ['HELLO'] 51 | printListString(sl); 52 | SLInsert(sl, s2); // => ['WORLD', 'HELLO'] 53 | printListString(sl); 54 | SLInsert(sl, s3); // => ['WORLD', 'HELLO', 'DOGS'] 55 | printListString(sl); 56 | SLInsert(sl, s4); // => ['WORLD', 'HELLO', 'DOGS'] 57 | printListString(sl); 58 | SLInsert(sl, s5); // => ['ZZZ', 'WORLD', 'HELLO', 'DOGS'] 59 | printListString(sl); 60 | SLRemove(sl, s2); // => ['ZZZ', 'HELLO', 'DOGS'] 61 | printListString(sl); 62 | 63 | slip = SLCreateIterator(sl); 64 | 65 | printf("------------------------------\n"); 66 | 67 | printf("%s\n", (char*)SLNextItem(slip)); 68 | SLRemove(sl, s1); 69 | printf("%s\n", (char*)SLNextItem(slip)); 70 | printListString(sl); 71 | printf("%s\n", (char*)SLNextItem(slip)); 72 | 73 | SLDestroyIterator(slip); 74 | SLDestroy(sl); 75 | 76 | return 0; 77 | } 78 | -------------------------------------------------------------------------------- /tests/stringtest.c: -------------------------------------------------------------------------------- 1 | /* 2 | * * sorted-list.c 3 | * */ 4 | 5 | #include 6 | #include 7 | 8 | #include "sorted-list.h" 9 | #include "test.h" 10 | 11 | int compareInts(void *p1, void *p2) 12 | { 13 | int i1 = *(int*)p1; 14 | int i2 = *(int*)p2; 15 | 16 | return i1 - i2; 17 | } 18 | 19 | int compareDoubles(void *p1, void *p2) 20 | { 21 | double d1 = *(double*)p1; 22 | double d2 = *(double*)p2; 23 | 24 | return (d1 < d2) ? -1 : ((d1 > d2) ? 1 : 0); 25 | } 26 | 27 | int compareStrings(void *p1, void *p2) 28 | { 29 | char *s1 = p1; 30 | char *s2 = p2; 31 | 32 | return strcmp(s1, s2); 33 | } 34 | 35 | int main(int argc, char **argv) { 36 | /* int i7,i5,i3,i4; */ 37 | /* double d1, d2, d3, d4, d5; */ 38 | char *s1, *s2, *s3, *s4, *s5; 39 | 40 | SortedListPtr sl; 41 | SortedListIteratorPtr slip; 42 | 43 | sl = SLCreate(compareInts); 44 | 45 | s1 = "Hello"; 46 | s2 = "World"; 47 | s3 = "Dawgs"; 48 | s4 = "Tacos"; 49 | s5 = "Zebra"; 50 | 51 | SLInsert(sl, s1); // => ['HELLO'] 52 | printListString(sl); 53 | SLInsert(sl, s2); // => ['WORLD', 'HELLO'] 54 | printListString(sl); 55 | SLInsert(sl, s3); // => ['WORLD', 'HELLO', 'DOGS'] 56 | printListString(sl); 57 | SLInsert(sl, s4); // => ['WORLD', 'HELLO', 'DOGS'] 58 | printListString(sl); 59 | SLInsert(sl, s5); // => ['ZZZ', 'WORLD', 'HELLO', 'DOGS'] 60 | printListString(sl); 61 | SLRemove(sl, s2); // => ['ZZZ', 'HELLO', 'DOGS'] 62 | printListString(sl); 63 | 64 | slip = SLCreateIterator(sl); 65 | 66 | printf("------------------------------\n"); 67 | 68 | printf("%s\n", (char*)SLNextItem(slip)); 69 | SLRemove(sl, s1); 70 | printf("%s\n", (char*)SLNextItem(slip)); 71 | printListString(sl); 72 | printf("%s\n", (char*)SLNextItem(slip)); 73 | 74 | SLDestroyIterator(slip); 75 | SLDestroy(sl); 76 | 77 | return 0; 78 | } 79 | -------------------------------------------------------------------------------- /tests/test: -------------------------------------------------------------------------------- 1 | // Using strings 2 | char *s1, *s2, *s3, *s4, *s5; 3 | s1 = "Hello"; 4 | s2 = "World"; 5 | s3 = "Dogs"; 6 | s4 = "Tacos"; 7 | s5 = "Zebra"; 8 | 9 | SLInsert(sl, s1); // => ['HELLO'] 10 | printListString(sl); 11 | SLInsert(sl, s2); // => ['WORLD', 'HELLO'] 12 | printListString(sl); 13 | SLInsert(sl, s3); // => ['WORLD', 'HELLO', 'DOGS'] 14 | printListString(sl); 15 | SLInsert(sl, s4); // => ['WORLD', 'HELLO', 'DOGS'] 16 | printListString(sl); 17 | SLInsert(sl, s5); // => ['Zebra', 'WORLD', 'HELLO', 'DOGS'] 18 | printListString(sl); 19 | SLRemove(sl, s2); // => ['Zebra', 'HELLO', 'DOGS'] 20 | printListString(sl); 21 | 22 | slip = SLCreateIterator(sl); 23 | 24 | printf("------------------------------\n"); 25 | 26 | printf("%s\n", (char*)SLNextItem(slip)); // => Zebra 27 | SLRemove(sl, s1); 28 | printf("%s\n", (char*)SLNextItem(slip)); // => Tacos 29 | printf("%s\n", (char*)SLNextItem(slip)); // => Dogs 30 | 31 | SLDestroyIterator(slip); 32 | SLDestroy(sl); 33 | 34 | 35 | // Using ints 36 | int s1, s2, s3, s4, s5; 37 | s1 = 1; 38 | s2 = 2; 39 | s3 = 3; 40 | s4 = 4; 41 | s5 = 5; 42 | 43 | SLInsert(sl, &s4); // => [4] 44 | SLInsert(sl, &s3); // => [4, 3] 45 | SLInsert(sl, &s5); // => [5, 4, 3] 46 | SLInsert(sl, &s2); // => [5, 4, 3, 2] 47 | SLInsert(sl, &s1); // => [5, 4, 3, 2, 1] 48 | SLRemove(sl, &s4); // => [5, 3, 2, 1] 49 | 50 | slip = SLCreateIterator(sl); 51 | 52 | printf("------------------------------\n"); 53 | 54 | printf("%i\n", *(int*)SLNextItem(slip)); // => 5 55 | SLRemove(sl, s1); 56 | printf("%i\n", *(int*)SLNextItem(slip)); // => 3 57 | printf("%i\n", *(int*)SLNextItem(slip)); // 2 58 | 59 | SLDestroyIterator(slip); 60 | SLDestroy(sl); 61 | 62 | // Special case with removing iter 63 | List[7, 5, 3] 64 | SLNextItem(sl); // => 7 65 | SLRemove(sl, &i5); 66 | SLNextItem(sl); // => 3 67 | 68 | // Special case with removing iter 69 | List[7, 5, 3] 70 | SLNextItem(sl); // => 7 71 | SLRemove(sl, &i5); 72 | SLInsert(sl, &i4); 73 | SLNextItem(sl); // => 4 74 | SLNextItem(sl); // => 3 75 | -------------------------------------------------------------------------------- /src/sorted-list.h: -------------------------------------------------------------------------------- 1 | #ifndef SORTED_LIST_H 2 | #define SORTED_LIST_H 3 | /* 4 | * sorted-list.h 5 | */ 6 | 7 | #include 8 | 9 | 10 | struct node_ { 11 | struct node_ *next; 12 | void *val; 13 | }; 14 | 15 | typedef struct node_* node; 16 | 17 | node create_node(void *); 18 | 19 | /* 20 | * When your sorted list is used to store objects of some type, since the 21 | * type is opaque to you, you will need a comparator function to order 22 | * the objects in your sorted list. 23 | * 24 | * You can expect a comparator function to return -1 if the 1st object is 25 | * smaller, 0 if the two objects are equal, and 1 if the 2nd object is 26 | * smaller. 27 | * 28 | * Note that you are not expected to implement any comparator functions. 29 | * You will be given a comparator function when a new sorted list is 30 | * created. 31 | */ 32 | 33 | typedef int (*CompareFuncT)(void *, void *); 34 | 35 | /* 36 | * Sorted list type. You need to fill in the type as part of your 37 | * implementation. 38 | */ 39 | struct SortedList 40 | { 41 | int (*cmp)(void *, void*); 42 | node ll; 43 | node iters; 44 | }; 45 | 46 | typedef struct SortedList* SortedListPtr; 47 | 48 | 49 | /* 50 | * Iterator type for user to "walk" through the list item by item, from 51 | * beginning to end. You need to fill in the type as part of your implementation. 52 | */ 53 | struct SortedListIterator 54 | { 55 | SortedListPtr sl; 56 | node curr; 57 | void *last_val_returned; 58 | }; 59 | 60 | typedef struct SortedListIterator* SortedListIteratorPtr; 61 | 62 | 63 | 64 | /* 65 | * SLCreate creates a new, empty sorted list. The caller must provide 66 | * a comparator function that can be used to order objects that will be 67 | * kept in the list. 68 | * 69 | * If the function succeeds, it returns a (non-NULL) SortedListT object. 70 | * Else, it returns NULL. 71 | * 72 | * You need to fill in this function as part of your implementation. 73 | */ 74 | 75 | SortedListPtr SLCreate(CompareFuncT cf); 76 | 77 | /* 78 | * SLDestroy destroys a list, freeing all dynamically allocated memory. 79 | * 80 | * You need to fill in this function as part of your implementation. 81 | */ 82 | void SLDestroy(SortedListPtr list); 83 | 84 | 85 | /* 86 | * SLInsert inserts a given object into a sorted list, maintaining sorted 87 | * order of all objects in the list. If the new object is equal to a subset 88 | * of existing objects in the list, then the subset can be kept in any 89 | * order. 90 | * 91 | * If the function succeeds, it returns 1. Else, it returns 0. 92 | * 93 | * You need to fill in this function as part of your implementation. 94 | */ 95 | 96 | int SLInsert(SortedListPtr list, void *newObj); 97 | 98 | 99 | /* 100 | * SLRemove removes a given object from a sorted list. Sorted ordering 101 | * should be maintained. 102 | * 103 | * If the function succeeds, it returns 1. Else, it returns 0. 104 | * 105 | * You need to fill in this function as part of your implementation. 106 | */ 107 | 108 | int SLRemove(SortedListPtr list, void *newObj); 109 | 110 | 111 | /* * SLCreateIterator creates an iterator object that will allow the caller 112 | * to "walk" through the list from beginning to the end using SLNextItem. 113 | * 114 | * If the function succeeds, it returns a non-NULL SortedListIterT object. 115 | * Else, it returns NULL. 116 | * 117 | * You need to fill in this function as part of your implementation. 118 | */ 119 | 120 | SortedListIteratorPtr SLCreateIterator(SortedListPtr list); 121 | 122 | 123 | /* 124 | * SLDestroyIterator destroys an iterator object that was created using 125 | * SLCreateIterator(). Note that this function should destroy the 126 | * iterator but should NOT affectt the original list used to create 127 | * the iterator in any way. 128 | * 129 | * You need to fill in this function as part of your implementation. 130 | */ 131 | 132 | void SLDestroyIterator(SortedListIteratorPtr iter); 133 | 134 | 135 | /* 136 | * SLNextItem returns the next object in the list encapsulated by the 137 | * given iterator. It should return a NULL when the end of the list 138 | * has been reached. 139 | * 140 | * One complication you MUST consider/address is what happens if a 141 | * sorted list encapsulated within an iterator is modified while that 142 | * iterator is active. For example, what if an iterator is "pointing" 143 | * to some object in the list as the next one to be returned but that 144 | * object is removed from the list using SLRemove() before SLNextItem() 145 | * is called. 146 | * 147 | * You need to fill in this function as part of your implementation. 148 | */ 149 | 150 | void *SLNextItem(SortedListIteratorPtr iter); 151 | 152 | void printList(SortedListPtr); 153 | void printListString(SortedListPtr); 154 | 155 | #endif 156 | -------------------------------------------------------------------------------- /src/sorted-list.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "sorted-list.h" 5 | 6 | node create_node(void *v) { 7 | node n = malloc(sizeof(struct node_)); 8 | n->next = NULL; 9 | n->val = v; 10 | 11 | return n; 12 | } 13 | 14 | void destroy_node(node n) { 15 | free(n); 16 | } 17 | 18 | SortedListPtr SLCreate(CompareFuncT cf) { 19 | SortedListPtr lp = malloc(sizeof(struct SortedList)); 20 | 21 | lp->cmp = cf; 22 | lp->ll = NULL; 23 | lp->iters = NULL; 24 | 25 | return lp; 26 | } 27 | 28 | void SLDestroy(SortedListPtr list) { 29 | node ptr, t; 30 | 31 | for (ptr = list->ll; ptr != NULL; ) { 32 | t = ptr->next; 33 | destroy_node(ptr); 34 | ptr = t; 35 | } 36 | 37 | for (ptr = list->iters; ptr != NULL; ) { 38 | SLDestroyIterator(ptr->val); 39 | } 40 | 41 | free(list); 42 | } 43 | 44 | int SLInsert(SortedListPtr list, void *value) { 45 | node new_node = create_node(NULL); 46 | node ptr; 47 | 48 | // If list doesn't exist, return 0. 49 | if (list == NULL || value == NULL) { 50 | return 0; 51 | } 52 | 53 | // If the list is empty, this is the first node 54 | if (list->ll == NULL) { 55 | new_node->val = value; 56 | list->ll = new_node; 57 | return 1; 58 | } 59 | else { 60 | // Loop through the list 61 | for (ptr = list->ll; ptr != NULL; ptr = ptr->next) { 62 | // We have found what item it goes before 63 | if (list->cmp(value, ptr->val) >= 0) { 64 | new_node->next = ptr->next; 65 | new_node->val = ptr->val; 66 | ptr->val = value; 67 | ptr->next = new_node; 68 | return 1; 69 | } 70 | // Item goes at the end of the list 71 | if (ptr->next == NULL) { 72 | new_node->val = value; 73 | ptr->next = new_node; 74 | return 1; 75 | } 76 | } 77 | } 78 | return 0; 79 | } 80 | 81 | 82 | int SLRemove(SortedListPtr list, void *newObj) { 83 | /* SortedListIteratorPtr iter; */ 84 | node ptr, iterptr, prev = NULL; 85 | SortedListIteratorPtr iter; 86 | 87 | if (list == NULL || newObj == NULL) { 88 | return 0; 89 | } 90 | 91 | // Go through the list to find item to delete 92 | for (ptr = list->ll; ptr != NULL; ptr = ptr->next) { 93 | // We have found the item to remove if we are 94 | if (list->cmp(newObj, ptr->val) == 0) { 95 | 96 | // Go through iters to see if anything is 97 | // pointing to this 98 | for (iterptr = list->iters; iterptr != NULL; iterptr = iterptr->next) { 99 | iter = iterptr->val; 100 | if (iter->curr == ptr) { 101 | iter->curr = ptr->next; 102 | } 103 | } 104 | 105 | // Deleteing first note 106 | if (prev == NULL) { 107 | list->ll = ptr->next; 108 | destroy_node(ptr); 109 | return 1; 110 | } else { 111 | prev->next = ptr->next; 112 | destroy_node(ptr); 113 | return 1; 114 | } 115 | } 116 | // Set previous to this node 117 | prev = ptr; 118 | } 119 | 120 | return 0; 121 | } 122 | 123 | SortedListIteratorPtr SLCreateIterator(SortedListPtr list) { 124 | node t; 125 | SortedListIteratorPtr slip; 126 | 127 | slip = malloc(sizeof(struct SortedListIterator)); 128 | slip->sl = list; 129 | slip->curr = list->ll; 130 | 131 | // If list->iters is null, it means there are no iterators 132 | // associated with the list. 133 | if (list->iters == NULL) { 134 | list->iters = create_node(slip); 135 | } else { 136 | t = create_node(slip); 137 | t->next = list->iters; 138 | list->iters = t; 139 | } 140 | 141 | return slip; 142 | } 143 | 144 | 145 | void SLDestroyIterator(SortedListIteratorPtr iter) { 146 | node ptr, prev = NULL; 147 | SortedListIteratorPtr slip; 148 | SortedListPtr list = iter->sl; 149 | 150 | for (ptr = list->iters; ptr != NULL; ptr = ptr->next) { 151 | slip = ptr->val; 152 | 153 | //DELETE IT 154 | if (slip == iter) { 155 | if (prev == NULL) { 156 | list->iters = list->iters->next; 157 | } else { 158 | prev->next = ptr->next; 159 | } 160 | } 161 | prev = ptr; 162 | } 163 | 164 | free(iter); 165 | } 166 | 167 | void *SLNextItem(SortedListIteratorPtr iter) { 168 | if (iter->curr == NULL) 169 | return NULL; 170 | 171 | void *return_me = iter->curr->val; 172 | iter->curr = iter->curr->next; 173 | iter->last_val_returned = return_me; 174 | 175 | return return_me; 176 | } 177 | 178 | 179 | void printList(SortedListPtr list) { 180 | node ptr; 181 | 182 | for (ptr = list->ll; ptr != NULL; ptr = ptr->next) { 183 | printf("%d, ", *(int*)ptr->val); 184 | } 185 | printf("\n"); 186 | } 187 | 188 | void printListString(SortedListPtr list) { 189 | node ptr; 190 | 191 | for (ptr = list->ll; ptr != NULL; ptr = ptr->next) { 192 | printf("%s, ", (char*)ptr->val); 193 | } 194 | printf("\n"); 195 | } 196 | -------------------------------------------------------------------------------- /src/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * sorted-list.c 3 | */ 4 | 5 | #include 6 | #include 7 | #include 8 | #include "sorted-list.h" 9 | 10 | 11 | enum TYPE{ 12 | STRING, 13 | INT, 14 | DOUBLE 15 | }; 16 | 17 | int compareInts(void *p1, void *p2) 18 | { 19 | int i1 = *(int*)p1; 20 | int i2 = *(int*)p2; 21 | 22 | return i1 - i2; 23 | } 24 | 25 | int compareDoubles(void *p1, void *p2) 26 | { 27 | double d1 = *(double*)p1; 28 | double d2 = *(double*)p2; 29 | 30 | return (d1 < d2) ? -1 : ((d1 > d2) ? 1 : 0); 31 | } 32 | 33 | int compareStrings(void *p1, void *p2) 34 | { 35 | char *s1 = p1; 36 | char *s2 = p2; 37 | 38 | return strcmp(s1, s2); 39 | } 40 | 41 | 42 | 43 | 44 | 45 | 46 | /* 47 | * 48 | * Helper functions 49 | * 50 | */ 51 | int populate_list(SortedListPtr s, int type, int len, int iargs[], double dargs[],char*sargs[]){ 52 | 53 | int i; 54 | if(type == INT){ 55 | for(i = 0; i < len; i++){ 56 | if(SLInsert(s,&(iargs[i])) == 0){ 57 | return 0; 58 | } 59 | } 60 | }else if (type == DOUBLE){ 61 | for(i = 0; i < len; i++){ 62 | if(SLInsert(s,&(dargs[i])) == 0){ 63 | return 0; 64 | } 65 | } 66 | }else if(type==STRING){ 67 | printf("STRINGS\n"); 68 | for(i = 0; i < len; i++){ 69 | printf("Inserting %s\n", sargs[i]); 70 | 71 | if(SLInsert(s,sargs[i]) == 0){ 72 | return 0; 73 | } 74 | } 75 | } 76 | 77 | return 1; 78 | 79 | } 80 | 81 | 82 | 83 | 84 | void iterprint_all(SortedListPtr s, SortedListIteratorPtr iter, int type){ 85 | void *item; 86 | while(1){ 87 | item = SLNextItem(iter); 88 | if (item == NULL){ 89 | break; 90 | }else{ 91 | if(type == INT){ 92 | printf("%d ", *((int*)item)); 93 | }else if(type == DOUBLE){ 94 | printf("%f ", *((double*)item)); 95 | } 96 | else if(type==STRING){ 97 | printf("%s ", ((char*)item)); 98 | } 99 | } 100 | } 101 | printf("\n"); 102 | } 103 | 104 | void iterprint_all_int(SortedListPtr s, SortedListIteratorPtr iter){ 105 | iterprint_all(s,iter,INT); 106 | } 107 | 108 | void success(){ 109 | char*passmessage = "Success"; 110 | printf("%s\n",passmessage); 111 | } 112 | 113 | void failure(){ 114 | char*failmessage = "Failure"; 115 | printf("%s\n",failmessage); 116 | } 117 | 118 | /* 119 | * 120 | * 121 | * Main 122 | * 123 | * Don't use valgrind. Things will leak. 124 | * 125 | * 126 | */ 127 | 128 | int main(int argc, char*argv[]) 129 | { 130 | int choice = atoi(argv[1]); 131 | 132 | 133 | printf("Choice: %d\n", choice); 134 | 135 | if(choice == 1){ 136 | //1. Normal SLInserts (@ beginning, middle, end of list) 137 | //a. integers 138 | SortedListPtr s = SLCreate(compareInts); 139 | int iarr[6] = {7,5,155,42,-1,6}; 140 | if(populate_list(s,INT,sizeof(iarr)/sizeof(int),iarr,NULL,NULL) == 0){ 141 | failure(); 142 | return 1; 143 | } 144 | SortedListIteratorPtr iter = SLCreateIterator(s); 145 | iterprint_all(s,iter,INT); //prints 155 42 7 6 5 -1 146 | 147 | 148 | }else if(choice == 2){ 149 | //b. doubles 150 | SortedListPtr s = SLCreate(compareDoubles); 151 | 152 | double darr[6] = {7.43,5.55,155.26,42.1,-1.5,6.7}; 153 | if(populate_list(s,DOUBLE,sizeof(darr)/sizeof(double),NULL,darr,NULL) == 0){ 154 | failure(); 155 | return 1; 156 | } 157 | 158 | 159 | SortedListIteratorPtr iter = SLCreateIterator(s); 160 | iterprint_all(s,iter,DOUBLE); //prints 155.26 42.1 7.43 6.7 5.55 -1.5 161 | 162 | 163 | }else if(choice == 3){ 164 | //c. strings 165 | SortedListPtr s = SLCreate(compareStrings); 166 | 167 | char *carr[6] = {"cat","dog","catamaran","apple","cormorant","zebra"}; 168 | if(populate_list(s,STRING,6,0,0,carr) == 0){ 169 | failure(); 170 | return 1; 171 | } 172 | 173 | SortedListIteratorPtr iter = SLCreateIterator(s); 174 | iterprint_all(s,iter,STRING); //prints apple cat catamaran cormorant dog zebra 175 | 176 | 177 | 178 | }else if(choice == 4){ 179 | //SLInsert on null list; 180 | int x = 5; 181 | if(SLInsert(NULL, &x) == 0){ 182 | success(); 183 | }else{ 184 | failure(); 185 | } 186 | }else if(choice == 5){ 187 | //SLInsert null object 188 | SortedListPtr s = SLCreate(compareDoubles); 189 | 190 | if (SLInsert(s,NULL) == 0){ 191 | success(); 192 | }else{ 193 | failure(); 194 | } 195 | 196 | }else if(choice == 6){ 197 | //SLRemove nonexistent element 198 | SortedListPtr s = SLCreate(compareInts); 199 | int iarr[6] = {7,5,155,42,-1,6}; 200 | populate_list(s,INT,sizeof(iarr)/sizeof(int),iarr,0,0); 201 | 202 | int toremove = 54; 203 | if (SLRemove(s,&toremove) != 0) { 204 | failure(); 205 | }else{ 206 | success(); 207 | } 208 | }else if(choice == 7){ 209 | //SLRemove existing element (no duplicates; didn't handle them anyway) 210 | SortedListPtr s = SLCreate(compareInts); 211 | int iarr[6] = {7,5,155,42,-1,6}; 212 | populate_list(s,INT,sizeof(iarr)/sizeof(int),iarr,0,0); 213 | 214 | 215 | int toremove = 5; 216 | 217 | if (SLRemove(s,&toremove) != 0) { 218 | SortedListIteratorPtr iter = SLCreateIterator(s); 219 | iterprint_all_int(s,iter); //prints 155 42 7 6 -1 220 | }else{ 221 | failure(); 222 | } 223 | 224 | }else if(choice == 8){ 225 | //Iterate on empty list 226 | SortedListPtr s = SLCreate(compareInts); 227 | SortedListIteratorPtr iter = SLCreateIterator(s); 228 | iterprint_all_int(s,iter); 229 | //TODO: Separate into a) create iterator b) print empty list 230 | }else if(choice == 9){ 231 | //Create new iterator on list, destroy old one mid-iteration 232 | SortedListPtr s = SLCreate(compareInts); 233 | int iarr[6] = {7,5,155,42,-1,6}; 234 | populate_list(s,INT,sizeof(iarr)/sizeof(int),iarr,0,0); 235 | 236 | 237 | 238 | SortedListIteratorPtr olditer = SLCreateIterator(s); 239 | void *olditem = SLNextItem(olditer); 240 | olditem = SLNextItem(olditer); 241 | SortedListIteratorPtr iter = SLCreateIterator(s); 242 | SLDestroyIterator(olditer); 243 | 244 | iterprint_all_int(s,iter); //prints 155 42 7 6 5 -1 245 | }else if(choice == 10){ //TODO 246 | //Create multiple iterators on same list, interleave iterations. 247 | 248 | SortedListPtr s = SLCreate(compareInts); 249 | int iarr[6] = {7,5,155,42,-1,6}; 250 | populate_list(s,INT,sizeof(iarr)/sizeof(int),iarr,0,0); 251 | 252 | SortedListIteratorPtr olditer = SLCreateIterator(s); 253 | 254 | SortedListIteratorPtr iter = SLCreateIterator(s); 255 | 256 | 257 | void *item; 258 | 259 | item = SLNextItem(olditer); 260 | printf("%d ", *((int*)item)); 261 | item = SLNextItem(olditer); 262 | printf("%d ", *((int*)item)); //prints 155 42 263 | 264 | item = SLNextItem(iter); 265 | printf("%d ", *((int*)item)); 266 | item = SLNextItem(iter); 267 | printf("%d ", *((int*)item)); 268 | item = SLNextItem(iter); 269 | printf("%d ", *((int*)item)); //prints 155 42 7 270 | 271 | 272 | item = SLNextItem(iter); //prints 6 273 | printf("%d ", *((int*)item)); 274 | item = SLNextItem(olditer); 275 | printf("%d ", *((int*)item)); //prints 7 276 | 277 | 278 | iterprint_all_int(s,iter); //prints 5 -1 279 | 280 | iterprint_all_int(s,olditer); //prints 6 5 -1 281 | 282 | }else if(choice == 11){ 283 | //SLRemove end element, iterator positioned on it 284 | 285 | SortedListPtr s = SLCreate(compareInts); 286 | int iarr[3] = {7,5,3}; 287 | populate_list(s,INT,sizeof(iarr)/sizeof(int),iarr,0,0); 288 | 289 | 290 | SortedListIteratorPtr iter = SLCreateIterator(s); 291 | 292 | int x3 = 3; 293 | void *item = SLNextItem(iter); 294 | item = SLNextItem(iter); 295 | if(SLRemove(s,&x3) && SLNextItem(iter) == NULL){ 296 | success(); 297 | }else{ 298 | failure(); 299 | } 300 | 301 | }else if(choice == 12){ //TODO 302 | //SLRemove beginning element, iterator positioned on it 303 | 304 | SortedListPtr s = SLCreate(compareInts); 305 | int iarr[3] = {7,5,3}; 306 | populate_list(s,INT,sizeof(iarr)/sizeof(int),iarr,0,0); 307 | 308 | SortedListIteratorPtr iter = SLCreateIterator(s); 309 | 310 | int x1 = 7; 311 | if(SLRemove(s,&x1) && *((int*)SLNextItem(iter)) == 5 && 312 | *((int*)SLNextItem(iter)) == 3 && 313 | ((int*)SLNextItem(iter)) == NULL){ 314 | success(); 315 | }else{ 316 | failure(); 317 | } 318 | }else if(choice == 13){ //TODO 319 | //SLRemove element in middle, iterator positioned on it 320 | SortedListPtr s = SLCreate(compareInts); 321 | int iarr[3] = {7,5,3}; 322 | populate_list(s,INT,sizeof(iarr)/sizeof(int),iarr,0,0); 323 | 324 | SortedListIteratorPtr iter = SLCreateIterator(s); 325 | void *item = SLNextItem(iter); 326 | 327 | if (*((int*)item) != 7) { 328 | failure(); 329 | return 1; 330 | } 331 | 332 | int x1 = 5; 333 | if(SLRemove(s,&x1) && *((int*)SLNextItem(iter)) == 3 && 334 | ((int*)SLNextItem(iter)) == NULL){ 335 | success(); 336 | }else{ 337 | failure(); 338 | } 339 | }else if(choice == 14){ 340 | //Add element after iterator 341 | SortedListPtr s = SLCreate(compareInts); 342 | int iarr[3] = {7,5,3}; 343 | populate_list(s,INT,sizeof(iarr)/sizeof(int),iarr,0,0); 344 | 345 | SortedListIteratorPtr iter = SLCreateIterator(s); 346 | void *item = SLNextItem(iter); 347 | item = SLNextItem(iter); 348 | int x4 = 4; 349 | SLInsert(s,&x4); //prints 4 3 350 | 351 | iterprint_all_int(s,iter); 352 | 353 | 354 | }else if(choice == 15){ 355 | //Remove element after iterator 356 | SortedListPtr s = SLCreate(compareInts); 357 | int iarr[4] = {7,5,3,4}; 358 | populate_list(s,INT,sizeof(iarr)/sizeof(int),iarr,0,0); 359 | 360 | 361 | SortedListIteratorPtr iter = SLCreateIterator(s); 362 | void *item = SLNextItem(iter); 363 | item = SLNextItem(iter); 364 | int x4 = 4; 365 | if(SLRemove(s,&x4)){ 366 | iterprint_all_int(s,iter); //prints 3 367 | }else{ 368 | failure(); 369 | } 370 | }else{ 371 | printf("Bad input\n"); 372 | } 373 | 374 | 375 | return 0; 376 | 377 | } 378 | --------------------------------------------------------------------------------