1/* ========================================================================== **
2 *                              ubi_dLinkList.c
3 *
4 *  Copyright (C) 1997, 1998 by Christopher R. Hertel
5 *
6 *  Email: crh@ubiqx.mn.org
7 * -------------------------------------------------------------------------- **
8 *  This module implements simple doubly-linked lists.
9 * -------------------------------------------------------------------------- **
10 *
11 *  This library is free software; you can redistribute it and/or
12 *  modify it under the terms of the GNU Library General Public
13 *  License as published by the Free Software Foundation; either
14 *  version 2 of the License, or (at your option) any later version.
15 *
16 *  This library is distributed in the hope that it will be useful,
17 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 *  Library General Public License for more details.
20 *
21 *  You should have received a copy of the GNU Library General Public
22 *  License along with this library; if not, write to the Free
23 *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 *
25 * -------------------------------------------------------------------------- **
26 *
27 * Log: ubi_dLinkList.c,v
28 * Revision 0.11  1999/06/19 16:58:06  crh
29 * Renamed the ubi_slRemove() function in ubi_sLinkList to
30 * ubi_slRemoveNext().  I was bothered by the fact that it didn't
31 * match the functionality of the ubi_dlRemove() function in
32 * ubi_dLinkList.  The new name is more 'correct'.
33 *
34 * Revision 0.10  1998/07/24 07:30:20  crh
35 * Added the ubi_dlNewList() macro.
36 *
37 * Revision 0.9  1998/06/04 21:29:27  crh
38 * Upper-cased defined constants (eg UBI_BINTREE_H) in some header files.
39 * This is more "standard", and is what people expect.  Weird, eh?
40 *
41 * Revision 0.8  1998/06/03 18:06:03  crh
42 * Further fiddling with sys_include.h, which has been moved from the .c file
43 * to the .h file.
44 *
45 * Revision 0.7  1998/06/02 01:38:47  crh
46 * Changed include file name from ubi_null.h to sys_include.h to make it
47 * more generic.
48 *
49 * Revision 0.6  1998/05/20 04:38:05  crh
50 * The C file now includes ubi_null.h.  See ubi_null.h for more info.
51 *
52 * Revision 0.5  1998/03/10 02:55:00  crh
53 * Simplified the code and added macros for stack & queue manipulations.
54 *
55 * Revision 0.4  1998/01/03 01:53:56  crh
56 * Added ubi_dlCount() macro.
57 *
58 * Revision 0.3  1997/10/15 03:05:39  crh
59 * Added some handy type casting to the macros.  Added AddHere and RemThis
60 * macros.
61 *
62 * Revision 0.2  1997/10/08 03:07:21  crh
63 * Fixed a few forgotten link-ups in Insert(), and fixed the AddHead()
64 * macro, which was passing the wrong value for <After> to Insert().
65 *
66 * Revision 0.1  1997/10/07 04:34:07  crh
67 * Initial Revision.
68 *
69 * -------------------------------------------------------------------------- **
70 * This module is similar to the ubi_sLinkList module, but it is neither a
71 * descendant type nor an easy drop-in replacement for the latter.  One key
72 * difference is that the ubi_dlRemove() function removes the indicated node,
73 * while the ubi_slRemoveNext() function (in ubi_sLinkList) removes the node
74 * *following* the indicated node.
75 *
76 * ========================================================================== **
77 */
78
79#include "ubi_dLinkList.h"  /* Header for *this* module. */
80
81/* ========================================================================== **
82 * Functions...
83 */
84
85ubi_dlListPtr ubi_dlInitList( ubi_dlListPtr ListPtr )
86  /* ------------------------------------------------------------------------ **
87   * Initialize a doubly-linked list header.
88   *
89   *  Input:  ListPtr - A pointer to the list structure that is to be
90   *                    initialized for use.
91   *
92   *  Output: A pointer to the initialized list header (i.e., same as
93   *          <ListPtr>).
94   *
95   * ------------------------------------------------------------------------ **
96   */
97  {
98  ListPtr->Head  = NULL;
99  ListPtr->Tail  = NULL;
100  ListPtr->count = 0;
101  return( ListPtr );
102  } /* ubi_dlInitList */
103
104ubi_dlNodePtr ubi_dlInsert( ubi_dlListPtr ListPtr,
105                            ubi_dlNodePtr New,
106                            ubi_dlNodePtr After )
107  /* ------------------------------------------------------------------------ **
108   * Insert a new node into the list.
109   *
110   *  Input:  ListPtr - A pointer to the list into which the node is to
111   *                    be inserted.
112   *          New     - Pointer to the new node.
113   *          After   - NULL, or a pointer to a node that is already in the
114   *                    list.
115   *                    If NULL, then <New> will be added at the head of the
116   *                    list, else it will be added following <After>.
117   *
118   *  Output: A pointer to the node that was inserted into the list (i.e.,
119   *          the same as <New>).
120   *
121   * ------------------------------------------------------------------------ **
122   */
123  {
124  ubi_dlNodePtr PredNode = After ? After : (ubi_dlNodePtr)ListPtr;
125
126  New->Next         = PredNode->Next;
127  New->Prev         = After;
128  PredNode->Next    = New;
129  if( New->Next )
130    New->Next->Prev = New;
131  else
132    ListPtr->Tail   = New;
133
134  (ListPtr->count)++;
135
136  return( New );
137  } /* ubi_dlInsert */
138
139ubi_dlNodePtr ubi_dlRemove( ubi_dlListPtr ListPtr, ubi_dlNodePtr Old )
140  /* ------------------------------------------------------------------------ **
141   * Remove a node from the list.
142   *
143   *  Input:  ListPtr - A pointer to the list from which <Old> is to be
144   *                    removed.
145   *          Old     - A pointer to the node that is to be removed from the
146   *                    list.
147   *
148   *  Output: A pointer to the node that was removed (i.e., <Old>).
149   *
150   * ------------------------------------------------------------------------ **
151   */
152  {
153  if( Old )
154    {
155    if( Old->Next )
156      Old->Next->Prev = Old->Prev;
157    else
158      ListPtr->Tail = Old->Prev;
159
160    if( Old->Prev )
161      Old->Prev->Next = Old->Next;
162    else
163      ListPtr->Head = Old->Next;
164
165    (ListPtr->count)--;
166    }
167
168  return( Old );
169  } /* ubi_dlRemove */
170
171/* ================================ The End ================================= */
172