erpc/list.c

91 lines
1.6 KiB
C
Raw Normal View History

2024-11-20 15:03:26 +00:00
#include "list.h"
#include "stdio.h"
#include "stdlib.h"
#include "config.h"
2024-11-20 15:03:26 +00:00
list_t *list_create(void *data)
{
list_t *node = (list_t *)malloc(sizeof(list_t));
if (node == NULL)
{
return NULL;
}
node->data = data;
node->next = NULL;
return node;
}
list_t *list_append(list_t *head, void *data)
{
list_t *node = list_create(data);
if (node == NULL)
{
return NULL;
}
if (head == NULL)
{
return NULL;
}
else
{
list_t *tail = head;
while (tail->next != NULL)
{
tail = tail->next;
}
tail->next = node;
}
return head;
}
void list_print(list_t *head)
{
list_t *node = head;
while (node != NULL)
{
ERPC_DEBUG("%p\n", node->data);
2024-11-20 15:03:26 +00:00
node = node->next;
}
}
/**
* Destroy a list and all its nodes.
* @param head The head of the list.
*/
void list_destroy(list_t *head)
{
list_t *node = head;
while (node != NULL)
{
list_t *next = node->next;
free(node);
node = next;
}
}
/**
* Delete a node from the list.
* @param head The head of the list.
*/
void list_delete(list_t *head, void *data)
{
list_t *node = head;
list_t *prev = NULL;
while (node != NULL)
{
if (node->data == data)
{
if (prev == NULL)
{
head = node->next;
}
else
{
prev->next = node->next;
}
free(node);
return;
}
prev = node;
node = node->next;
}
}