Robot Agent  1.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
doublylinkedlist.c
Go to the documentation of this file.
1 
11 /* -- Includes -- */
12 /* system libraries */
13 #include <stdio.h>
14 #include <stdlib.h>
15 /* project libraries */
16 #include "doublylinkedlist.h"
17 #include "def.h"
18 #include "robot.h"
19 #include "pheromone.h"
20 
21 /* -- Defines -- */
22 
23 /* -- Functions -- */
24 
30 {
31  // Allocate memory for doubly linked list structure
32  doublylinkedlist_t *dlls = (doublylinkedlist_t *) malloc(sizeof(doublylinkedlist_t));
33 
34  // Init doubly linked list
35  dlls->first = NULL;
36  dlls->last = NULL;
37  dlls->count = 0;
38 
39  return dlls;
40 }
41 
48 {
49  // Empty the doubly linked list
51  // Free memory
52  free(dlls);
53 }
54 
63 void doublylinkedlist_insert_after(doublylinkedlist_t *dlls, doublylinkedlist_node_t *node, void *data, int data_type)
64 {
65  // Allocate memory for new node structure
67 
68  // Increase count of nodes in the doubly linked list
69  dlls->count++;
70 
71  // Set pointers to next and previous node for the new node
72  new_node->prev = node;
73  new_node->next = node->next;
74 
75  // Check if the node was in the end of the list
76  if(node->next == NULL)
77  {
78  // If yes, than it is placed as last
79  dlls->last = new_node;
80  }
81  else
82  {
83  // Otherwise next node is informed about change of its previous node
84  node->next->prev = new_node;
85  }
86  // Update nodes next node to new as it is placed in front of it
87  node->next = new_node;
88 
89  // Save data type in node
90  new_node->data_type = data_type;
91 
92  // Allocate memory for data structure and copy data
93  switch(data_type)
94  {
95  // Robot pose
97  new_node->data = (void *)malloc(sizeof(robot_t));
98  *(robot_t *)new_node->data = *(robot_t *)data;
99  break;
100  // Victim information
102  new_node->data = (void *)malloc(sizeof(victim_t));
103  *(victim_t *)new_node->data = *(victim_t *)data;
104  break;
105  // Pheromone map
107  new_node->data = (void *)malloc(sizeof(pheromone_map_sector_t));
108  *(pheromone_map_sector_t *)new_node->data = *(pheromone_map_sector_t *)data;
109  break;
110  // Command
112  new_node->data = (void *)malloc(sizeof(command_t));
113  *(command_t *)new_node->data = *(command_t *)data;
114  break;
116  new_node->data = (void *)malloc(sizeof(stream_t));
117  *(stream_t *)new_node->data = *(stream_t *)data;
118  // Other
119  default :
120  // Do nothing
121  break;
122  }
123 
124 }
125 
134 void doublylinkedlist_insert_before(doublylinkedlist_t *dlls, doublylinkedlist_node_t *node, void *data, int data_type)
135 {
136  // Allocate memory for new node structure
138 
139  // Increase count of nodes in the doubly linked list
140  dlls->count++;
141 
142  // Set pointers to next and previous node for the new node
143  new_node->prev = node->prev;
144  new_node->next = node;
145 
146  // Check if the node was in the beginning of the list
147  if(node->prev == NULL)
148  {
149  // If yes, than it is placed as last
150  dlls->first = new_node;
151  }
152  else
153  {
154  // Otherwise previous node is informed about change of its next node
155  node->prev->next = new_node;
156  }
157  // Update nodes previous node to new as it is placed in back of it
158  node->prev = new_node;
159 
160  // Save data type in node
161  new_node->data_type = data_type;
162 
163  // Allocate memory for data structure and copy data
164  switch(data_type)
165  {
166  // Robot pose
168  new_node->data = (void *)malloc(sizeof(robot_t));
169  *(robot_t *)new_node->data = *(robot_t *)data;
170  break;
171  // Victim information
173  new_node->data = (void *)malloc(sizeof(victim_t));
174  *(victim_t *)new_node->data = *(victim_t *)data;
175  break;
176  // Pheromone map
178  new_node->data = (void *)malloc(sizeof(pheromone_map_sector_t));
179  *(pheromone_map_sector_t *)new_node->data = *(pheromone_map_sector_t *)data;
180  break;
181  // Command
183  new_node->data = (void *)malloc(sizeof(command_t));
184  *(command_t *)new_node->data = *(command_t *)data;
185  break;
187  new_node->data = (void *)malloc(sizeof(stream_t));
188  *(stream_t *)new_node->data = *(stream_t *)data;
189  // Other
190  default :
191  // Do nothing
192  break;
193  }
194 
195 }
196 
197 
205 void doublylinkedlist_insert_beginning(doublylinkedlist_t *dlls, void *data, int data_type)
206 {
207 
208  // Check if there are any nodes in the doubly linked list
209  // If no, then create first node
210  if(dlls->first == NULL)
211  {
212  // Allocate memory for new node structure
214 
215  // Increase count of nodes in the doubly linked list
216  dlls->count++;
217 
218  // As this is the first and only node in the list, then it is also logically the last one too.
219  dlls->first = new_node;
220  dlls->last = new_node;
221 
222  // There are no previous or next nodes.
223  new_node->next = NULL;
224  new_node->prev = NULL;
225 
226  // Save data type in node
227  new_node->data_type = data_type;
228 
229  // Allocate memory for data structure and copy data
230  switch(data_type)
231  {
232  // Robot pose
234  new_node->data = (void *)malloc(sizeof(robot_t));
235  *(robot_t *)new_node->data = *(robot_t *)data;
236  break;
237  // Victim information
239  new_node->data = (void *)malloc(sizeof(victim_t));
240  *(victim_t *)new_node->data = *(victim_t *)data;
241  break;
242  // Pheromone map
244  new_node->data = (void *)malloc(sizeof(pheromone_map_sector_t));
245  *(pheromone_map_sector_t *)new_node->data = *(pheromone_map_sector_t *)data;
246  break;
247  // Command
249  new_node->data = (void *)malloc(sizeof(command_t));
250  *(command_t *)new_node->data = *(command_t *)data;
251  break;
253  new_node->data = (void *)malloc(sizeof(stream_t));
254  *(stream_t *)new_node->data = *(stream_t *)data;
255  // Other
256  default :
257  // Do nothing
258  break;
259  }
260  }
261  // Else place a new node before the first node
262  else
263  {
264  doublylinkedlist_insert_before(dlls, dlls->first, data, data_type);
265  }
266 }
267 
275 void doublylinkedlist_insert_end(doublylinkedlist_t *dlls, void *data, int data_type)
276 {
277  // Check if there are any nodes in the doubly linked list
278  // If no, then create first node
279  if(dlls->last == NULL)
280  {
281  doublylinkedlist_insert_beginning(dlls, data, data_type);
282  }
283  // Else place a new node after the last node
284  else
285  {
286  doublylinkedlist_insert_after(dlls, dlls->last, data, data_type);
287  }
288 }
289 
298 void doublylinkedlist_remove(doublylinkedlist_t *dlls, doublylinkedlist_node_t *node, void *data, int *data_type)
299 {
300  // Save data type
301  *data_type = node->data_type;
302  // Save data according to the data type
303  switch(*data_type)
304  {
305  // Robot pose
307  *(robot_t *)data = *(robot_t *)node->data;
308  break;
309  // Victim information
311  *(victim_t *)data = *(victim_t *)node->data;
312  break;
313  // Pheromone map
316  break;
317  // Command
319  *(command_t *)data = *(command_t *)node->data;
320  break;
322  *(stream_t *)data = *(stream_t *)node->data;
323  break;
324  // Other
325  default :
326  // Do nothing
327  break;
328  }
329 
330  // Delete the node
331  doublylinkedlist_delete(dlls, node);
332 }
333 
341 {
342  // Check if the node is the first one in the list
343  // If yes, then make next node as the first now
344  if(node->prev == NULL)
345  {
346  dlls->first = node->next;
347  }
348  // Else inform previous node that it has a new next node
349  else
350  {
351  node->prev->next = node->next;
352  }
353 
354  // Check if the node is the last one in the list
355  // If yes, then make previous node as the last now
356  if(node->next == NULL)
357  {
358  dlls->last = node->prev;
359  }
360  // Else inform next node that it has a new previous node
361  else
362  {
363  node->next->prev = node->prev;
364  }
365 
366  // Free memory (data)
367  free(node->data);
368  // Free memory (node)
369  free(node);
370  // Decrease count of nodes in the doubly linked list
371  dlls->count--;
372 }
373 
380 {
381  // Temporary pointer to first node
382  doublylinkedlist_node_t *temp_first_node;
383 
384  // Take nodes out until doubly linked list is empty
385  while(dlls->first != NULL)
386  {
387  // Save pointer to first node for later to be able to free the memory
388  temp_first_node = dlls->first;
389  // Make next node to be the first
390  dlls->first = dlls->first->next;
391  // Free memory (data)
392  free(temp_first_node->data);
393  // Free memory (node)
394  free(temp_first_node);
395  }
396 
397  // Set number of nodes in the doubly linked list to zero
398  dlls->count = 0;
399 
400  // Deinit last
401  dlls->last = NULL;
402 }
#define s_DATA_STRUCT_TYPE_ROBOT
Definition: def.h:68
doublylinkedlist_node_t * last
void doublylinkedlist_destroy(doublylinkedlist_t *dlls)
struct s_DOUBLYLINKEDLIST_NODE_STRUCT * prev
doublylinkedlist_node_t * first
doublylinkedlist_t * doublylinkedlist_init(void)
void doublylinkedlist_insert_after(doublylinkedlist_t *dlls, doublylinkedlist_node_t *node, void *data, int data_type)
Stream structure.
Definition: def.h:48
void doublylinkedlist_insert_before(doublylinkedlist_t *dlls, doublylinkedlist_node_t *node, void *data, int data_type)
Victim structure.
Definition: def.h:28
Command structure.
Definition: def.h:39
struct s_DOUBLYLINKEDLIST_NODE_STRUCT * next
Doubly linked list node structure.
#define s_DATA_STRUCT_TYPE_PHEROMONE
Definition: def.h:70
Robot structure.
Definition: robot.h:25
#define s_DATA_STRUCT_TYPE_STREAM
Definition: def.h:72
void doublylinkedlist_delete(doublylinkedlist_t *dlls, doublylinkedlist_node_t *node)
Doubly linked list structure.
void doublylinkedlist_insert_beginning(doublylinkedlist_t *dlls, void *data, int data_type)
void doublylinkedlist_insert_end(doublylinkedlist_t *dlls, void *data, int data_type)
void doublylinkedlist_empty(doublylinkedlist_t *dlls)
#define s_DATA_STRUCT_TYPE_CMD
Definition: def.h:71
#define s_DATA_STRUCT_TYPE_VICTIM
Definition: def.h:69
void doublylinkedlist_remove(doublylinkedlist_t *dlls, doublylinkedlist_node_t *node, void *data, int *data_type)
Pheromone Map Sector structure.
Definition: pheromone.h:58