2013-11-27 16:28:31 +01:00
|
|
|
/*
|
2013-06-18 17:21:38 +02:00
|
|
|
* Copyright (C) 2013 Freie Universität Berlin
|
2010-09-22 15:10:42 +02:00
|
|
|
*
|
2014-07-31 19:45:27 +02:00
|
|
|
* This file is subject to the terms and conditions of the GNU Lesser
|
|
|
|
* General Public License v2.1. See the file LICENSE in the top level
|
|
|
|
* directory for more details.
|
2013-11-27 16:28:31 +01:00
|
|
|
*/
|
2014-02-11 18:15:43 +01:00
|
|
|
|
2013-11-27 16:28:31 +01:00
|
|
|
/**
|
|
|
|
* @ingroup core_util
|
2010-09-22 15:10:42 +02:00
|
|
|
* @{
|
2013-11-27 16:28:31 +01:00
|
|
|
*
|
2015-05-22 07:34:41 +02:00
|
|
|
* @file
|
2013-11-27 16:28:31 +01:00
|
|
|
* @brief Circular linked list implementation
|
|
|
|
*
|
2014-01-28 11:50:12 +01:00
|
|
|
* @author Kaspar Schleiser <kaspar@schleiser.de>
|
2013-11-27 16:28:31 +01:00
|
|
|
*
|
2010-09-22 15:10:42 +02:00
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
#include "clist.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
/* inserts new_node after node */
|
2013-06-20 18:18:29 +02:00
|
|
|
void clist_add(clist_node_t **node, clist_node_t *new_node)
|
|
|
|
{
|
2013-06-24 22:37:35 +02:00
|
|
|
if (*node != NULL) {
|
2010-09-22 15:10:42 +02:00
|
|
|
new_node->next = (*node);
|
|
|
|
new_node->prev = (*node)->prev;
|
|
|
|
(*node)->prev->next = new_node;
|
|
|
|
(*node)->prev = new_node;
|
2013-06-20 18:18:29 +02:00
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if ((*node)->prev == *node) {
|
2010-09-22 15:10:42 +02:00
|
|
|
(*node)->prev = new_node;
|
|
|
|
}
|
2013-06-20 18:18:29 +02:00
|
|
|
}
|
|
|
|
else {
|
2010-09-22 15:10:42 +02:00
|
|
|
*node = new_node;
|
|
|
|
new_node->next = new_node;
|
|
|
|
new_node->prev = new_node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* removes node. */
|
2013-06-20 18:18:29 +02:00
|
|
|
void clist_remove(clist_node_t **list, clist_node_t *node)
|
|
|
|
{
|
2013-06-24 22:37:35 +02:00
|
|
|
if (node->next != node) {
|
2010-09-22 15:10:42 +02:00
|
|
|
node->prev->next = node->next;
|
|
|
|
node->next->prev = node->prev;
|
2013-06-20 18:18:29 +02:00
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (node == *list) {
|
2013-06-20 18:18:29 +02:00
|
|
|
*list = node->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2010-09-22 15:10:42 +02:00
|
|
|
*list = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-10 17:06:41 +02:00
|
|
|
#if ENABLE_DEBUG
|
2013-06-20 18:18:29 +02:00
|
|
|
void clist_print(clist_node_t *clist)
|
|
|
|
{
|
2014-04-15 16:13:26 +02:00
|
|
|
clist_node_t *start = clist, *node = start;
|
|
|
|
if (!start) {
|
|
|
|
return;
|
|
|
|
}
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2014-04-15 16:13:26 +02:00
|
|
|
do {
|
|
|
|
printf("list entry: %p: prev=%p next=%p\n", clist, clist->prev, clist->next);
|
2010-09-22 15:10:42 +02:00
|
|
|
clist = clist->next;
|
2013-06-20 18:18:29 +02:00
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (clist == start) {
|
2013-06-20 18:18:29 +02:00
|
|
|
break;
|
|
|
|
}
|
2014-04-15 16:13:26 +02:00
|
|
|
} while (node != start);
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
2013-10-10 17:06:41 +02:00
|
|
|
#endif
|