2016-01-16 11:44:02 +03:00
|
|
|
#include <stdlib.h>
|
2017-01-20 16:13:21 +03:00
|
|
|
#include "unicorn/platform.h"
|
2016-01-16 11:44:02 +03:00
|
|
|
#include "list.h"
|
|
|
|
|
|
|
|
// simple linked list implementation
|
|
|
|
|
|
|
|
struct list *list_new(void)
|
|
|
|
{
|
|
|
|
return calloc(1, sizeof(struct list));
|
|
|
|
}
|
|
|
|
|
|
|
|
// removed linked list nodes but does not free their content
|
|
|
|
void list_clear(struct list *list)
|
|
|
|
{
|
|
|
|
struct list_item *next, *cur = list->head;
|
|
|
|
while (cur != NULL) {
|
|
|
|
next = cur->next;
|
|
|
|
free(cur);
|
|
|
|
cur = next;
|
|
|
|
}
|
2016-01-23 05:28:17 +03:00
|
|
|
list->head = NULL;
|
|
|
|
list->tail = NULL;
|
2016-01-16 11:44:02 +03:00
|
|
|
}
|
|
|
|
|
2017-06-16 08:22:38 +03:00
|
|
|
// insert a new item at the begin of the list.
|
|
|
|
// returns generated linked list node, or NULL on failure
|
|
|
|
void *list_insert(struct list *list, void *data)
|
|
|
|
{
|
|
|
|
struct list_item *item = malloc(sizeof(struct list_item));
|
|
|
|
if (item == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
item->data = data;
|
|
|
|
item->next = list->head;
|
|
|
|
|
|
|
|
if (list->tail == NULL) {
|
|
|
|
list->tail = item;
|
|
|
|
}
|
|
|
|
|
|
|
|
list->head = item;
|
|
|
|
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
// append a new item at the end of the list.
|
2016-01-16 11:44:02 +03:00
|
|
|
// returns generated linked list node, or NULL on failure
|
|
|
|
void *list_append(struct list *list, void *data)
|
|
|
|
{
|
|
|
|
struct list_item *item = malloc(sizeof(struct list_item));
|
|
|
|
if (item == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
item->next = NULL;
|
|
|
|
item->data = data;
|
|
|
|
if (list->head == NULL) {
|
|
|
|
list->head = item;
|
|
|
|
} else {
|
|
|
|
list->tail->next = item;
|
|
|
|
}
|
|
|
|
list->tail = item;
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns true if entry was removed, false otherwise
|
|
|
|
bool list_remove(struct list *list, void *data)
|
|
|
|
{
|
|
|
|
struct list_item *next, *cur, *prev = NULL;
|
|
|
|
// is list empty?
|
|
|
|
if (list->head == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
cur = list->head;
|
|
|
|
while (cur != NULL) {
|
|
|
|
next = cur->next;
|
|
|
|
if (cur->data == data) {
|
|
|
|
if (cur == list->head) {
|
|
|
|
list->head = next;
|
2016-03-26 06:28:23 +03:00
|
|
|
} else {
|
|
|
|
prev->next = next;
|
2016-01-16 11:44:02 +03:00
|
|
|
}
|
|
|
|
if (cur == list->tail) {
|
|
|
|
list->tail = prev;
|
|
|
|
}
|
|
|
|
free(cur);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
prev = cur;
|
|
|
|
cur = next;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|