C_Algorithms 2.0.0
Documentation
Loading...
Searching...
No Matches
doublyLinkedList.c
Go to the documentation of this file.
1
2#include "doublyLinkedList.h"
3
7{
8 Node *node = (Node *)malloc(sizeof(Node));
9 if (node == NULL)
10 {
11 return NULL;
12 }
13
14 // Initialize node
15 node->next_ = NULL;
16 node->previous_ = NULL;
17 node->data_ = 0;
18
19 return node;
20}
21
25bool freeNode(Node *node)
26{
27 if (node == NULL)
28 {
29 return false;
30 }
31
32 free(node);
33 return true;
34}
35
39{
40 List *list = (List *)malloc(sizeof(List));
41 if (list == NULL)
42 {
43 return NULL;
44 }
45
46 list->size_ = 0;
47 list->first_ = NULL;
48 list->last_ = NULL;
49
50 return list;
51}
52
56bool freeList(List *list)
57{
58 if (list == NULL)
59 {
60 return false;
61 }
62
63 int list_size = list->size_;
64 if (list_size > 0)
65 {
66 Node *current_node;
67 for (int i = 0; i < list_size; i++)
68 {
69 current_node = list->last_;
70 if (!removeNodeFromList(list, current_node))
71 {
72 return false;
73 }
74 if (!freeNode(current_node))
75 {
76 return false;
77 }
78 }
79 }
80
81 free(list);
82 return true;
83}
84
89bool checkElementInList(Node *current_node, Node *node_to_check)
90{
91 // Check if list is empty
92 if (current_node == NULL)
93 {
94 return false;
95 }
96 // Check if the current node is the same as the one to check
97 if (current_node == node_to_check)
98 {
99 return true;
100 }
101 // Else go recursive into the function with the next element in the list
102 else
103 {
104 return checkElementInList(current_node->next_, node_to_check);
105 }
106 return false;
107}
108
115Node *appendNodeToEndOfList(List *list, Node *node, bool check_for_existing_node)
116{
117 // Element is already in the list
118 if ((check_for_existing_node ? checkElementInList(list->first_, node) : false))
119 {
120 return node;
121 }
122
123 if (list->size_ == 0)
124 {
125 list->first_ = node;
126 list->last_ = node;
127 list->size_++;
128 }
129 else
130 {
131 list->last_->next_ = node; // Set next from last element in list to the new node
132 node->previous_ = list->last_; // Set pointer to now second last element
133 list->last_ = node;
134 list->size_++;
135 }
136 return node;
137}
138
139List *appendListToEndOfList(List *list, List *list_to_append)
140{
141 if (list->size_ == 0)
142 {
143 list->first_ = list_to_append->first_;
144 list->last_ = list_to_append->last_;
145 list->size_ = list_to_append->size_;
146 }
147 else
148 {
149 list->last_->next_ = list_to_append->first_; // Set next from last element in list to the new node
150 list_to_append->first_->previous_ = list->last_;
151 list->last_ = list_to_append->last_;
152 list->size_ = list->size_ + list_to_append->size_;
153 }
154 list_to_append->first_ = NULL;
155 list_to_append->last_ = NULL;
156 list_to_append->size_ = 0;
157 return list;
158}
159
164bool removeNodeFromList(List *list, Node *node)
165{
166 if (list->size_ == 0)
167 {
168 return NULL;
169 }
170
171 if (node->next_ == NULL && node->previous_ == NULL)
172 {
173 list->first_ = NULL;
174 list->last_ = NULL;
175 list->size_--;
176
177 return true;
178 }
179 else if (node->next_ == NULL) // Node is the last element in the list
180 {
181 list->last_ = node->previous_; // Set last list element to the one before this node
182 node->previous_->next_ = NULL; // Set the pointer from the second last node to this to NULL
183 list->size_--;
184
185 return true;
186 }
187 else if (node->previous_ == NULL) // Node is first element in list
188 {
189 list->first_ = node->next_; // Set first list element to the one after this node
190 node->next_->previous_ = NULL; // Set the pointer from the second node to this to NULL
191 list->size_--;
192
193 return true;
194 }
195 else
196 {
197 node->next_->previous_ = node->previous_;
198 node->previous_->next_ = node->next_;
199 list->size_--;
200
201 return true;
202 }
203 return false;
204}
205
211{
212 Node *current_node = list->first_;
213 while (current_node != NULL)
214 {
215 if (current_node->data_ == data)
216 {
217 if (current_node == list->first_)
218 {
219 list->first_ = current_node->next_;
220 }
221 else
222 {
223 current_node->previous_->next_ = current_node->next_;
224 }
225 if (current_node == list->last_)
226 {
227 list->last_ = current_node->previous_;
228 }
229 else
230 {
231 current_node->next_->previous_ = current_node->previous_;
232 }
233 if(list->first_ != NULL)
234 {
235 list->first_->previous_ = NULL;
236 }
237 if(list->last_ != NULL)
238 {
239 list->last_->next_ = NULL;
240 }
241
242 list->size_--;
243 current_node->previous_ = NULL;
244 current_node->next_ = NULL;
245 return current_node;
246 }
247 current_node = current_node->next_;
248 }
249 return NULL; // value not found
250}
251
256{
257 if (list->size_ == 0)
258 {
259 return NULL;
260 }
261
262 Node *removed = list->first_;
263 Node *second = list->first_->next_;
264 list->first_ = second;
265 list->size_--;
266 return removed;
267}
268
271void printList(List *list)
272{
273 Node *current_node = list->first_;
274 int count = 0;
275 while (current_node != NULL)
276 {
277 // Example print if data is an integer
278 printf("List element %d, value: %d\n", count, current_node->data_);
279 current_node = current_node->next_;
280 count ++;
281 }
282}
Node * appendNodeToEndOfList(List *list, Node *node, bool check_for_existing_node)
Append a node to the end of a list.
Node * createNode(void)
Allocates memory for a new node and initalizes it.
Node * removeNodeWithValue(List *list, int data)
Remove a node from a list with a specific value of playing_card_.
bool removeNodeFromList(List *list, Node *node)
Removes a node from the list.
void printList(List *list)
Print the whole List without trailing .
bool freeNode(Node *node)
Frees the memory of a node.
bool freeList(List *list)
Frees the memory of a list and all nodes in it.
List * createList(void)
Allocates memory for a new list and initalizes it.
List * appendListToEndOfList(List *list, List *list_to_append)
Node * remove_first(List *list)
Remove first node in list.
bool checkElementInList(Node *current_node, Node *node_to_check)
Checks if the adress of a node is already in a list (rekursive)
Node * last_
Node * first_
Node * next_
Node * previous_