2020-08-03 02:11:29 +02:00
|
|
|
#include <assert.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
|
|
|
|
#include "linked_list.h"
|
|
|
|
|
|
|
|
void linked_list_init(struct linked_list *list) {
|
|
|
|
list->next = list;
|
|
|
|
list->prev = list;
|
|
|
|
}
|
|
|
|
|
|
|
|
void linked_list_insert(struct linked_list *list, struct linked_list *elem) {
|
|
|
|
assert(list->prev != NULL && list->next != NULL);
|
|
|
|
assert(elem->prev == NULL && elem->next == NULL);
|
|
|
|
|
|
|
|
elem->prev = list;
|
|
|
|
elem->next = list->next;
|
|
|
|
list->next = elem;
|
|
|
|
elem->next->prev = elem;
|
|
|
|
}
|
|
|
|
|
|
|
|
void linked_list_remove(struct linked_list *elem) {
|
|
|
|
assert(elem->prev != NULL && elem->next != NULL);
|
|
|
|
|
|
|
|
elem->prev->next = elem->next;
|
|
|
|
elem->next->prev = elem->prev;
|
|
|
|
elem->next = NULL;
|
|
|
|
elem->prev = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool linked_list_empty(struct linked_list *list) {
|
2020-08-04 14:25:07 +02:00
|
|
|
assert(list->prev != NULL && list->next != NULL);
|
2020-08-03 02:11:29 +02:00
|
|
|
return list->next == list;
|
|
|
|
}
|
2020-08-30 00:05:19 +02:00
|
|
|
|
|
|
|
void linked_list_take(struct linked_list *target, struct linked_list *source) {
|
|
|
|
if (linked_list_empty(source)) {
|
|
|
|
linked_list_init(target);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
target->next = source->next;
|
|
|
|
target->prev = source->prev;
|
|
|
|
target->next->prev = target;
|
|
|
|
target->prev->next = target;
|
|
|
|
|
|
|
|
linked_list_init(source);
|
|
|
|
}
|