mirror of
https://github.com/hmaxnl/netex.git
synced 2025-04-19 11:38:13 +02:00
125 lines
3.0 KiB
C
Executable File
125 lines
3.0 KiB
C
Executable File
#include <errno.h>
|
|
#include "netex.h"
|
|
#include "stdlib.h"
|
|
|
|
#include "list.h"
|
|
|
|
list_node* insert_node(list_node* node, void* data)
|
|
{
|
|
list_node* newNode = (list_node*)malloc(sizeof(list_node));
|
|
if (node == NULL)
|
|
{
|
|
int error = errno;
|
|
//Failed -> log
|
|
return NULL;
|
|
}
|
|
newNode->data = data;
|
|
newNode->previous = node;
|
|
newNode->next = node->next;
|
|
node->next = newNode;
|
|
return newNode;
|
|
}
|
|
|
|
list_node* add_node(list_node* node, void* data, const bool add_end)
|
|
{
|
|
list_node* new_node = (list_node*)malloc(sizeof(list_node));
|
|
if (new_node == NULL)
|
|
{
|
|
int error = errno;
|
|
//Failed -> log
|
|
return NULL;
|
|
}
|
|
new_node->data = data;
|
|
new_node->previous = NULL;
|
|
new_node->next = NULL;
|
|
|
|
if (node != NULL)
|
|
{
|
|
if (add_end)
|
|
{
|
|
if (node->next == NULL)
|
|
node->next = new_node;
|
|
list_node* current = node->next;
|
|
for (;;)
|
|
{
|
|
if (current->next == NULL)
|
|
{
|
|
current->next = new_node;
|
|
new_node->previous = current;
|
|
break;
|
|
}
|
|
current = current->next;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (node->previous == NULL)
|
|
node->previous = new_node;
|
|
list_node* current = node->previous;
|
|
for (;;)
|
|
{
|
|
if (current->previous == NULL)
|
|
{
|
|
current->previous = new_node;
|
|
new_node->next = current;
|
|
break;
|
|
}
|
|
current = current->previous;
|
|
}
|
|
}
|
|
}
|
|
|
|
return new_node;
|
|
}
|
|
|
|
void delete_node(list_node* node)
|
|
{
|
|
if (node == NULL)
|
|
return;
|
|
if (node->previous != NULL)
|
|
node->previous->next = node->next;
|
|
if (node->next != NULL)
|
|
node->next->previous = node->previous;
|
|
free(node);
|
|
}
|
|
|
|
void iterate_list(list_node* list, const bool forward, iterate_state (*iterFunc)(const void*))
|
|
{
|
|
if (list == NULL || iterFunc == NULL)
|
|
return;
|
|
list_node * current = list;
|
|
while (current != NULL)
|
|
{
|
|
list_node* temp_next = current->next;
|
|
list_node* temp_previous = current->previous;
|
|
|
|
// State
|
|
iterate_state iter_state = iterFunc(current->data);
|
|
if (iter_state & DELETE)
|
|
delete_node(current);
|
|
if (iter_state & BREAK)
|
|
break;
|
|
|
|
if (forward)
|
|
current = temp_next;
|
|
else
|
|
current = temp_previous;
|
|
}
|
|
}
|
|
|
|
list_node* find_node(list_node* list_start, const bool forward, found_state (*findFunc)(const void*))
|
|
{
|
|
if (list_start == NULL || findFunc == NULL)
|
|
return NULL;
|
|
list_node* current = list_start;
|
|
while (current != NULL)
|
|
{
|
|
if (findFunc(current->data) == FOUND)
|
|
return current;
|
|
if (forward)
|
|
current = current->next;
|
|
else
|
|
current = current->previous;
|
|
}
|
|
return NULL;
|
|
} |