erpc/list.c

87 lines
1.7 KiB
C

#include "list.h"
#include <stdio.h>
#include <stdlib.h>
int list_node_add(list_t *list, void *data)
{
list_node_t *new_node = (list_node_t *)malloc(sizeof(list_node_t));
if (new_node == NULL)
{
return LIST_ERR_MALLOC_FAILED;
}
new_node->data = data;
new_node->next = NULL;
if (*list == NULL)
{
*list = new_node;
}
else
{
list_node_t *node = *list;
while (node->next != NULL)
{
node = node->next;
}
node->next = new_node;
}
return LIST_OK;
}
int list_node_remove(list_t *list, void *data)
{
if (*list == NULL)
{
return LIST_ERR_LIST_IS_NULL;
}
list_node_t *node = *list;
list_node_t *prev = NULL;
while (node != NULL)
{
if (node->data == data)
{
if (prev == NULL)
{
if (node->next != NULL)
{
*list = node->next;
}
else
{
*list = NULL;
}
}
else
{
if (node->next != NULL)
{
prev->next = node->next;
}
else
{
prev->next = NULL;
}
}
free(node);
return LIST_OK;
}
prev = node;
node = node->next;
}
return 0;
}
int list_size(list_t *list)
{
if (*list == NULL)
{
return -1;
}
int size = 0;
list_node_t *node = *list;
while (node != NULL)
{
size++;
node = node->next;
}
return size;
}