|
| 1 | +/***************************************************************** |
| 2 | + * Circular Doubly Linked List |
| 3 | + * |
| 4 | + * Algorithm explanation: |
| 5 | + * A double-linked circular list is a data structure made up of |
| 6 | + * nodes. Each node has two pointers that point to the previous |
| 7 | + * node and the next node in the circular list. |
| 8 | + * |
| 9 | + *****************************************************************/ |
| 10 | +#include <stdio.h> |
| 11 | +#include <stdlib.h> |
| 12 | + |
| 13 | +// structure of a node |
| 14 | +typedef struct list |
| 15 | +{ |
| 16 | + double value; ///< value stored in the node |
| 17 | + struct list *next, *prev; ///< next/previous node pointing |
| 18 | +} List; |
| 19 | + |
| 20 | +// create list function |
| 21 | +List *create(double value) |
| 22 | +{ |
| 23 | + List *new_list = (List *)malloc(sizeof(List)); |
| 24 | + new_list->value = value; |
| 25 | + new_list->next = new_list; |
| 26 | + new_list->prev = new_list; |
| 27 | + return new_list; |
| 28 | +} |
| 29 | + |
| 30 | +// insert a value at the beginning of the list |
| 31 | +List *insertBegin(List *list, double value) |
| 32 | +{ |
| 33 | + if (list == NULL) |
| 34 | + list = create(value); |
| 35 | + else |
| 36 | + { |
| 37 | + List *new_node = create(value); |
| 38 | + new_node->next = list; |
| 39 | + new_node->prev = list->prev; |
| 40 | + list->prev->next = new_node; |
| 41 | + list->prev = new_node; |
| 42 | + list = new_node; |
| 43 | + } |
| 44 | + return list; |
| 45 | +} |
| 46 | + |
| 47 | +// insert a value at the end of the list |
| 48 | +List *insertEnd(List *list, double value) |
| 49 | +{ |
| 50 | + if (list == NULL) |
| 51 | + list = create(value); |
| 52 | + else |
| 53 | + { |
| 54 | + List *new_node = create(value); |
| 55 | + new_node->next = list; |
| 56 | + new_node->prev = list->prev; |
| 57 | + list->prev->next = new_node; |
| 58 | + list->prev = new_node; |
| 59 | + } |
| 60 | + return list; |
| 61 | +} |
| 62 | + |
| 63 | +// insert a value after other value in the list |
| 64 | +List *insertAfter(List *list, double new_value, double value) |
| 65 | +{ |
| 66 | + if (list == NULL) |
| 67 | + list = create(value); |
| 68 | + else |
| 69 | + { |
| 70 | + List *new_node = create(new_value), *prior = list; |
| 71 | + |
| 72 | + while (prior->value != value && prior != list->prev) |
| 73 | + prior = prior->next; |
| 74 | + |
| 75 | + if (prior->value == value) |
| 76 | + { |
| 77 | + new_node->next = prior->next; |
| 78 | + new_node->prev = prior; |
| 79 | + prior->next->prev = new_node; |
| 80 | + prior->next = new_node; |
| 81 | + } |
| 82 | + } |
| 83 | + return list; |
| 84 | +} |
| 85 | + |
| 86 | +// insert a value by position in the list |
| 87 | +List *insertPos(List *list, double value, int pos) |
| 88 | +{ |
| 89 | + if (list == NULL) |
| 90 | + list = create(value); |
| 91 | + |
| 92 | + // positive position case |
| 93 | + if (pos > 0) |
| 94 | + { |
| 95 | + // first position case |
| 96 | + if (pos == 1) |
| 97 | + return insertBegin(list, value); |
| 98 | + |
| 99 | + int it = 1; |
| 100 | + List *prior = list; |
| 101 | + |
| 102 | + while (it != pos && prior != list->prev) |
| 103 | + { |
| 104 | + prior = prior->next; |
| 105 | + it++; |
| 106 | + } |
| 107 | + |
| 108 | + // position in list range case |
| 109 | + if (it + 2 > pos) |
| 110 | + { |
| 111 | + |
| 112 | + // last position case |
| 113 | + if (prior == list->prev && it < pos) |
| 114 | + list = insertEnd(list, value); |
| 115 | + |
| 116 | + // general case |
| 117 | + else |
| 118 | + { |
| 119 | + List *new_node = create(value); |
| 120 | + new_node->next = prior; |
| 121 | + new_node->prev = prior->prev; |
| 122 | + prior->prev->next = new_node; |
| 123 | + prior->prev = new_node; |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + return list; |
| 128 | +} |
| 129 | + |
| 130 | +// delete a value at the beginning in the list |
| 131 | +List *deleteBegin(List *list) |
| 132 | +{ |
| 133 | + if (list != NULL) |
| 134 | + { |
| 135 | + List *tmp = list; |
| 136 | + list = list->next; |
| 137 | + tmp->prev->next = list; |
| 138 | + list->prev = tmp->prev; |
| 139 | + } |
| 140 | + return list; |
| 141 | +} |
| 142 | + |
| 143 | +// delete a value at the end in the list |
| 144 | +List *deleteEnd(List *list) |
| 145 | +{ |
| 146 | + if (list != NULL) |
| 147 | + { |
| 148 | + List *tmp = list->prev; |
| 149 | + tmp->prev->next = list; |
| 150 | + list->prev = tmp->prev; |
| 151 | + } |
| 152 | + return list; |
| 153 | +} |
| 154 | + |
| 155 | +// delete a value after other value in the list |
| 156 | +List *deleteAfter(List *list, double value) |
| 157 | +{ |
| 158 | + if (list != NULL) |
| 159 | + { |
| 160 | + List *prior = list; |
| 161 | + while (prior->value != value && prior != list->prev) |
| 162 | + prior = prior->next; |
| 163 | + |
| 164 | + if (prior != list->prev && prior->value == value) |
| 165 | + { |
| 166 | + List *tmp = prior->next; |
| 167 | + tmp->next->prev = prior; |
| 168 | + prior->next = tmp->next; |
| 169 | + } |
| 170 | + } |
| 171 | + return list; |
| 172 | +} |
| 173 | + |
| 174 | +// delete value by position in the list |
| 175 | +List *deletePos(List *list, int pos) |
| 176 | +{ |
| 177 | + if (list != NULL) |
| 178 | + { |
| 179 | + // first position case |
| 180 | + if (pos == 1) |
| 181 | + return deleteBegin(list); |
| 182 | + |
| 183 | + List *prior = list; |
| 184 | + int it = 1; |
| 185 | + |
| 186 | + while (it != pos && prior != list->prev) |
| 187 | + { |
| 188 | + prior = prior->next; |
| 189 | + it++; |
| 190 | + } |
| 191 | + |
| 192 | + // last position case |
| 193 | + if (prior == list->prev && it == pos) |
| 194 | + list = deleteEnd(list); |
| 195 | + |
| 196 | + // general case |
| 197 | + else |
| 198 | + { |
| 199 | + List *tmp = prior->next; |
| 200 | + prior->prev->next = tmp; |
| 201 | + tmp->prev = prior->prev; |
| 202 | + } |
| 203 | + } |
| 204 | + return list; |
| 205 | +} |
| 206 | + |
| 207 | +// search value function, `1` to exists and `0` to doesn't exist |
| 208 | +int search(List *list, double value) |
| 209 | +{ |
| 210 | + if (list != NULL) |
| 211 | + { |
| 212 | + List *last_node = list->prev, *it = list; |
| 213 | + while (it != last_node) |
| 214 | + { |
| 215 | + if (it->value == value) |
| 216 | + return 1; |
| 217 | + it = it->next; |
| 218 | + } |
| 219 | + if (it->value == value) |
| 220 | + return 1; |
| 221 | + return 0; |
| 222 | + } |
| 223 | +} |
| 224 | + |
| 225 | +// print the circular doubly linked list |
| 226 | +void display(List *list) |
| 227 | +{ |
| 228 | + if (list != NULL) |
| 229 | + { |
| 230 | + List *it = list; |
| 231 | + printf("\n...->"); |
| 232 | + if (it == list->prev) |
| 233 | + printf("%f<-...", it->value); |
| 234 | + else |
| 235 | + { |
| 236 | + while (it != list->prev) |
| 237 | + { |
| 238 | + printf("%f<->", it->value); |
| 239 | + it = it->next; |
| 240 | + } |
| 241 | + printf("%f<->", it->value); |
| 242 | + printf("<-...\n"); |
| 243 | + } |
| 244 | + } |
| 245 | +} |
| 246 | + |
| 247 | +// example function, it shows up how the code works. |
| 248 | +void example() |
| 249 | +{ |
| 250 | + List *my_list = NULL; |
| 251 | + |
| 252 | + my_list = create(0); |
| 253 | + display(my_list); |
| 254 | + |
| 255 | + my_list = insertBegin(my_list, 3); |
| 256 | + display(my_list); |
| 257 | + |
| 258 | + my_list = insertAfter(my_list, 2, 3); |
| 259 | + display(my_list); |
| 260 | + |
| 261 | + my_list = insertEnd(my_list, 5); |
| 262 | + display(my_list); |
| 263 | + |
| 264 | + my_list = insertPos(my_list, 100, 3); |
| 265 | + display(my_list); |
| 266 | + |
| 267 | + my_list = deleteBegin(my_list); |
| 268 | + display(my_list); |
| 269 | + |
| 270 | + my_list = deleteEnd(my_list); |
| 271 | + display(my_list); |
| 272 | + |
| 273 | + my_list = deleteAfter(my_list, 100); |
| 274 | + display(my_list); |
| 275 | + |
| 276 | + my_list = deletePos(my_list, 1); |
| 277 | + display(my_list); |
| 278 | + |
| 279 | + printf("\n%d\t%d", search(my_list, 100), search(my_list, 0)); |
| 280 | +} |
| 281 | + |
| 282 | +int main() |
| 283 | +{ |
| 284 | + example(); |
| 285 | + return 0; |
| 286 | +} |
0 commit comments