90 lines
1.5 KiB
C
90 lines
1.5 KiB
C
#include "list.h"
|
|
#include "stdio.h"
|
|
|
|
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)
|
|
{
|
|
printf("%p\n", node->data);
|
|
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;
|
|
}
|
|
}
|