2 * lists.c -- Functions to implement a double linked list
3 * XBoard $Id: lists.c,v 2.1 2003/10/27 19:21:00 mann Exp $
5 * Copyright 1995 Free Software Foundation, Inc.
7 * ------------------------------------------------------------------------
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
21 * ------------------------------------------------------------------------
23 * This file could well be a part of backend.c, but I prefer it this
32 #endif /* not STDC_HEADERS */
39 /* Check, if List l is empty; returns TRUE, if it is, FALSE
45 return(l->head == (ListNode *) &l->tail);
49 /* Initialize a list. Must be executed before list is used.
54 l->head = (ListNode *) &l->tail;
56 l->tailPred = (ListNode *) l;
60 /* Remove node n from the list it is inside.
65 if (n->succ != NULL) { /* Be safe */
66 n->pred->succ = n->succ;
67 n->succ->pred = n->pred;
68 n->succ = NULL; /* Mark node as no longer being member */
69 n->pred = NULL; /* of a list. */
86 /* Create a list node with size s. Returns NULL, if out of memory.
88 ListNode *ListNodeCreate(s)
93 if ((n = (ListNode*) malloc(s))) {
94 n->succ = NULL; /* Mark node as not being member of a list. */
101 /* Insert node n into the list of node m after m.
103 void ListInsert(m, n)
113 /* Add node n to the head of list l.
115 void ListAddHead(l, n)
119 ListInsert((ListNode *) &l->head, n);
123 /* Add node n to the tail of list l.
125 void ListAddTail(l, n)
129 ListInsert((ListNode *) l->tailPred, n);
133 /* Return element with number n of list l. (NULL, if n doesn't exist.)
134 * Counting starts with 0.
136 ListNode *ListElem(l, n)
142 for (ln = l->head; ln->succ; ln = ln->succ) {