| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- /**
- * @file LinkedList1.h
- * @author Ambroz Bizjak <ambrop7@gmail.com>
- *
- * @section LICENSE
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. Neither the name of the author nor the
- * names of its contributors may be used to endorse or promote products
- * derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
- * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- * @section DESCRIPTION
- *
- * Simple doubly linked list.
- */
- #ifndef BADVPN_STRUCTURE_LINKEDLIST1_H
- #define BADVPN_STRUCTURE_LINKEDLIST1_H
- #include <stddef.h>
- #include <misc/debug.h>
- /**
- * Linked list node.
- */
- typedef struct LinkedList1Node_t
- {
- struct LinkedList1Node_t *p;
- struct LinkedList1Node_t *n;
- } LinkedList1Node;
- /**
- * Simple doubly linked list.
- */
- typedef struct
- {
- LinkedList1Node *first;
- LinkedList1Node *last;
- } LinkedList1;
- /**
- * Initializes the linked list.
- *
- * @param list list to initialize
- */
- static void LinkedList1_Init (LinkedList1 *list);
- /**
- * Determines if the list is empty.
- *
- * @param list the list
- * @return 1 if empty, 0 if not
- */
- static int LinkedList1_IsEmpty (LinkedList1 *list);
- /**
- * Returns the first node of the list.
- *
- * @param list the list
- * @return first node of the list, or NULL if the list is empty
- */
- static LinkedList1Node * LinkedList1_GetFirst (LinkedList1 *list);
- /**
- * Returns the last node of the list.
- *
- * @param list the list
- * @return last node of the list, or NULL if the list is empty
- */
- static LinkedList1Node * LinkedList1_GetLast (LinkedList1 *list);
- /**
- * Inserts a node to the beginning of the list.
- *
- * @param list the list
- * @param node uninitialized node to insert
- */
- static void LinkedList1_Prepend (LinkedList1 *list, LinkedList1Node *node);
- /**
- * Inserts a node to the end of the list.
- *
- * @param list the list
- * @param node uninitialized node to insert
- */
- static void LinkedList1_Append (LinkedList1 *list, LinkedList1Node *node);
- /**
- * Inserts a node before a given node.
- *
- * @param list the list
- * @param node uninitialized node to insert
- * @param target node in the list to insert before
- */
- static void LinkedList1_InsertBefore (LinkedList1 *list, LinkedList1Node *node, LinkedList1Node *target);
- /**
- * Inserts a node after a given node.
- *
- * @param list the list
- * @param node uninitialized node to insert
- * @param target node in the list to insert after
- */
- static void LinkedList1_InsertAfter (LinkedList1 *list, LinkedList1Node *node, LinkedList1Node *target);
- /**
- * Removes a node from the list.
- *
- * @param list the list
- * @param node node to remove
- */
- static void LinkedList1_Remove (LinkedList1 *list, LinkedList1Node *node);
- /**
- * Returns the next node of a given node.
- *
- * @param node reference node
- * @return next node, or NULL if none
- */
- static LinkedList1Node * LinkedList1Node_Next (LinkedList1Node *node);
- /**
- * Returns the previous node of a given node.
- *
- * @param node reference node
- * @return previous node, or NULL if none
- */
- static LinkedList1Node * LinkedList1Node_Prev (LinkedList1Node *node);
- void LinkedList1_Init (LinkedList1 *list)
- {
- list->first = NULL;
- list->last = NULL;
- }
- int LinkedList1_IsEmpty (LinkedList1 *list)
- {
- return (!list->first);
- }
- LinkedList1Node * LinkedList1_GetFirst (LinkedList1 *list)
- {
- return (list->first);
- }
- LinkedList1Node * LinkedList1_GetLast (LinkedList1 *list)
- {
- return (list->last);
- }
- void LinkedList1_Prepend (LinkedList1 *list, LinkedList1Node *node)
- {
- node->p = NULL;
- node->n = list->first;
- if (list->first) {
- list->first->p = node;
- } else {
- list->last = node;
- }
- list->first = node;
- }
- void LinkedList1_Append (LinkedList1 *list, LinkedList1Node *node)
- {
- node->p = list->last;
- node->n = NULL;
- if (list->last) {
- list->last->n = node;
- } else {
- list->first = node;
- }
- list->last = node;
- }
- void LinkedList1_InsertBefore (LinkedList1 *list, LinkedList1Node *node, LinkedList1Node *target)
- {
- node->p = target->p;
- node->n = target;
- if (target->p) {
- target->p->n = node;
- } else {
- list->first = node;
- }
- target->p = node;
- }
- void LinkedList1_InsertAfter (LinkedList1 *list, LinkedList1Node *node, LinkedList1Node *target)
- {
- node->p = target;
- node->n = target->n;
- if (target->n) {
- target->n->p = node;
- } else {
- list->last = node;
- }
- target->n = node;
- }
- void LinkedList1_Remove (LinkedList1 *list, LinkedList1Node *node)
- {
- // remove from list
- if (node->p) {
- node->p->n = node->n;
- } else {
- list->first = node->n;
- }
- if (node->n) {
- node->n->p = node->p;
- } else {
- list->last = node->p;
- }
- }
- LinkedList1Node * LinkedList1Node_Next (LinkedList1Node *node)
- {
- return node->n;
- }
- LinkedList1Node * LinkedList1Node_Prev (LinkedList1Node *node)
- {
- return node->p;
- }
- #endif
|