1 /********************************************************************
2 * Class: CList.cpp. This is part of WinUI.
4 * Purpose: A simple way to create doubly linked lists.
6 * Authors: Originally coded by Claude Catonio.
8 * License: Original code was public domain.
9 * Present revised CList classes are covered by GNU General Public License.
12 * Manu B. 10/05/01 Terms was translated to English.
13 * Manu B. 10/16/01 Add CList::Destroy(CNode *node) method.
14 * Manu B. 11/12/01 Add InsertBefore/InsertAfter methods.
15 * Manu B. 11/17/01 First() & Last() now returns an integer value.
16 * Manu B. 11/19/01 CNode::Destroy() returns next node by default.
17 * Manu B. 12/28/01 Simplify CList, add InsertSorted().
18 * Manu B. 03/13/02 Suppress CNode methods, next and prev are public.
19 * Manu B. 03/28/02 Add Compare().
21 ********************************************************************/
24 /********************************************************************
27 * Purpose: List management.
31 ********************************************************************/
33 //MessageBox (0, "CList", "destructor", MB_OK);
34 while (first
!= NULL
){
39 current
= last
= first
;
43 /********************************************************************
45 ********************************************************************/
46 CNode
* CList::First(){
51 CNode
* CList::Last(){
56 CNode
* CList::Prev(){
59 if(current
->prev
== NULL
){
64 current
= current
->prev
;
71 CNode
* CList::Next(){
74 if(current
->next
== NULL
){
79 current
= current
->next
;
86 /********************************************************************
88 ********************************************************************/
89 void CList::InsertFirst(CNode
*node
){
97 // Insert in the list.
101 // Set current node, increment the node counter.
106 void CList::InsertLast(CNode
*node
){
111 // Set node pointers.
114 // Insert in the list.
118 // Set current node, increment the node counter.
123 void CList::InsertBefore(CNode
*node
){
128 if (current
== first
)
130 // Set node pointers.
131 node
->prev
= current
->prev
;
132 node
->next
= current
;
133 // Insert in the list.
135 node
->prev
->next
= node
;
136 current
->prev
= node
;
138 // Set current node, increment the node counter.
143 void CList::InsertAfter(CNode
*node
){
150 // Set node pointers.
151 node
->prev
= current
;
152 node
->next
= current
->next
;
153 // Insert in the list.
155 node
->next
->prev
= node
;
156 current
->next
= node
;
158 // Set current node, increment the node counter.
163 bool CList::InsertSorted(CNode
* newNode
){
164 // Get the current node.
165 CNode
* currentNode
= GetCurrent();
169 // The list is empty, InsertFirst() and return.
170 InsertFirst(newNode
);
174 // Compare new node and current node data to know if we must parse Up
175 // or Down from current node.
176 cmpResult
= Compare(newNode
, currentNode
);
178 // Search Up -----------------------------------------------------------------
179 if (cmpResult
== -1){
180 // Parse the list while there's a previous node.
182 currentNode
= GetCurrent();
183 cmpResult
= Compare(newNode
, currentNode
);
186 // Correct position found.
187 InsertAfter(newNode
);
189 }else if (cmpResult
== 0){
190 // Don't add a file twice.
194 // There's no previous node, so insert first.
195 InsertFirst(newNode
);
198 // Search Down --------------------------------------------------------------
199 else if (cmpResult
== 1){
200 // Parse the list while there's a next node.
202 currentNode
= GetCurrent();
203 cmpResult
= Compare(newNode
, currentNode
);
205 if (cmpResult
== -1){
206 // Correct position found.
207 InsertBefore(newNode
);
209 }else if (cmpResult
== 0){
210 // Don't add a file twice.
214 // There's no next node, so insert last.
218 // Don't add a file twice (cmpResult == 0) -------------------------------------
223 int CList::InsertSorted_New(CNode
* newNode
){
226 /* Get the current node */
227 CNode
* currentNode
= GetCurrent();
231 /* Parse up or down ? */
232 cmpResult
= Compare(newNode
, currentNode
);
235 if (cmpResult
== -1){
236 // Parse the list while there's a previous node.
238 currentNode
= GetCurrent();
239 cmpResult
= Compare(newNode
, currentNode
);
242 // Correct position found.
244 }else if (cmpResult
== 0){
245 // Don't add a file twice.
249 // There's no previous node, so insert first.
253 }else if (cmpResult
== 1){
254 // Parse the list while there's a next node.
256 currentNode
= GetCurrent();
257 cmpResult
= Compare(newNode
, currentNode
);
259 if (cmpResult
== -1){
260 // Correct position found.
261 return INSERT_BEFORE
;
262 }else if (cmpResult
== 0){
263 // Don't add a file twice.
267 // There's no next node, so insert last.
270 // Don't add a file twice (cmpResult == 0) -------------------------------------
275 /********************************************************************
277 ********************************************************************/
278 void CList::DestroyCurrent(){
279 // CNode * node = current;
283 void CList::Destroy(CNode
* node
){
285 if (current
!= NULL
){
286 // Detach node from the list.
287 if (node
->next
!= NULL
)
288 node
->next
->prev
= node
->prev
;
289 if (node
->prev
!= NULL
)
290 node
->prev
->next
= node
->next
;
293 if(node
->next
!= NULL
)
294 current
= node
->next
;
296 current
= node
->prev
;
298 if (current
== NULL
){
299 // Now, the list is empty.
302 }else if (first
== node
){
303 // Detached node was first.
306 }else if (last
== node
){
307 // Detached node was last.
315 void CList::DestroyList(){
316 while (first
!= NULL
){
321 current
= last
= first
;