This commit was manufactured by cvs2svn to create tag
[Samba/gbeck.git] / source / ubiqx / ubi_dLinkList.c
blobc780bd1df84b6a4675aeaf901989c7a3567eadf8
1 /* ========================================================================== **
2 * ubi_dLinkList.c
4 * Copyright (C) 1997, 1998 by Christopher R. Hertel
6 * Email: crh@ubiqx.mn.org
7 * -------------------------------------------------------------------------- **
8 * This module implements simple doubly-linked lists.
9 * -------------------------------------------------------------------------- **
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.
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.
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.
25 * -------------------------------------------------------------------------- **
27 * Log: ubi_dLinkList.c,v
28 * Revision 0.10 1998/07/24 07:30:20 crh
29 * Added the ubi_dlNewList() macro.
31 * Revision 0.9 1998/06/04 21:29:27 crh
32 * Upper-cased defined constants (eg UBI_BINTREE_H) in some header files.
33 * This is more "standard", and is what people expect. Weird, eh?
35 * Revision 0.8 1998/06/03 18:06:03 crh
36 * Further fiddling with sys_include.h, which has been moved from the .c file
37 * to the .h file.
39 * Revision 0.7 1998/06/02 01:38:47 crh
40 * Changed include file name from ubi_null.h to sys_include.h to make it
41 * more generic.
43 * Revision 0.6 1998/05/20 04:38:05 crh
44 * The C file now includes ubi_null.h. See ubi_null.h for more info.
46 * Revision 0.5 1998/03/10 02:55:00 crh
47 * Simplified the code and added macros for stack & queue manipulations.
49 * Revision 0.4 1998/01/03 01:53:56 crh
50 * Added ubi_dlCount() macro.
52 * Revision 0.3 1997/10/15 03:05:39 crh
53 * Added some handy type casting to the macros. Added AddHere and RemThis
54 * macros.
56 * Revision 0.2 1997/10/08 03:07:21 crh
57 * Fixed a few forgotten link-ups in Insert(), and fixed the AddHead()
58 * macro, which was passing the wrong value for <After> to Insert().
60 * Revision 0.1 1997/10/07 04:34:07 crh
61 * Initial Revision.
63 * -------------------------------------------------------------------------- **
64 * This module is similar to the ubi_sLinkList module, but it is neither a
65 * descendant type nor an easy drop-in replacement for the latter. One key
66 * difference is that the ubi_dlRemove() function removes the indicated node,
67 * while the ubi_slRemove() function (in ubi_sLinkList) removes the node
68 * *following* the indicated node.
70 * ========================================================================== **
73 #include "ubi_dLinkList.h" /* Header for *this* module. */
75 /* ========================================================================== **
76 * Functions...
79 ubi_dlListPtr ubi_dlInitList( ubi_dlListPtr ListPtr )
80 /* ------------------------------------------------------------------------ **
81 * Initialize a doubly-linked list header.
83 * Input: ListPtr - A pointer to the list structure that is to be
84 * initialized for use.
86 * Output: A pointer to the initialized list header (i.e., same as
87 * <ListPtr>).
89 * ------------------------------------------------------------------------ **
92 ListPtr->Head = NULL;
93 ListPtr->Tail = NULL;
94 ListPtr->count = 0;
95 return( ListPtr );
96 } /* ubi_dlInitList */
98 ubi_dlNodePtr ubi_dlInsert( ubi_dlListPtr ListPtr,
99 ubi_dlNodePtr New,
100 ubi_dlNodePtr After )
101 /* ------------------------------------------------------------------------ **
102 * Insert a new node into the list.
104 * Input: ListPtr - A pointer to the list into which the node is to
105 * be inserted.
106 * New - Pointer to the new node.
107 * After - NULL, or a pointer to a node that is already in the
108 * list.
109 * If NULL, then <New> will be added at the head of the
110 * list, else it will be added following <After>.
112 * Output: A pointer to the node that was inserted into the list (i.e.,
113 * the same as <New>).
115 * ------------------------------------------------------------------------ **
118 ubi_dlNodePtr PredNode = After ? After : (ubi_dlNodePtr)ListPtr;
120 New->Next = PredNode->Next;
121 New->Prev = After;
122 PredNode->Next = New;
123 if( New->Next )
124 New->Next->Prev = New;
125 else
126 ListPtr->Tail = New;
128 (ListPtr->count)++;
130 return( New );
131 } /* ubi_dlInsert */
133 ubi_dlNodePtr ubi_dlRemove( ubi_dlListPtr ListPtr, ubi_dlNodePtr Old )
134 /* ------------------------------------------------------------------------ **
135 * Remove a node from the list.
137 * Input: ListPtr - A pointer to the list from which <Old> is to be
138 * removed.
139 * Old - A pointer to the node that is to be removed from the
140 * list.
142 * Output: A pointer to the node that was removed (i.e., <Old>).
144 * ------------------------------------------------------------------------ **
147 if( Old )
149 if( Old->Next )
150 Old->Next->Prev = Old->Prev;
151 else
152 ListPtr->Tail = Old->Prev;
154 if( Old->Prev )
155 Old->Prev->Next = Old->Next;
156 else
157 ListPtr->Head = Old->Next;
159 (ListPtr->count)--;
162 return( Old );
163 } /* ubi_dlRemove */
165 /* ================================ The End ================================= */