hello
Dependents: nespresso_demo nespresso_endpoint EnvoyNespressoEndpointColorDetectorV2
Fork of nsdl by
sn_linked_list.c@2:05e4cd1a1542, 2014-07-14 (annotated)
- Committer:
- bjblazkowicz
- Date:
- Mon Jul 14 12:33:59 2014 +0000
- Revision:
- 2:05e4cd1a1542
- Parent:
- 0:f6e4e1bbb3fe
WIP - registration is broken in this commit.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
GeofferyOmlette | 0:f6e4e1bbb3fe | 1 | #include "nsdl_types.h" |
GeofferyOmlette | 0:f6e4e1bbb3fe | 2 | #include "sn_linked_list.h" |
GeofferyOmlette | 0:f6e4e1bbb3fe | 3 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 4 | SN_LINKED_LIST_FUNCTION_POINTER_MEMORY_ATTRIBUTE |
GeofferyOmlette | 0:f6e4e1bbb3fe | 5 | void *(*sn_linked_list_memory_alloc)(uint16_t); |
GeofferyOmlette | 0:f6e4e1bbb3fe | 6 | SN_LINKED_LIST_FUNCTION_POINTER_MEMORY_ATTRIBUTE |
GeofferyOmlette | 0:f6e4e1bbb3fe | 7 | void (*sn_linked_list_memory_free)(void*); |
GeofferyOmlette | 0:f6e4e1bbb3fe | 8 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 9 | SN_LINKED_LIST_FUNCTION_MEMORY_ATTRIBUTE |
GeofferyOmlette | 0:f6e4e1bbb3fe | 10 | void sn_linked_list_init(void *(*linked_list_alloc_function)(uint16_t), void (*linked_list_free_function)(void*)) |
GeofferyOmlette | 0:f6e4e1bbb3fe | 11 | { |
GeofferyOmlette | 0:f6e4e1bbb3fe | 12 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 13 | sn_linked_list_memory_alloc = linked_list_alloc_function; |
GeofferyOmlette | 0:f6e4e1bbb3fe | 14 | sn_linked_list_memory_free = linked_list_free_function; |
GeofferyOmlette | 0:f6e4e1bbb3fe | 15 | } |
GeofferyOmlette | 0:f6e4e1bbb3fe | 16 | SN_LINKED_LIST_FUNCTION_MEMORY_ATTRIBUTE |
GeofferyOmlette | 0:f6e4e1bbb3fe | 17 | sn_linked_list_t *sn_linked_list_create(void) |
GeofferyOmlette | 0:f6e4e1bbb3fe | 18 | { |
GeofferyOmlette | 0:f6e4e1bbb3fe | 19 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 20 | sn_linked_list_t *new_linked_list = sn_linked_list_memory_alloc(sizeof(sn_linked_list_t)); |
GeofferyOmlette | 0:f6e4e1bbb3fe | 21 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 22 | if(new_linked_list) |
GeofferyOmlette | 0:f6e4e1bbb3fe | 23 | { |
GeofferyOmlette | 0:f6e4e1bbb3fe | 24 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 25 | new_linked_list->first_node = NULL; |
GeofferyOmlette | 0:f6e4e1bbb3fe | 26 | new_linked_list->current_node = NULL; |
GeofferyOmlette | 0:f6e4e1bbb3fe | 27 | new_linked_list->node_count = 0; |
GeofferyOmlette | 0:f6e4e1bbb3fe | 28 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 29 | } |
GeofferyOmlette | 0:f6e4e1bbb3fe | 30 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 31 | return new_linked_list; |
GeofferyOmlette | 0:f6e4e1bbb3fe | 32 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 33 | } |
GeofferyOmlette | 0:f6e4e1bbb3fe | 34 | SN_LINKED_LIST_FUNCTION_MEMORY_ATTRIBUTE |
GeofferyOmlette | 0:f6e4e1bbb3fe | 35 | int8_t sn_linked_list_free(sn_linked_list_t *linked_list) |
GeofferyOmlette | 0:f6e4e1bbb3fe | 36 | { |
GeofferyOmlette | 0:f6e4e1bbb3fe | 37 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 38 | if(!linked_list) |
GeofferyOmlette | 0:f6e4e1bbb3fe | 39 | { |
GeofferyOmlette | 0:f6e4e1bbb3fe | 40 | return SN_LINKED_LIST_ERROR_INVALID_LIST_POINTER; |
GeofferyOmlette | 0:f6e4e1bbb3fe | 41 | } |
GeofferyOmlette | 0:f6e4e1bbb3fe | 42 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 43 | if(linked_list->first_node) |
GeofferyOmlette | 0:f6e4e1bbb3fe | 44 | { |
GeofferyOmlette | 0:f6e4e1bbb3fe | 45 | return SN_LINKED_LIST_ERROR_LIST_NOT_EMPTY; |
GeofferyOmlette | 0:f6e4e1bbb3fe | 46 | } |
GeofferyOmlette | 0:f6e4e1bbb3fe | 47 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 48 | sn_linked_list_memory_free(linked_list); |
GeofferyOmlette | 0:f6e4e1bbb3fe | 49 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 50 | return SN_LINKED_LIST_ERROR_NO_ERROR; |
GeofferyOmlette | 0:f6e4e1bbb3fe | 51 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 52 | } |
GeofferyOmlette | 0:f6e4e1bbb3fe | 53 | SN_LINKED_LIST_FUNCTION_MEMORY_ATTRIBUTE |
GeofferyOmlette | 0:f6e4e1bbb3fe | 54 | int8_t sn_linked_list_add_node(sn_linked_list_t *linked_list, void *data) |
GeofferyOmlette | 0:f6e4e1bbb3fe | 55 | { |
GeofferyOmlette | 0:f6e4e1bbb3fe | 56 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 57 | struct sn_linked_list_node *node_to_add = NULL; |
GeofferyOmlette | 0:f6e4e1bbb3fe | 58 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 59 | if(!linked_list) |
GeofferyOmlette | 0:f6e4e1bbb3fe | 60 | { |
GeofferyOmlette | 0:f6e4e1bbb3fe | 61 | return SN_LINKED_LIST_ERROR_INVALID_LIST_POINTER; |
GeofferyOmlette | 0:f6e4e1bbb3fe | 62 | } |
GeofferyOmlette | 0:f6e4e1bbb3fe | 63 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 64 | if(!data) |
GeofferyOmlette | 0:f6e4e1bbb3fe | 65 | { |
GeofferyOmlette | 0:f6e4e1bbb3fe | 66 | return SN_LINKED_LIST_ERROR_NO_DATA_TO_ADD; |
GeofferyOmlette | 0:f6e4e1bbb3fe | 67 | } |
GeofferyOmlette | 0:f6e4e1bbb3fe | 68 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 69 | node_to_add = sn_linked_list_memory_alloc(sizeof(struct sn_linked_list_node)); |
GeofferyOmlette | 0:f6e4e1bbb3fe | 70 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 71 | if(!node_to_add) |
GeofferyOmlette | 0:f6e4e1bbb3fe | 72 | { |
GeofferyOmlette | 0:f6e4e1bbb3fe | 73 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 74 | return SN_LINKED_LIST_ERROR_DATA_ALLOCATOIN_FAILED; |
GeofferyOmlette | 0:f6e4e1bbb3fe | 75 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 76 | } |
GeofferyOmlette | 0:f6e4e1bbb3fe | 77 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 78 | node_to_add->data = data; |
GeofferyOmlette | 0:f6e4e1bbb3fe | 79 | node_to_add->next_node = linked_list->first_node; |
GeofferyOmlette | 0:f6e4e1bbb3fe | 80 | node_to_add->previous_node = NULL; |
GeofferyOmlette | 0:f6e4e1bbb3fe | 81 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 82 | if(linked_list->first_node) |
GeofferyOmlette | 0:f6e4e1bbb3fe | 83 | { |
GeofferyOmlette | 0:f6e4e1bbb3fe | 84 | linked_list->first_node->previous_node = node_to_add; |
GeofferyOmlette | 0:f6e4e1bbb3fe | 85 | } |
GeofferyOmlette | 0:f6e4e1bbb3fe | 86 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 87 | linked_list->first_node = node_to_add; |
GeofferyOmlette | 0:f6e4e1bbb3fe | 88 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 89 | linked_list->node_count++; |
GeofferyOmlette | 0:f6e4e1bbb3fe | 90 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 91 | return SN_LINKED_LIST_ERROR_NO_ERROR; |
GeofferyOmlette | 0:f6e4e1bbb3fe | 92 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 93 | } |
GeofferyOmlette | 0:f6e4e1bbb3fe | 94 | SN_LINKED_LIST_FUNCTION_MEMORY_ATTRIBUTE |
GeofferyOmlette | 0:f6e4e1bbb3fe | 95 | void *sn_linked_list_get_first_node(sn_linked_list_t *linked_list) |
GeofferyOmlette | 0:f6e4e1bbb3fe | 96 | { |
GeofferyOmlette | 0:f6e4e1bbb3fe | 97 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 98 | if(!linked_list) |
GeofferyOmlette | 0:f6e4e1bbb3fe | 99 | { |
GeofferyOmlette | 0:f6e4e1bbb3fe | 100 | return NULL; |
GeofferyOmlette | 0:f6e4e1bbb3fe | 101 | } |
GeofferyOmlette | 0:f6e4e1bbb3fe | 102 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 103 | if(linked_list->first_node) |
GeofferyOmlette | 0:f6e4e1bbb3fe | 104 | { |
GeofferyOmlette | 0:f6e4e1bbb3fe | 105 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 106 | linked_list->current_node = linked_list->first_node; |
GeofferyOmlette | 0:f6e4e1bbb3fe | 107 | return linked_list->first_node->data; |
GeofferyOmlette | 0:f6e4e1bbb3fe | 108 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 109 | } |
GeofferyOmlette | 0:f6e4e1bbb3fe | 110 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 111 | return NULL; |
GeofferyOmlette | 0:f6e4e1bbb3fe | 112 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 113 | } |
GeofferyOmlette | 0:f6e4e1bbb3fe | 114 | SN_LINKED_LIST_FUNCTION_MEMORY_ATTRIBUTE |
GeofferyOmlette | 0:f6e4e1bbb3fe | 115 | void *sn_linked_list_get_next_node(sn_linked_list_t *linked_list) |
GeofferyOmlette | 0:f6e4e1bbb3fe | 116 | { |
GeofferyOmlette | 0:f6e4e1bbb3fe | 117 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 118 | if(!linked_list) |
GeofferyOmlette | 0:f6e4e1bbb3fe | 119 | { |
GeofferyOmlette | 0:f6e4e1bbb3fe | 120 | return NULL; |
GeofferyOmlette | 0:f6e4e1bbb3fe | 121 | } |
GeofferyOmlette | 0:f6e4e1bbb3fe | 122 | if(!linked_list->current_node) |
GeofferyOmlette | 0:f6e4e1bbb3fe | 123 | { |
GeofferyOmlette | 0:f6e4e1bbb3fe | 124 | return NULL; |
GeofferyOmlette | 0:f6e4e1bbb3fe | 125 | } |
GeofferyOmlette | 0:f6e4e1bbb3fe | 126 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 127 | linked_list->current_node = linked_list->current_node->next_node; |
GeofferyOmlette | 0:f6e4e1bbb3fe | 128 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 129 | if(!linked_list->current_node) |
GeofferyOmlette | 0:f6e4e1bbb3fe | 130 | { |
GeofferyOmlette | 0:f6e4e1bbb3fe | 131 | return NULL; |
GeofferyOmlette | 0:f6e4e1bbb3fe | 132 | } |
GeofferyOmlette | 0:f6e4e1bbb3fe | 133 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 134 | return linked_list->current_node->data; |
GeofferyOmlette | 0:f6e4e1bbb3fe | 135 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 136 | } |
GeofferyOmlette | 0:f6e4e1bbb3fe | 137 | SN_LINKED_LIST_FUNCTION_MEMORY_ATTRIBUTE |
GeofferyOmlette | 0:f6e4e1bbb3fe | 138 | void *sn_linked_list_get_current_node(sn_linked_list_t *linked_list) |
GeofferyOmlette | 0:f6e4e1bbb3fe | 139 | { |
GeofferyOmlette | 0:f6e4e1bbb3fe | 140 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 141 | if(!linked_list) |
GeofferyOmlette | 0:f6e4e1bbb3fe | 142 | { |
GeofferyOmlette | 0:f6e4e1bbb3fe | 143 | return NULL; |
GeofferyOmlette | 0:f6e4e1bbb3fe | 144 | } |
GeofferyOmlette | 0:f6e4e1bbb3fe | 145 | if(!linked_list->current_node) |
GeofferyOmlette | 0:f6e4e1bbb3fe | 146 | { |
GeofferyOmlette | 0:f6e4e1bbb3fe | 147 | return NULL; |
GeofferyOmlette | 0:f6e4e1bbb3fe | 148 | } |
GeofferyOmlette | 0:f6e4e1bbb3fe | 149 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 150 | return linked_list->current_node->data; |
GeofferyOmlette | 0:f6e4e1bbb3fe | 151 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 152 | } |
GeofferyOmlette | 0:f6e4e1bbb3fe | 153 | SN_LINKED_LIST_FUNCTION_MEMORY_ATTRIBUTE |
GeofferyOmlette | 0:f6e4e1bbb3fe | 154 | void *sn_linked_list_get_previous_node(sn_linked_list_t *linked_list) |
GeofferyOmlette | 0:f6e4e1bbb3fe | 155 | { |
GeofferyOmlette | 0:f6e4e1bbb3fe | 156 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 157 | if(!linked_list) |
GeofferyOmlette | 0:f6e4e1bbb3fe | 158 | { |
GeofferyOmlette | 0:f6e4e1bbb3fe | 159 | return NULL; |
GeofferyOmlette | 0:f6e4e1bbb3fe | 160 | } |
GeofferyOmlette | 0:f6e4e1bbb3fe | 161 | if(!linked_list->current_node) |
GeofferyOmlette | 0:f6e4e1bbb3fe | 162 | { |
GeofferyOmlette | 0:f6e4e1bbb3fe | 163 | return NULL; |
GeofferyOmlette | 0:f6e4e1bbb3fe | 164 | } |
GeofferyOmlette | 0:f6e4e1bbb3fe | 165 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 166 | linked_list->current_node = linked_list->current_node->previous_node; |
GeofferyOmlette | 0:f6e4e1bbb3fe | 167 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 168 | if(!linked_list->current_node) |
GeofferyOmlette | 0:f6e4e1bbb3fe | 169 | { |
GeofferyOmlette | 0:f6e4e1bbb3fe | 170 | return NULL; |
GeofferyOmlette | 0:f6e4e1bbb3fe | 171 | } |
GeofferyOmlette | 0:f6e4e1bbb3fe | 172 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 173 | return linked_list->current_node->data; |
GeofferyOmlette | 0:f6e4e1bbb3fe | 174 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 175 | } |
GeofferyOmlette | 0:f6e4e1bbb3fe | 176 | SN_LINKED_LIST_FUNCTION_MEMORY_ATTRIBUTE |
GeofferyOmlette | 0:f6e4e1bbb3fe | 177 | extern void *sn_linked_list_get_last_node(sn_linked_list_t *linked_list) |
GeofferyOmlette | 0:f6e4e1bbb3fe | 178 | { |
GeofferyOmlette | 0:f6e4e1bbb3fe | 179 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 180 | if(!sn_linked_list_get_first_node(linked_list)) |
GeofferyOmlette | 0:f6e4e1bbb3fe | 181 | { |
GeofferyOmlette | 0:f6e4e1bbb3fe | 182 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 183 | return NULL; |
GeofferyOmlette | 0:f6e4e1bbb3fe | 184 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 185 | } |
GeofferyOmlette | 0:f6e4e1bbb3fe | 186 | while(linked_list->current_node->next_node) |
GeofferyOmlette | 0:f6e4e1bbb3fe | 187 | { |
GeofferyOmlette | 0:f6e4e1bbb3fe | 188 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 189 | sn_linked_list_get_next_node(linked_list); |
GeofferyOmlette | 0:f6e4e1bbb3fe | 190 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 191 | } |
GeofferyOmlette | 0:f6e4e1bbb3fe | 192 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 193 | return linked_list->current_node->data; |
GeofferyOmlette | 0:f6e4e1bbb3fe | 194 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 195 | } |
GeofferyOmlette | 0:f6e4e1bbb3fe | 196 | SN_LINKED_LIST_FUNCTION_MEMORY_ATTRIBUTE |
GeofferyOmlette | 0:f6e4e1bbb3fe | 197 | void *sn_linked_list_remove_current_node(sn_linked_list_t *linked_list) |
GeofferyOmlette | 0:f6e4e1bbb3fe | 198 | { |
GeofferyOmlette | 0:f6e4e1bbb3fe | 199 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 200 | struct sn_linked_list_node *node_to_remove = NULL; |
GeofferyOmlette | 0:f6e4e1bbb3fe | 201 | void *data_from_removed_node = NULL; |
GeofferyOmlette | 0:f6e4e1bbb3fe | 202 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 203 | if(!linked_list) |
GeofferyOmlette | 0:f6e4e1bbb3fe | 204 | { |
GeofferyOmlette | 0:f6e4e1bbb3fe | 205 | return NULL; |
GeofferyOmlette | 0:f6e4e1bbb3fe | 206 | } |
GeofferyOmlette | 0:f6e4e1bbb3fe | 207 | if(!linked_list->current_node) |
GeofferyOmlette | 0:f6e4e1bbb3fe | 208 | { |
GeofferyOmlette | 0:f6e4e1bbb3fe | 209 | return NULL; |
GeofferyOmlette | 0:f6e4e1bbb3fe | 210 | } |
GeofferyOmlette | 0:f6e4e1bbb3fe | 211 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 212 | node_to_remove = linked_list->current_node; |
GeofferyOmlette | 0:f6e4e1bbb3fe | 213 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 214 | if(node_to_remove != linked_list->first_node) |
GeofferyOmlette | 0:f6e4e1bbb3fe | 215 | { |
GeofferyOmlette | 0:f6e4e1bbb3fe | 216 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 217 | node_to_remove->previous_node->next_node = node_to_remove->next_node; |
GeofferyOmlette | 0:f6e4e1bbb3fe | 218 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 219 | if(node_to_remove->next_node) |
GeofferyOmlette | 0:f6e4e1bbb3fe | 220 | { |
GeofferyOmlette | 0:f6e4e1bbb3fe | 221 | node_to_remove->next_node->previous_node = node_to_remove->previous_node; |
GeofferyOmlette | 0:f6e4e1bbb3fe | 222 | } |
GeofferyOmlette | 0:f6e4e1bbb3fe | 223 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 224 | } |
GeofferyOmlette | 0:f6e4e1bbb3fe | 225 | else |
GeofferyOmlette | 0:f6e4e1bbb3fe | 226 | { |
GeofferyOmlette | 0:f6e4e1bbb3fe | 227 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 228 | linked_list->first_node = node_to_remove->next_node; |
GeofferyOmlette | 0:f6e4e1bbb3fe | 229 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 230 | if(node_to_remove->next_node) |
GeofferyOmlette | 0:f6e4e1bbb3fe | 231 | { |
GeofferyOmlette | 0:f6e4e1bbb3fe | 232 | node_to_remove->next_node->previous_node = NULL; |
GeofferyOmlette | 0:f6e4e1bbb3fe | 233 | } |
GeofferyOmlette | 0:f6e4e1bbb3fe | 234 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 235 | } |
GeofferyOmlette | 0:f6e4e1bbb3fe | 236 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 237 | if(!node_to_remove->next_node && node_to_remove->previous_node) |
GeofferyOmlette | 0:f6e4e1bbb3fe | 238 | { |
GeofferyOmlette | 0:f6e4e1bbb3fe | 239 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 240 | linked_list->current_node = node_to_remove->previous_node; |
GeofferyOmlette | 0:f6e4e1bbb3fe | 241 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 242 | } |
GeofferyOmlette | 0:f6e4e1bbb3fe | 243 | else |
GeofferyOmlette | 0:f6e4e1bbb3fe | 244 | { |
GeofferyOmlette | 0:f6e4e1bbb3fe | 245 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 246 | linked_list->current_node = node_to_remove->next_node; |
GeofferyOmlette | 0:f6e4e1bbb3fe | 247 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 248 | } |
GeofferyOmlette | 0:f6e4e1bbb3fe | 249 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 250 | data_from_removed_node = node_to_remove->data; |
GeofferyOmlette | 0:f6e4e1bbb3fe | 251 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 252 | sn_linked_list_memory_free(node_to_remove); |
GeofferyOmlette | 0:f6e4e1bbb3fe | 253 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 254 | linked_list->node_count--; |
GeofferyOmlette | 0:f6e4e1bbb3fe | 255 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 256 | return data_from_removed_node; |
GeofferyOmlette | 0:f6e4e1bbb3fe | 257 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 258 | } |
GeofferyOmlette | 0:f6e4e1bbb3fe | 259 | SN_LINKED_LIST_FUNCTION_MEMORY_ATTRIBUTE |
GeofferyOmlette | 0:f6e4e1bbb3fe | 260 | int8_t sn_linked_list_update_current_node(sn_linked_list_t *linked_list, void *data) |
GeofferyOmlette | 0:f6e4e1bbb3fe | 261 | { |
GeofferyOmlette | 0:f6e4e1bbb3fe | 262 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 263 | if(!linked_list) |
GeofferyOmlette | 0:f6e4e1bbb3fe | 264 | { |
GeofferyOmlette | 0:f6e4e1bbb3fe | 265 | return SN_LINKED_LIST_ERROR_INVALID_LIST_POINTER; |
GeofferyOmlette | 0:f6e4e1bbb3fe | 266 | } |
GeofferyOmlette | 0:f6e4e1bbb3fe | 267 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 268 | if(!data) |
GeofferyOmlette | 0:f6e4e1bbb3fe | 269 | { |
GeofferyOmlette | 0:f6e4e1bbb3fe | 270 | return SN_LINKED_LIST_ERROR_NO_DATA_TO_ADD; |
GeofferyOmlette | 0:f6e4e1bbb3fe | 271 | } |
GeofferyOmlette | 0:f6e4e1bbb3fe | 272 | if(!linked_list->current_node) |
GeofferyOmlette | 0:f6e4e1bbb3fe | 273 | { |
GeofferyOmlette | 0:f6e4e1bbb3fe | 274 | return SN_LINKED_LIST_ERROR_INVALID_LIST_POINTER; |
GeofferyOmlette | 0:f6e4e1bbb3fe | 275 | } |
GeofferyOmlette | 0:f6e4e1bbb3fe | 276 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 277 | linked_list->current_node->data = data; |
GeofferyOmlette | 0:f6e4e1bbb3fe | 278 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 279 | return SN_LINKED_LIST_ERROR_NO_ERROR; |
GeofferyOmlette | 0:f6e4e1bbb3fe | 280 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 281 | } |
GeofferyOmlette | 0:f6e4e1bbb3fe | 282 | SN_LINKED_LIST_FUNCTION_MEMORY_ATTRIBUTE |
GeofferyOmlette | 0:f6e4e1bbb3fe | 283 | uint16_t sn_linked_list_count_nodes(sn_linked_list_t *linked_list) |
GeofferyOmlette | 0:f6e4e1bbb3fe | 284 | { |
GeofferyOmlette | 0:f6e4e1bbb3fe | 285 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 286 | return linked_list->node_count; |
GeofferyOmlette | 0:f6e4e1bbb3fe | 287 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 288 | } |
GeofferyOmlette | 0:f6e4e1bbb3fe | 289 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 290 | /* |
GeofferyOmlette | 0:f6e4e1bbb3fe | 291 | #include <stdio.h> |
GeofferyOmlette | 0:f6e4e1bbb3fe | 292 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 293 | int main() |
GeofferyOmlette | 0:f6e4e1bbb3fe | 294 | { |
GeofferyOmlette | 0:f6e4e1bbb3fe | 295 | int test_node1 = 8; |
GeofferyOmlette | 0:f6e4e1bbb3fe | 296 | int test_node2 = 3456; |
GeofferyOmlette | 0:f6e4e1bbb3fe | 297 | int test_node3 = 321; |
GeofferyOmlette | 0:f6e4e1bbb3fe | 298 | sn_linked_list_t *linked_list = NULL; |
GeofferyOmlette | 0:f6e4e1bbb3fe | 299 | linked_list = sn_linked_list_create(); |
GeofferyOmlette | 0:f6e4e1bbb3fe | 300 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 301 | printf("test 1: %i\n",sn_linked_list_add_node(linked_list, &test_node1)); |
GeofferyOmlette | 0:f6e4e1bbb3fe | 302 | printf("test 2: %i\n",sn_linked_list_add_node(linked_list, &test_node2)); |
GeofferyOmlette | 0:f6e4e1bbb3fe | 303 | printf("test 3: %i\n",sn_linked_list_add_node(linked_list, &test_node3)); |
GeofferyOmlette | 0:f6e4e1bbb3fe | 304 | printf("test 4: %i\n",(int*)sn_linked_list_get_first_node(linked_list)); |
GeofferyOmlette | 0:f6e4e1bbb3fe | 305 | printf("test 5: %i\n",(int*)sn_linked_list_get_next_node(linked_list)); |
GeofferyOmlette | 0:f6e4e1bbb3fe | 306 | printf("test 6: %i\n",sn_linked_list_remove_current_node(linked_list)); |
GeofferyOmlette | 0:f6e4e1bbb3fe | 307 | printf("test 7: %i\n",sn_linked_list_remove_current_node(linked_list)); |
GeofferyOmlette | 0:f6e4e1bbb3fe | 308 | printf("test 8: %i\n",sn_linked_list_remove_current_node(linked_list)); |
GeofferyOmlette | 0:f6e4e1bbb3fe | 309 | printf("test 9: %i\n",sn_linked_list_free(linked_list)); |
GeofferyOmlette | 0:f6e4e1bbb3fe | 310 | printf("test 10: %i\n",(int*)sn_linked_list_get_first_node(linked_list)); |
GeofferyOmlette | 0:f6e4e1bbb3fe | 311 | printf("test 11: %i\n",sn_linked_list_remove_current_node(linked_list)); |
GeofferyOmlette | 0:f6e4e1bbb3fe | 312 | printf("test 12: %i\n",sn_linked_list_free(linked_list)); |
GeofferyOmlette | 0:f6e4e1bbb3fe | 313 | linked_list = NULL; |
GeofferyOmlette | 0:f6e4e1bbb3fe | 314 | printf("test 13: %i\n",sn_linked_list_add_node(linked_list, &test_node1)); |
GeofferyOmlette | 0:f6e4e1bbb3fe | 315 | linked_list = sn_linked_list_create(); |
GeofferyOmlette | 0:f6e4e1bbb3fe | 316 | if(linked_list) |
GeofferyOmlette | 0:f6e4e1bbb3fe | 317 | { |
GeofferyOmlette | 0:f6e4e1bbb3fe | 318 | printf("test 14: %i\n",sn_linked_list_add_node(linked_list, &test_node1)); |
GeofferyOmlette | 0:f6e4e1bbb3fe | 319 | printf("test 15: %i\n",sn_linked_list_add_node(linked_list, &test_node2)); |
GeofferyOmlette | 0:f6e4e1bbb3fe | 320 | printf("test 16: %i\n",sn_linked_list_add_node(linked_list, &test_node3)); |
GeofferyOmlette | 0:f6e4e1bbb3fe | 321 | printf("test 17: %i\n",(int*)sn_linked_list_get_next_node(linked_list)); |
GeofferyOmlette | 0:f6e4e1bbb3fe | 322 | printf("test 18: %i\n",(int*)sn_linked_list_get_first_node(linked_list)); |
GeofferyOmlette | 0:f6e4e1bbb3fe | 323 | printf("test 19: %i\n",(int*)sn_linked_list_get_next_node(linked_list)); |
GeofferyOmlette | 0:f6e4e1bbb3fe | 324 | printf("test 20: %i\n",(int*)sn_linked_list_get_next_node(linked_list)); |
GeofferyOmlette | 0:f6e4e1bbb3fe | 325 | printf("test 21: %i\n",(int*)sn_linked_list_get_next_node(linked_list)); |
GeofferyOmlette | 0:f6e4e1bbb3fe | 326 | printf("test 22: %i\n",(int*)sn_linked_list_get_next_node(linked_list)); |
GeofferyOmlette | 0:f6e4e1bbb3fe | 327 | printf("test 23: %i\n",(int*)sn_linked_list_get_first_node(linked_list)); |
GeofferyOmlette | 0:f6e4e1bbb3fe | 328 | printf("test 24: %i\n",sn_linked_list_remove_current_node(linked_list)); |
GeofferyOmlette | 0:f6e4e1bbb3fe | 329 | printf("test 25: %i\n",sn_linked_list_remove_current_node(linked_list)); |
GeofferyOmlette | 0:f6e4e1bbb3fe | 330 | printf("test 26: %i\n",sn_linked_list_remove_current_node(linked_list)); |
GeofferyOmlette | 0:f6e4e1bbb3fe | 331 | printf("test 27: %i\n",sn_linked_list_remove_current_node(linked_list)); |
GeofferyOmlette | 0:f6e4e1bbb3fe | 332 | printf("test 28: %i\n",sn_linked_list_add_node(linked_list, NULL)); |
GeofferyOmlette | 0:f6e4e1bbb3fe | 333 | printf("test 29: %i\n",sn_linked_list_free(linked_list)); |
GeofferyOmlette | 0:f6e4e1bbb3fe | 334 | } |
GeofferyOmlette | 0:f6e4e1bbb3fe | 335 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 336 | return 0; |
GeofferyOmlette | 0:f6e4e1bbb3fe | 337 | } |
GeofferyOmlette | 0:f6e4e1bbb3fe | 338 | */ |
GeofferyOmlette | 0:f6e4e1bbb3fe | 339 | |
GeofferyOmlette | 0:f6e4e1bbb3fe | 340 | /* EOF */ |