|
| 1 | +#include <stddef.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <stdio.h> |
| 4 | +#include <string.h> |
| 5 | + |
| 6 | +#include "../include/list.h" |
| 7 | +static listEntry_t *listEntry_create(void *data) { |
| 8 | + listEntry_t *createdListEntry = malloc(sizeof(listEntry_t)); |
| 9 | + if(createdListEntry == NULL) { |
| 10 | + // TODO Malloc Failure |
| 11 | + return NULL; |
| 12 | + } |
| 13 | + createdListEntry->data = data; |
| 14 | + |
| 15 | + return createdListEntry; |
| 16 | +} |
| 17 | + |
| 18 | +void listEntry_free(listEntry_t *listEntry, void *additionalData) { |
| 19 | + free(listEntry); |
| 20 | +} |
| 21 | + |
| 22 | +void listEntry_printAsInt(listEntry_t *listEntry, void *additionalData) { |
| 23 | + printf("%i\n", *(int *)listEntry->data); |
| 24 | +} |
| 25 | + |
| 26 | +list_t *list_createList() { |
| 27 | + list_t *createdList = malloc(sizeof(list_t)); |
| 28 | + if(createdList == NULL) { |
| 29 | + // TODO Malloc Failure |
| 30 | + return NULL; |
| 31 | + } |
| 32 | + createdList->firstEntry = NULL; |
| 33 | + createdList->lastEntry = NULL; |
| 34 | + createdList->count = 0; |
| 35 | + |
| 36 | + return createdList; |
| 37 | +} |
| 38 | + |
| 39 | +void list_iterateThroughListForward(list_t *list, |
| 40 | + void (*operationToPerform)( |
| 41 | + listEntry_t *, |
| 42 | + void *callbackFunctionUsedData), |
| 43 | + void *additionalDataNeededForCallbackFunction) |
| 44 | +{ |
| 45 | + listEntry_t *currentListEntry = list->firstEntry; |
| 46 | + listEntry_t *nextListEntry; |
| 47 | + |
| 48 | + if(currentListEntry == NULL) { |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + nextListEntry = currentListEntry->nextListEntry; |
| 53 | + |
| 54 | + operationToPerform(currentListEntry, |
| 55 | + additionalDataNeededForCallbackFunction); |
| 56 | + currentListEntry = nextListEntry; |
| 57 | + |
| 58 | + while(currentListEntry != NULL) { |
| 59 | + nextListEntry = currentListEntry->nextListEntry; |
| 60 | + operationToPerform(currentListEntry, |
| 61 | + additionalDataNeededForCallbackFunction); |
| 62 | + currentListEntry = nextListEntry; |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +void list_iterateThroughListBackward(list_t *list, |
| 67 | + void (*operationToPerform)( |
| 68 | + listEntry_t *, |
| 69 | + void *callbackFunctionUsedData), |
| 70 | + void *additionalDataNeededForCallbackFunction) |
| 71 | +{ |
| 72 | + listEntry_t *currentListEntry = list->lastEntry; |
| 73 | + listEntry_t *nextListEntry = currentListEntry->prevListEntry; |
| 74 | + |
| 75 | + if(currentListEntry == NULL) { |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + operationToPerform(currentListEntry, |
| 80 | + additionalDataNeededForCallbackFunction); |
| 81 | + currentListEntry = nextListEntry; |
| 82 | + |
| 83 | + while(currentListEntry != NULL) { |
| 84 | + nextListEntry = currentListEntry->prevListEntry; |
| 85 | + operationToPerform(currentListEntry, |
| 86 | + additionalDataNeededForCallbackFunction); |
| 87 | + currentListEntry = nextListEntry; |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +void list_freeList(list_t *list) { |
| 92 | + if(list){ |
| 93 | + list_iterateThroughListForward(list, listEntry_free, NULL); |
| 94 | + free(list); |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +void list_addElement(list_t *list, void *dataToAddInList) { |
| 99 | + listEntry_t *newListEntry = listEntry_create(dataToAddInList); |
| 100 | + if(newListEntry == NULL) { |
| 101 | + // TODO Malloc Failure |
| 102 | + return; |
| 103 | + } |
| 104 | + if(list->firstEntry == NULL) { |
| 105 | + list->firstEntry = newListEntry; |
| 106 | + list->lastEntry = newListEntry; |
| 107 | + |
| 108 | + newListEntry->prevListEntry = NULL; |
| 109 | + newListEntry->nextListEntry = NULL; |
| 110 | + |
| 111 | + list->count++; |
| 112 | + |
| 113 | + return; |
| 114 | + } |
| 115 | + |
| 116 | + list->lastEntry->nextListEntry = newListEntry; |
| 117 | + newListEntry->prevListEntry = list->lastEntry; |
| 118 | + newListEntry->nextListEntry = NULL; |
| 119 | + list->lastEntry = newListEntry; |
| 120 | + |
| 121 | + list->count++; |
| 122 | +} |
| 123 | + |
| 124 | +void list_removeElement(list_t *list, listEntry_t *elementToRemove) { |
| 125 | + listEntry_t *elementBeforeElementToRemove = |
| 126 | + elementToRemove->prevListEntry; |
| 127 | + listEntry_t *elementAfterElementToRemove = |
| 128 | + elementToRemove->nextListEntry; |
| 129 | + |
| 130 | + if(elementBeforeElementToRemove != NULL) { |
| 131 | + elementBeforeElementToRemove->nextListEntry = |
| 132 | + elementAfterElementToRemove; |
| 133 | + } else { |
| 134 | + list->firstEntry = elementAfterElementToRemove; |
| 135 | + } |
| 136 | + |
| 137 | + if(elementAfterElementToRemove != NULL) { |
| 138 | + elementAfterElementToRemove->prevListEntry = |
| 139 | + elementBeforeElementToRemove; |
| 140 | + } else { |
| 141 | + list->lastEntry = elementBeforeElementToRemove; |
| 142 | + } |
| 143 | + |
| 144 | + listEntry_free(elementToRemove, NULL); |
| 145 | + |
| 146 | + list->count--; |
| 147 | +} |
| 148 | + |
| 149 | +listEntry_t *list_getElementAt(list_t *list, long indexOfElement) { |
| 150 | + listEntry_t *currentListEntry; |
| 151 | + |
| 152 | + if((list->count / 2) > indexOfElement) { |
| 153 | + currentListEntry = list->firstEntry; |
| 154 | + |
| 155 | + for(int i = 0; i < indexOfElement; i++) { |
| 156 | + currentListEntry = currentListEntry->nextListEntry; |
| 157 | + } |
| 158 | + |
| 159 | + return currentListEntry; |
| 160 | + } else { |
| 161 | + currentListEntry = list->lastEntry; |
| 162 | + |
| 163 | + for(int i = 1; i < (list->count - indexOfElement); i++) { |
| 164 | + currentListEntry = currentListEntry->prevListEntry; |
| 165 | + } |
| 166 | + |
| 167 | + return currentListEntry; |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +char* findStrInStrList(list_t *strList, const char *str) |
| 172 | +{ |
| 173 | + if (!strList || !str) { |
| 174 | + return NULL; |
| 175 | + } |
| 176 | + |
| 177 | + listEntry_t* listEntry = NULL; |
| 178 | + list_ForEach(listEntry, strList) { |
| 179 | + if (strstr(listEntry->data, str) != NULL) { |
| 180 | + return listEntry->data; |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + return NULL; |
| 185 | +} |
| 186 | + |
| 187 | +void clear_and_free_string_list(list_t *list) |
| 188 | +{ |
| 189 | + if (!list) { |
| 190 | + return; |
| 191 | + } |
| 192 | + |
| 193 | + listEntry_t *listEntry = NULL; |
| 194 | + list_ForEach(listEntry, list) { |
| 195 | + char *list_item = listEntry->data; |
| 196 | + free(list_item); |
| 197 | + list_item = NULL; |
| 198 | + } |
| 199 | + list_freeList(list); |
| 200 | +} |
0 commit comments