fixed deleteForward and deleteBackward
doubly_linked_list.cpp@1:c24e4dd27d48, 2016-10-12 (annotated)
- Committer:
- conantina
- Date:
- Wed Oct 12 15:29:04 2016 +0000
- Revision:
- 1:c24e4dd27d48
- Parent:
- 0:294bc2018c3a
Oct 12 Newest version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
conantina | 0:294bc2018c3a | 1 | #include <stdlib.h> |
conantina | 0:294bc2018c3a | 2 | #include <stdio.h> |
conantina | 0:294bc2018c3a | 3 | #include "doubly_linked_list.h" |
conantina | 0:294bc2018c3a | 4 | |
conantina | 0:294bc2018c3a | 5 | |
conantina | 0:294bc2018c3a | 6 | /** |
conantina | 1:c24e4dd27d48 | 7 | * create_llnode g |
conantina | 0:294bc2018c3a | 8 | * |
conantina | 0:294bc2018c3a | 9 | * Helper function that creates a node by allocating memory for it on the heap, |
conantina | 0:294bc2018c3a | 10 | * and initializing any pointers in it to NULL. |
conantina | 0:294bc2018c3a | 11 | * |
conantina | 0:294bc2018c3a | 12 | * @param data A void pointer to data the user is adding to the doublely linked list. |
conantina | 0:294bc2018c3a | 13 | * @return The llnode |
conantina | 0:294bc2018c3a | 14 | */ |
conantina | 0:294bc2018c3a | 15 | LLNode* create_llnode(void* data) { |
conantina | 0:294bc2018c3a | 16 | LLNode* newNode = (LLNode*)malloc(sizeof(LLNode)); |
conantina | 0:294bc2018c3a | 17 | newNode->data = data; |
conantina | 0:294bc2018c3a | 18 | newNode->previous = NULL; |
conantina | 0:294bc2018c3a | 19 | newNode->next = NULL; |
conantina | 0:294bc2018c3a | 20 | return newNode; |
conantina | 0:294bc2018c3a | 21 | } |
conantina | 0:294bc2018c3a | 22 | |
conantina | 0:294bc2018c3a | 23 | |
conantina | 0:294bc2018c3a | 24 | /** |
conantina | 0:294bc2018c3a | 25 | * create_dlinkedlist |
conantina | 0:294bc2018c3a | 26 | * |
conantina | 0:294bc2018c3a | 27 | * Creates a doublely liked list by allocating memory for it on the heap. Initialize the size to zero, as well as head and tail to NULL. |
conantina | 0:294bc2018c3a | 28 | * |
conantina | 0:294bc2018c3a | 29 | * @return An empty dlinkedlist |
conantina | 0:294bc2018c3a | 30 | */ |
conantina | 0:294bc2018c3a | 31 | DLinkedList* create_dlinkedlist(void) { |
conantina | 0:294bc2018c3a | 32 | DLinkedList* newList = (DLinkedList*)malloc(sizeof(DLinkedList)); |
conantina | 0:294bc2018c3a | 33 | newList->head = NULL; |
conantina | 0:294bc2018c3a | 34 | newList->tail = NULL; |
conantina | 0:294bc2018c3a | 35 | newList->current = NULL; |
conantina | 0:294bc2018c3a | 36 | newList->size = 0; |
conantina | 0:294bc2018c3a | 37 | return newList; |
conantina | 0:294bc2018c3a | 38 | } |
conantina | 0:294bc2018c3a | 39 | |
conantina | 0:294bc2018c3a | 40 | /** |
conantina | 0:294bc2018c3a | 41 | * insertHead |
conantina | 0:294bc2018c3a | 42 | * |
conantina | 0:294bc2018c3a | 43 | * insert the data to the head of the doublely linked list |
conantina | 0:294bc2018c3a | 44 | * |
conantina | 0:294bc2018c3a | 45 | * @param data A void pointer to data the user is adding to the doublely linked list. |
conantina | 0:294bc2018c3a | 46 | * |
conantina | 0:294bc2018c3a | 47 | */ |
conantina | 0:294bc2018c3a | 48 | |
conantina | 0:294bc2018c3a | 49 | void insertHead(DLinkedList* dLinkedList, void* data){ |
conantina | 0:294bc2018c3a | 50 | LLNode* newNode = create_llnode(data); |
conantina | 0:294bc2018c3a | 51 | if(dLinkedList->head == NULL){ |
conantina | 0:294bc2018c3a | 52 | dLinkedList->size++; |
conantina | 0:294bc2018c3a | 53 | dLinkedList->head = newNode; |
conantina | 0:294bc2018c3a | 54 | dLinkedList->current = newNode; |
conantina | 0:294bc2018c3a | 55 | dLinkedList->tail = newNode; |
conantina | 0:294bc2018c3a | 56 | }else{ |
conantina | 0:294bc2018c3a | 57 | dLinkedList->size++; |
conantina | 0:294bc2018c3a | 58 | newNode->next = dLinkedList->head; |
conantina | 0:294bc2018c3a | 59 | (dLinkedList->head)->previous = newNode; |
conantina | 0:294bc2018c3a | 60 | dLinkedList->head = newNode; |
conantina | 0:294bc2018c3a | 61 | } |
conantina | 0:294bc2018c3a | 62 | } |
conantina | 0:294bc2018c3a | 63 | |
conantina | 0:294bc2018c3a | 64 | /** |
conantina | 0:294bc2018c3a | 65 | * insertEnd |
conantina | 0:294bc2018c3a | 66 | * |
conantina | 0:294bc2018c3a | 67 | * insert the data to the tail of the doublely linked list |
conantina | 0:294bc2018c3a | 68 | * |
conantina | 0:294bc2018c3a | 69 | * @param data A void pointer to data the user is adding to the doublely linked list. |
conantina | 0:294bc2018c3a | 70 | * |
conantina | 0:294bc2018c3a | 71 | */ |
conantina | 0:294bc2018c3a | 72 | |
conantina | 0:294bc2018c3a | 73 | void insertTail(DLinkedList* dLinkedList, void* data){ |
conantina | 0:294bc2018c3a | 74 | LLNode* newNode = create_llnode(data); |
conantina | 0:294bc2018c3a | 75 | if(dLinkedList->tail == NULL){ |
conantina | 0:294bc2018c3a | 76 | dLinkedList->size++; |
conantina | 0:294bc2018c3a | 77 | dLinkedList->head = newNode; |
conantina | 0:294bc2018c3a | 78 | dLinkedList->current = newNode; |
conantina | 0:294bc2018c3a | 79 | dLinkedList->tail = newNode; |
conantina | 0:294bc2018c3a | 80 | }else{ |
conantina | 0:294bc2018c3a | 81 | dLinkedList->size++; |
conantina | 0:294bc2018c3a | 82 | newNode->previous = dLinkedList->tail; |
conantina | 0:294bc2018c3a | 83 | (dLinkedList->tail)->next = newNode; |
conantina | 0:294bc2018c3a | 84 | dLinkedList->tail = newNode; |
conantina | 0:294bc2018c3a | 85 | } |
conantina | 0:294bc2018c3a | 86 | } |
conantina | 0:294bc2018c3a | 87 | |
conantina | 0:294bc2018c3a | 88 | /** |
conantina | 0:294bc2018c3a | 89 | * insertAfter |
conantina | 0:294bc2018c3a | 90 | * |
conantina | 0:294bc2018c3a | 91 | * insert the new data to the doublely linked list right after the current pointer |
conantina | 0:294bc2018c3a | 92 | * |
conantina | 0:294bc2018c3a | 93 | * @param newData A void pointer to the new data that the user want to add after data |
conantina | 0:294bc2018c3a | 94 | * @return 1 if insert the new data successfully |
conantina | 0:294bc2018c3a | 95 | * 0 if not |
conantina | 0:294bc2018c3a | 96 | */ |
conantina | 0:294bc2018c3a | 97 | |
conantina | 0:294bc2018c3a | 98 | int insertAfter(DLinkedList* dLinkedList, void* newData){ |
conantina | 0:294bc2018c3a | 99 | if(dLinkedList->current == NULL){ |
conantina | 0:294bc2018c3a | 100 | return 0; |
conantina | 0:294bc2018c3a | 101 | }else{ |
conantina | 0:294bc2018c3a | 102 | LLNode* newNode = create_llnode(newData); |
conantina | 0:294bc2018c3a | 103 | newNode->previous = dLinkedList->current; |
conantina | 0:294bc2018c3a | 104 | newNode->next = (dLinkedList->current)->next; |
conantina | 0:294bc2018c3a | 105 | if(dLinkedList->current == dLinkedList->tail){ |
conantina | 0:294bc2018c3a | 106 | dLinkedList->tail = newNode; |
conantina | 0:294bc2018c3a | 107 | }else{ |
conantina | 0:294bc2018c3a | 108 | ((dLinkedList->current)->next)->previous = newNode; |
conantina | 0:294bc2018c3a | 109 | } |
conantina | 0:294bc2018c3a | 110 | (dLinkedList->current)->next = newNode; |
conantina | 0:294bc2018c3a | 111 | dLinkedList->size++; |
conantina | 0:294bc2018c3a | 112 | return 1; |
conantina | 0:294bc2018c3a | 113 | } |
conantina | 0:294bc2018c3a | 114 | |
conantina | 0:294bc2018c3a | 115 | } |
conantina | 0:294bc2018c3a | 116 | /** |
conantina | 0:294bc2018c3a | 117 | * insertBefore |
conantina | 0:294bc2018c3a | 118 | * |
conantina | 0:294bc2018c3a | 119 | * insert the new data to the doublely linked list right before the current pointer |
conantina | 0:294bc2018c3a | 120 | * |
conantina | 0:294bc2018c3a | 121 | * @param newData A void pointer to the new data that the user want to add after data |
conantina | 0:294bc2018c3a | 122 | * @return 1 if insert the new data successfully |
conantina | 0:294bc2018c3a | 123 | * 0 if not |
conantina | 0:294bc2018c3a | 124 | */ |
conantina | 0:294bc2018c3a | 125 | |
conantina | 0:294bc2018c3a | 126 | int insertBefore(DLinkedList* dLinkedList, void* newData){ |
conantina | 0:294bc2018c3a | 127 | if(dLinkedList->current == NULL){ |
conantina | 0:294bc2018c3a | 128 | return 0; |
conantina | 0:294bc2018c3a | 129 | }else{ |
conantina | 0:294bc2018c3a | 130 | LLNode* newNode = create_llnode(newData); |
conantina | 0:294bc2018c3a | 131 | newNode->next = dLinkedList->current; |
conantina | 0:294bc2018c3a | 132 | newNode->previous = (dLinkedList->current)->previous; |
conantina | 0:294bc2018c3a | 133 | if(dLinkedList->current == dLinkedList->head){ |
conantina | 0:294bc2018c3a | 134 | dLinkedList->head = newNode; |
conantina | 0:294bc2018c3a | 135 | }else{ |
conantina | 0:294bc2018c3a | 136 | ((dLinkedList->current)->previous)->next = newNode; |
conantina | 0:294bc2018c3a | 137 | } |
conantina | 0:294bc2018c3a | 138 | (dLinkedList->current)->previous = newNode; |
conantina | 0:294bc2018c3a | 139 | dLinkedList->size++; |
conantina | 0:294bc2018c3a | 140 | return 1; |
conantina | 0:294bc2018c3a | 141 | } |
conantina | 0:294bc2018c3a | 142 | } |
conantina | 0:294bc2018c3a | 143 | |
conantina | 0:294bc2018c3a | 144 | /** |
conantina | 0:294bc2018c3a | 145 | * deleteBackward |
conantina | 0:294bc2018c3a | 146 | * |
conantina | 0:294bc2018c3a | 147 | * delete the node the current pointer is pointed at, and move the pointer backwards |
conantina | 0:294bc2018c3a | 148 | * |
conantina | 0:294bc2018c3a | 149 | * @param free_func The function that the user created to free the data. |
conantina | 0:294bc2018c3a | 150 | * @return 1 if deleted successfully |
conantina | 0:294bc2018c3a | 151 | * 0 if not |
conantina | 0:294bc2018c3a | 152 | */ |
conantina | 0:294bc2018c3a | 153 | |
conantina | 0:294bc2018c3a | 154 | void* deleteBackward(DLinkedList* dLinkedList, dll_op free_func){ |
conantina | 0:294bc2018c3a | 155 | if(dLinkedList->current != NULL){ |
conantina | 0:294bc2018c3a | 156 | LLNode* deleteNode = dLinkedList->current; |
conantina | 0:294bc2018c3a | 157 | if((dLinkedList->current)->next != NULL){ |
conantina | 0:294bc2018c3a | 158 | ((dLinkedList->current)->next)->previous = (dLinkedList->current)->previous; |
conantina | 0:294bc2018c3a | 159 | } |
conantina | 0:294bc2018c3a | 160 | if((dLinkedList->current)->previous != NULL){ |
conantina | 0:294bc2018c3a | 161 | ((dLinkedList->current)->previous)->next = (dLinkedList->current)->next; |
conantina | 0:294bc2018c3a | 162 | } |
conantina | 0:294bc2018c3a | 163 | if((dLinkedList->current) == (dLinkedList->tail)){ |
conantina | 0:294bc2018c3a | 164 | dLinkedList->tail = (dLinkedList->current)->previous; |
conantina | 0:294bc2018c3a | 165 | } |
conantina | 0:294bc2018c3a | 166 | if((dLinkedList->current) == (dLinkedList->head)){ |
conantina | 0:294bc2018c3a | 167 | dLinkedList->head = (dLinkedList->current)->next; |
conantina | 0:294bc2018c3a | 168 | } |
conantina | 0:294bc2018c3a | 169 | if((dLinkedList->current)->previous != NULL){ |
conantina | 0:294bc2018c3a | 170 | dLinkedList->current = (dLinkedList->current)->previous; |
conantina | 0:294bc2018c3a | 171 | }else{ |
conantina | 0:294bc2018c3a | 172 | dLinkedList->current = NULL; |
conantina | 0:294bc2018c3a | 173 | } |
conantina | 0:294bc2018c3a | 174 | free_func(deleteNode->data); |
conantina | 0:294bc2018c3a | 175 | free(deleteNode); |
conantina | 0:294bc2018c3a | 176 | dLinkedList->size--; |
conantina | 0:294bc2018c3a | 177 | if(dLinkedList->current != NULL){ |
conantina | 0:294bc2018c3a | 178 | return (dLinkedList->current)->data; |
conantina | 0:294bc2018c3a | 179 | }else{ |
conantina | 0:294bc2018c3a | 180 | return NULL; |
conantina | 0:294bc2018c3a | 181 | } |
conantina | 0:294bc2018c3a | 182 | }else{ |
conantina | 0:294bc2018c3a | 183 | return NULL; |
conantina | 0:294bc2018c3a | 184 | } |
conantina | 0:294bc2018c3a | 185 | } |
conantina | 0:294bc2018c3a | 186 | |
conantina | 0:294bc2018c3a | 187 | |
conantina | 0:294bc2018c3a | 188 | /** |
conantina | 0:294bc2018c3a | 189 | * deleteForward |
conantina | 0:294bc2018c3a | 190 | * |
conantina | 0:294bc2018c3a | 191 | * delete the node the current pointer is pointed at, and move the pointer to the next |
conantina | 0:294bc2018c3a | 192 | * |
conantina | 0:294bc2018c3a | 193 | * @param free_func The function that the user created to free the data. |
conantina | 0:294bc2018c3a | 194 | * @return 1 if deleted successfully |
conantina | 0:294bc2018c3a | 195 | * 0 if not |
conantina | 0:294bc2018c3a | 196 | */ |
conantina | 0:294bc2018c3a | 197 | |
conantina | 0:294bc2018c3a | 198 | void* deleteForward(DLinkedList* dLinkedList, dll_op free_func){ |
conantina | 0:294bc2018c3a | 199 | if(dLinkedList->current != NULL){ |
conantina | 0:294bc2018c3a | 200 | LLNode* deleteNode = dLinkedList->current; |
conantina | 0:294bc2018c3a | 201 | if((dLinkedList->current)->next != NULL){ |
conantina | 0:294bc2018c3a | 202 | ((dLinkedList->current)->next)->previous = (dLinkedList->current)->previous; |
conantina | 0:294bc2018c3a | 203 | } |
conantina | 0:294bc2018c3a | 204 | if((dLinkedList->current)->previous != NULL){ |
conantina | 0:294bc2018c3a | 205 | ((dLinkedList->current)->previous)->next = (dLinkedList->current)->next; |
conantina | 0:294bc2018c3a | 206 | } |
conantina | 0:294bc2018c3a | 207 | if((dLinkedList->current) == (dLinkedList->tail)){ |
conantina | 0:294bc2018c3a | 208 | dLinkedList->tail = (dLinkedList->current)->previous; |
conantina | 0:294bc2018c3a | 209 | } |
conantina | 0:294bc2018c3a | 210 | if((dLinkedList->current) == (dLinkedList->head)){ |
conantina | 0:294bc2018c3a | 211 | dLinkedList->head = (dLinkedList->current)->next; |
conantina | 0:294bc2018c3a | 212 | } |
conantina | 0:294bc2018c3a | 213 | if((dLinkedList->current)->next != NULL){ |
conantina | 0:294bc2018c3a | 214 | dLinkedList->current = (dLinkedList->current)->next; |
conantina | 0:294bc2018c3a | 215 | }else{ |
conantina | 0:294bc2018c3a | 216 | dLinkedList->current = NULL; |
conantina | 0:294bc2018c3a | 217 | } |
conantina | 0:294bc2018c3a | 218 | free_func(deleteNode->data); |
conantina | 0:294bc2018c3a | 219 | free(deleteNode); |
conantina | 0:294bc2018c3a | 220 | dLinkedList->size--; |
conantina | 0:294bc2018c3a | 221 | if(dLinkedList->current != NULL){ |
conantina | 0:294bc2018c3a | 222 | return (dLinkedList->current)->data; |
conantina | 0:294bc2018c3a | 223 | }else{ |
conantina | 0:294bc2018c3a | 224 | return NULL; |
conantina | 0:294bc2018c3a | 225 | } |
conantina | 0:294bc2018c3a | 226 | }else{ |
conantina | 0:294bc2018c3a | 227 | return NULL; |
conantina | 0:294bc2018c3a | 228 | } |
conantina | 0:294bc2018c3a | 229 | } |
conantina | 0:294bc2018c3a | 230 | |
conantina | 0:294bc2018c3a | 231 | |
conantina | 0:294bc2018c3a | 232 | /** |
conantina | 0:294bc2018c3a | 233 | * traverseList |
conantina | 0:294bc2018c3a | 234 | * |
conantina | 0:294bc2018c3a | 235 | * traverse the list and do something on each pieae of data |
conantina | 0:294bc2018c3a | 236 | * |
conantina | 0:294bc2018c3a | 237 | * @param do_func The function to perform on each piece of data. |
conantina | 0:294bc2018c3a | 238 | * |
conantina | 0:294bc2018c3a | 239 | */ |
conantina | 0:294bc2018c3a | 240 | |
conantina | 0:294bc2018c3a | 241 | void traverseList(DLinkedList* dLinkedList, dll_op do_func){ |
conantina | 0:294bc2018c3a | 242 | if(dLinkedList->head != NULL){ |
conantina | 0:294bc2018c3a | 243 | LLNode* thisNode = dLinkedList->head; |
conantina | 0:294bc2018c3a | 244 | do{ |
conantina | 0:294bc2018c3a | 245 | do_func(thisNode->data); |
conantina | 0:294bc2018c3a | 246 | thisNode = thisNode->next; |
conantina | 0:294bc2018c3a | 247 | }while(thisNode != NULL); |
conantina | 0:294bc2018c3a | 248 | }else{ |
conantina | 0:294bc2018c3a | 249 | if(dLinkedList->head == NULL){ |
conantina | 0:294bc2018c3a | 250 | printf("Null head\n"); |
conantina | 0:294bc2018c3a | 251 | } |
conantina | 0:294bc2018c3a | 252 | if(dLinkedList->tail == NULL){ |
conantina | 0:294bc2018c3a | 253 | printf("Null tail\n"); |
conantina | 0:294bc2018c3a | 254 | } |
conantina | 0:294bc2018c3a | 255 | if(dLinkedList->current == NULL){ |
conantina | 0:294bc2018c3a | 256 | printf("Null current\n"); |
conantina | 0:294bc2018c3a | 257 | } |
conantina | 0:294bc2018c3a | 258 | } |
conantina | 0:294bc2018c3a | 259 | } |
conantina | 0:294bc2018c3a | 260 | |
conantina | 0:294bc2018c3a | 261 | |
conantina | 0:294bc2018c3a | 262 | /** |
conantina | 0:294bc2018c3a | 263 | * destroyList |
conantina | 0:294bc2018c3a | 264 | * |
conantina | 0:294bc2018c3a | 265 | * Destroy the doublely linked list. Everything in the linked list including list structure, |
conantina | 0:294bc2018c3a | 266 | * nodes and data are all freed from the heap. |
conantina | 0:294bc2018c3a | 267 | * |
conantina | 0:294bc2018c3a | 268 | * @param do_func The function to perform on each piece of data. |
conantina | 0:294bc2018c3a | 269 | * |
conantina | 0:294bc2018c3a | 270 | */ |
conantina | 0:294bc2018c3a | 271 | |
conantina | 0:294bc2018c3a | 272 | void destroyList(DLinkedList* dLinkedList, dll_op free_func){ |
conantina | 0:294bc2018c3a | 273 | if(dLinkedList->head != NULL){ |
conantina | 0:294bc2018c3a | 274 | getHead(dLinkedList); |
conantina | 0:294bc2018c3a | 275 | while(deleteForward(dLinkedList,free_func)){}; |
conantina | 0:294bc2018c3a | 276 | } |
conantina | 0:294bc2018c3a | 277 | free(dLinkedList); |
conantina | 0:294bc2018c3a | 278 | } |
conantina | 0:294bc2018c3a | 279 | |
conantina | 0:294bc2018c3a | 280 | |
conantina | 0:294bc2018c3a | 281 | /** |
conantina | 0:294bc2018c3a | 282 | * getHead |
conantina | 0:294bc2018c3a | 283 | * |
conantina | 0:294bc2018c3a | 284 | * return the data contained in the head of the doublely linked list, and set the current pointer |
conantina | 0:294bc2018c3a | 285 | * to head |
conantina | 0:294bc2018c3a | 286 | * @return the head data or NULL if head == NULL |
conantina | 0:294bc2018c3a | 287 | */ |
conantina | 0:294bc2018c3a | 288 | |
conantina | 0:294bc2018c3a | 289 | void* getHead(DLinkedList* dLinkedList){ |
conantina | 0:294bc2018c3a | 290 | if(dLinkedList->head != NULL){ |
conantina | 0:294bc2018c3a | 291 | dLinkedList->current = dLinkedList->head; |
conantina | 0:294bc2018c3a | 292 | return (dLinkedList->head)->data; |
conantina | 0:294bc2018c3a | 293 | }else{ |
conantina | 0:294bc2018c3a | 294 | return NULL; |
conantina | 0:294bc2018c3a | 295 | } |
conantina | 0:294bc2018c3a | 296 | } |
conantina | 0:294bc2018c3a | 297 | |
conantina | 0:294bc2018c3a | 298 | /** |
conantina | 0:294bc2018c3a | 299 | * getTail |
conantina | 0:294bc2018c3a | 300 | * |
conantina | 0:294bc2018c3a | 301 | * return the data contained in the tail of the doublely linked list, and set the current pointer |
conantina | 0:294bc2018c3a | 302 | * to tail |
conantina | 0:294bc2018c3a | 303 | * @return the tail data or NULL if tail == NULL |
conantina | 0:294bc2018c3a | 304 | */ |
conantina | 0:294bc2018c3a | 305 | void* getTail(DLinkedList* dLinkedList){ |
conantina | 0:294bc2018c3a | 306 | if(dLinkedList->tail != NULL){ |
conantina | 0:294bc2018c3a | 307 | dLinkedList->current = dLinkedList->tail; |
conantina | 0:294bc2018c3a | 308 | return (dLinkedList->tail)->data; |
conantina | 0:294bc2018c3a | 309 | }else{ |
conantina | 0:294bc2018c3a | 310 | return NULL; |
conantina | 0:294bc2018c3a | 311 | } |
conantina | 0:294bc2018c3a | 312 | } |
conantina | 0:294bc2018c3a | 313 | |
conantina | 0:294bc2018c3a | 314 | /** |
conantina | 0:294bc2018c3a | 315 | * getTail |
conantina | 0:294bc2018c3a | 316 | * |
conantina | 0:294bc2018c3a | 317 | * return the data the current pointer is pointing at |
conantina | 0:294bc2018c3a | 318 | * @return the current data or NULL if current == NULL |
conantina | 0:294bc2018c3a | 319 | */ |
conantina | 0:294bc2018c3a | 320 | |
conantina | 0:294bc2018c3a | 321 | void* getCurrent(DLinkedList* dLinkedList){ |
conantina | 0:294bc2018c3a | 322 | if((dLinkedList->current) == NULL){ |
conantina | 0:294bc2018c3a | 323 | return NULL; |
conantina | 0:294bc2018c3a | 324 | }else { |
conantina | 0:294bc2018c3a | 325 | return (dLinkedList->current)->data; |
conantina | 0:294bc2018c3a | 326 | } |
conantina | 0:294bc2018c3a | 327 | } |
conantina | 0:294bc2018c3a | 328 | |
conantina | 0:294bc2018c3a | 329 | /** |
conantina | 0:294bc2018c3a | 330 | * getNext |
conantina | 0:294bc2018c3a | 331 | * |
conantina | 0:294bc2018c3a | 332 | * return the next data the current pointer is pointing at, and move the current pointer to next |
conantina | 0:294bc2018c3a | 333 | * @return the next data or NULL if current == NULL |
conantina | 0:294bc2018c3a | 334 | */ |
conantina | 0:294bc2018c3a | 335 | |
conantina | 0:294bc2018c3a | 336 | void* getNext(DLinkedList* dLinkedList){ |
conantina | 0:294bc2018c3a | 337 | if(dLinkedList->current == NULL){ |
conantina | 0:294bc2018c3a | 338 | return NULL; |
conantina | 0:294bc2018c3a | 339 | }else if((dLinkedList->current)->next == NULL){ |
conantina | 0:294bc2018c3a | 340 | dLinkedList->current = NULL; |
conantina | 0:294bc2018c3a | 341 | return NULL; |
conantina | 0:294bc2018c3a | 342 | }else { |
conantina | 0:294bc2018c3a | 343 | dLinkedList->current = dLinkedList->current->next; |
conantina | 0:294bc2018c3a | 344 | return dLinkedList->current->data; |
conantina | 0:294bc2018c3a | 345 | } |
conantina | 0:294bc2018c3a | 346 | } |
conantina | 0:294bc2018c3a | 347 | |
conantina | 0:294bc2018c3a | 348 | /** |
conantina | 0:294bc2018c3a | 349 | * getPrevious |
conantina | 0:294bc2018c3a | 350 | * |
conantina | 0:294bc2018c3a | 351 | * return the previous data the current pointer is pointing at, and move the current pointer to |
conantina | 0:294bc2018c3a | 352 | * previous |
conantina | 0:294bc2018c3a | 353 | * @return the previous data or NULL if current == NULL |
conantina | 0:294bc2018c3a | 354 | */ |
conantina | 0:294bc2018c3a | 355 | |
conantina | 0:294bc2018c3a | 356 | void* getPrevious(DLinkedList* dLinkedList){ |
conantina | 0:294bc2018c3a | 357 | if(dLinkedList->current == NULL){ |
conantina | 0:294bc2018c3a | 358 | return NULL; |
conantina | 0:294bc2018c3a | 359 | }else if((dLinkedList->current)->previous == NULL){ |
conantina | 0:294bc2018c3a | 360 | dLinkedList->current = NULL; |
conantina | 0:294bc2018c3a | 361 | return NULL; |
conantina | 0:294bc2018c3a | 362 | }else { |
conantina | 0:294bc2018c3a | 363 | dLinkedList->current = dLinkedList->current->previous; |
conantina | 0:294bc2018c3a | 364 | return dLinkedList->current->data; |
conantina | 0:294bc2018c3a | 365 | } |
conantina | 0:294bc2018c3a | 366 | } |
conantina | 0:294bc2018c3a | 367 | |
conantina | 0:294bc2018c3a | 368 | |
conantina | 0:294bc2018c3a | 369 | /** |
conantina | 0:294bc2018c3a | 370 | * getSize |
conantina | 0:294bc2018c3a | 371 | * |
conantina | 0:294bc2018c3a | 372 | * @return the size |
conantina | 0:294bc2018c3a | 373 | */ |
conantina | 0:294bc2018c3a | 374 | |
conantina | 0:294bc2018c3a | 375 | int getSize(DLinkedList* dLinkedList){ |
conantina | 0:294bc2018c3a | 376 | return dLinkedList->size; |
conantina | 0:294bc2018c3a | 377 | } |
conantina | 0:294bc2018c3a | 378 | |
conantina | 0:294bc2018c3a | 379 |