0bec3323edc1ead67ae8d0663d86000acd1613de
[samba.git] / source3 / ubiqx / ubi_dLinkList.c
1 /* ========================================================================== **
2  *                              ubi_dLinkList.c
3  *
4  *  Copyright (C) 1997 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 1.1  1997/10/10 14:46:43  crh
29  * This is the ubiqx binary tree and linked list library.
30  * This library is being included as part of the Samba distribution.
31  * (Hurray!)
32  *
33  * Revision 0.1  1997/10/07 04:34:07  crh
34  * Initial Revision.
35  *
36  *
37  * ========================================================================== **
38  */
39
40 #include "ubi_dLinkList.h"
41
42 /* ========================================================================== **
43  * Functions...
44  */
45
46 ubi_dlListPtr ubi_dlInitList( ubi_dlListPtr ListPtr )
47   /* ------------------------------------------------------------------------ **
48    * Initialize a doubly-linked list header.
49    *
50    *  Input:  ListPtr - A pointer to the list structure that is to be
51    *                    initialized for use.
52    *
53    *  Output: A pointer to the initialized list header (i.e., same as
54    *          <ListPtr>).
55    *
56    * ------------------------------------------------------------------------ **
57    */
58   {
59   ListPtr->Head  = NULL;
60   ListPtr->Tail  = NULL;
61   ListPtr->count = 0;
62   return( ListPtr );
63   } /* ubi_dlInitList */
64
65 ubi_dlNodePtr ubi_dlInsert( ubi_dlListPtr ListPtr,
66                             ubi_dlNodePtr New,
67                             ubi_dlNodePtr After )
68   /* ------------------------------------------------------------------------ **
69    * Insert a new node into the list.
70    *
71    *  Input:  ListPtr - A pointer to the list into which the node is to
72    *                    be inserted.
73    *          New     - Pointer to the new node.
74    *          After   - NULL, or a pointer to a node that is already in the
75    *                    list.
76    *                    If NULL, then <New> will be added at the head of the
77    *                    list, else it will be added following <After>.
78    * 
79    *  Output: A pointer to the node that was inserted into the list (i.e.,
80    *          the same as <New>).
81    *
82    * ------------------------------------------------------------------------ **
83    */
84   {
85   if( NULL == After )
86     {
87     New->Next           = ListPtr->Head;
88     New->Prev           = NULL;
89     if( NULL != ListPtr->Head )
90       ListPtr->Head->Prev = New;
91     else
92       ListPtr->Tail       = New;
93     ListPtr->Head       = New;
94     }
95   else
96     {
97     New->Next         = After->Next;
98     New->Prev         = After;
99     if( NULL != After->Next )
100       After->Next->Prev = New;
101     else
102       ListPtr->Tail       = New;
103     After->Next       = New;
104     }
105
106   ++(ListPtr->count);
107
108   return( New );
109   } /* ubi_dlInsert */
110
111 ubi_dlNodePtr ubi_dlRemove( ubi_dlListPtr ListPtr, ubi_dlNodePtr Old )
112   /* ------------------------------------------------------------------------ **
113    * Remove a node from the list.
114    *
115    *  Input:  ListPtr - A pointer to the list from which <Old> is to be
116    *                    removed.
117    *          Old     - A pointer to the node that is to be removed from the
118    *                    list.
119    *
120    *  Output: A pointer to the node that was removed (i.e., <Old>).
121    *
122    * ------------------------------------------------------------------------ **
123    */
124   {
125   if( NULL != Old )
126     {
127     if( Old->Next )
128       Old->Next->Prev = Old->Prev;
129     else
130       ListPtr->Tail = Old->Prev;
131
132     if( Old->Prev )
133       Old->Prev->Next = Old->Next;
134     else
135       ListPtr->Head = Old->Next;
136
137     --(ListPtr->count);
138     }
139
140   return( Old );
141   } /* ubi_dlRemove */
142
143
144 /* ================================ The End ================================= */