some cleanups in the clientutil.c code.
[Samba.git] / source / ubi_dLinkList.c
blob49bf495b9dc743d46d1c49f22b4801d4b9ad60e9
1 /* ========================================================================== **
2 * ubi_dLinkList.c
4 * Copyright (C) 1997 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 * Revision 0.3 1997/10/15 03:05:39 crh
28 * Added some handy type casting to the macros. Added AddHere and RemThis
29 * macros.
31 * Revision 0.2 1997/10/08 03:07:21 crh
32 * Fixed a few forgotten link-ups in Insert(), and fixed the AddHead()
33 * macro, which was passing the wrong value for <After> to Insert().
35 * Revision 0.1 1997/10/07 04:34:07 crh
36 * Initial Revision.
39 * ========================================================================== **
42 #include "ubi_dLinkList.h"
44 /* ========================================================================== **
45 * Functions...
48 ubi_dlListPtr ubi_dlInitList( ubi_dlListPtr ListPtr )
49 /* ------------------------------------------------------------------------ **
50 * Initialize a doubly-linked list header.
52 * Input: ListPtr - A pointer to the list structure that is to be
53 * initialized for use.
55 * Output: A pointer to the initialized list header (i.e., same as
56 * <ListPtr>).
58 * ------------------------------------------------------------------------ **
61 ListPtr->Head = NULL;
62 ListPtr->Tail = NULL;
63 ListPtr->count = 0;
64 return( ListPtr );
65 } /* ubi_dlInitList */
67 ubi_dlNodePtr ubi_dlInsert( ubi_dlListPtr ListPtr,
68 ubi_dlNodePtr New,
69 ubi_dlNodePtr After )
70 /* ------------------------------------------------------------------------ **
71 * Insert a new node into the list.
73 * Input: ListPtr - A pointer to the list into which the node is to
74 * be inserted.
75 * New - Pointer to the new node.
76 * After - NULL, or a pointer to a node that is already in the
77 * list.
78 * If NULL, then <New> will be added at the head of the
79 * list, else it will be added following <After>.
81 * Output: A pointer to the node that was inserted into the list (i.e.,
82 * the same as <New>).
84 * ------------------------------------------------------------------------ **
87 if( NULL == After )
89 New->Next = ListPtr->Head;
90 New->Prev = NULL;
91 if( NULL != ListPtr->Head )
92 ListPtr->Head->Prev = New;
93 else
94 ListPtr->Tail = New;
95 ListPtr->Head = New;
97 else
99 New->Next = After->Next;
100 New->Prev = After;
101 if( NULL != After->Next )
102 After->Next->Prev = New;
103 else
104 ListPtr->Tail = New;
105 After->Next = New;
108 ++(ListPtr->count);
110 return( New );
111 } /* ubi_dlInsert */
113 ubi_dlNodePtr ubi_dlRemove( ubi_dlListPtr ListPtr, ubi_dlNodePtr Old )
114 /* ------------------------------------------------------------------------ **
115 * Remove a node from the list.
117 * Input: ListPtr - A pointer to the list from which <Old> is to be
118 * removed.
119 * Old - A pointer to the node that is to be removed from the
120 * list.
122 * Output: A pointer to the node that was removed (i.e., <Old>).
124 * ------------------------------------------------------------------------ **
127 if( NULL != Old )
129 if( Old->Next )
130 Old->Next->Prev = Old->Prev;
131 else
132 ListPtr->Tail = Old->Prev;
134 if( Old->Prev )
135 Old->Prev->Next = Old->Next;
136 else
137 ListPtr->Head = Old->Next;
139 --(ListPtr->count);
142 return( Old );
143 } /* ubi_dlRemove */
146 /* ================================ The End ================================= */