|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <DoubleLinkedList.h> |
| 4 | + |
| 5 | +//Define the head, tail & length of the linkedlist |
| 6 | +int length = 0; |
| 7 | +Node *head = NULL, *tail = NULL; |
| 8 | + |
| 9 | +void Add(int data) |
| 10 | +{ |
| 11 | + Node *newNode = (Node *) malloc(sizeof(Node)); |
| 12 | + newNode->Data = data; |
| 13 | + newNode->Prev = newNode->Next = NULL; |
| 14 | + |
| 15 | + if(head == NULL) // linked list is empty |
| 16 | + { //first time call... |
| 17 | + head = tail = newNode; |
| 18 | + } |
| 19 | + else |
| 20 | + { |
| 21 | + //add a new item after the old tail |
| 22 | + tail->Next = newNode; |
| 23 | + //assign the tail to the Prev pointer of the newNode Pointer |
| 24 | + newNode->Prev = tail; |
| 25 | + //overwrite the old tail with the new one |
| 26 | + tail = newNode; |
| 27 | + } |
| 28 | + //assign the index to length-1 |
| 29 | + //length++ is a postfix incremental operation ==> increases the length after this line |
| 30 | + newNode->index = length++; |
| 31 | +} |
| 32 | + |
| 33 | +void Display() |
| 34 | +{ |
| 35 | + Node *current = head; |
| 36 | + |
| 37 | + while(current != NULL) |
| 38 | + { |
| 39 | + printf("%d \t", current->Data); |
| 40 | + current = current->Next; |
| 41 | + } |
| 42 | + printf("\n"); |
| 43 | +} |
| 44 | + |
| 45 | +Node* Search(int data) |
| 46 | +{ |
| 47 | + Node *current = head; |
| 48 | + |
| 49 | + while(current != NULL) |
| 50 | + { |
| 51 | + if(current->Data == data) |
| 52 | + { |
| 53 | + return current; |
| 54 | + } |
| 55 | + |
| 56 | + current = current->Next; |
| 57 | + } |
| 58 | + |
| 59 | + return NULL; |
| 60 | +} |
| 61 | +/** |
| 62 | +* swap the Next with the current Node index |
| 63 | +* starting from the start index. |
| 64 | +* @param startIndex value reference to the start index where the iterator would apply the indices swapping operation. |
| 65 | +*/ |
| 66 | +void updateIndices(){ |
| 67 | + int index = 0; |
| 68 | + Node* iterator = head; |
| 69 | + while(iterator != NULL && index < length){ |
| 70 | + iterator->index = index++; |
| 71 | + iterator = iterator->Next; |
| 72 | + } |
| 73 | +} |
| 74 | +void Delete(int data) |
| 75 | +{ |
| 76 | + //deep data copy |
| 77 | + Node *pDelete = Search(data); |
| 78 | + |
| 79 | + if(pDelete == NULL) |
| 80 | + return ; |
| 81 | + |
| 82 | + |
| 83 | + if(pDelete == head) |
| 84 | + { |
| 85 | + if(pDelete == tail) |
| 86 | + { |
| 87 | + head = tail = NULL; |
| 88 | + } |
| 89 | + else |
| 90 | + { |
| 91 | + head = pDelete->Next; |
| 92 | + head->Prev = NULL; |
| 93 | + } |
| 94 | + } |
| 95 | + else if(pDelete == tail) |
| 96 | + { |
| 97 | + tail = pDelete->Prev; |
| 98 | + tail->Next = NULL; |
| 99 | + } |
| 100 | + else |
| 101 | + { |
| 102 | + //assign the next of the previousNode to the next of the pDelete Node, dissolving the pDelete Node from the head |
| 103 | + Node* previousNode = pDelete->Prev; |
| 104 | + previousNode->Next = pDelete->Next; |
| 105 | + |
| 106 | + //assign the preva of the nextNode to the preva of the pDelete node, dissolving the pDelete Node from the tail |
| 107 | + Node* nextNode = pDelete->Next; |
| 108 | + nextNode->Prev = pDelete->Prev; |
| 109 | + } |
| 110 | + //remove one item from the length |
| 111 | + --length; |
| 112 | + //update the indices in accordance to the new length |
| 113 | + updateIndices(); |
| 114 | + free(pDelete); |
| 115 | + pDelete = NULL; |
| 116 | + |
| 117 | +} |
| 118 | + |
| 119 | +/** |
| 120 | +* inserts data in the node after this one. |
| 121 | +* @param afterData the data of the node that we want to add after it. |
| 122 | +* @param data the proposed data to fill inside the wanted node. |
| 123 | +*/ |
| 124 | +void InsertAfter(int afterData, int data){ |
| 125 | + //get the node to add after it |
| 126 | + Node* pNode = Search(afterData); |
| 127 | + //create a new node & fill its data |
| 128 | + Node* newNode = new Node(); |
| 129 | + newNode->Data = data; |
| 130 | + newNode->Prev = pNode; |
| 131 | + newNode->Next = pNode->Next; |
| 132 | + |
| 133 | + //assign the next Node of the pNode to the newNode values |
| 134 | + pNode->Next = newNode; |
| 135 | + //assign the previous Node of the next node of the newNode with the values of the newNode |
| 136 | + newNode->Next->Prev = newNode; |
| 137 | + //advance the length by 1 new Node |
| 138 | + ++length; |
| 139 | + //update the indices in accordance to the new length |
| 140 | + updateIndices(); |
| 141 | +} |
| 142 | +int GetItemsCount(){ |
| 143 | + return length; |
| 144 | +} |
| 145 | +/** |
| 146 | +* gets the node by an index |
| 147 | +* @param index. |
| 148 | +*/ |
| 149 | +Node* GetByIndex(int index){ |
| 150 | + if(index >= length){ |
| 151 | + perror("Specified Node with this index, doesn't exist !, ArrayIndexOutOfBoundsException"); |
| 152 | + return new Node(); |
| 153 | + } |
| 154 | + //mark the head node as the start --Shallow Copy of Data |
| 155 | + Node* node = head; |
| 156 | + //loop over the list as long as the proceeding node of this node is available |
| 157 | + while(node != NULL) |
| 158 | + { |
| 159 | + //check if indices match |
| 160 | + if(node->index == index) |
| 161 | + { |
| 162 | + return node; |
| 163 | + } |
| 164 | + //shallow copy the next node to this pointer |
| 165 | + node = node->Next; |
| 166 | + } |
| 167 | + |
| 168 | + return NULL; |
| 169 | +} |
| 170 | + |
| 171 | + |
| 172 | + |
0 commit comments