1 /***************************************************************************
2 * Copyright (C) 2007 by Prabakaran Thirumalai *
3 * praba_tuty@yahoo.com *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 ***************************************************************************/
35 ListIterator(ListNode
*head
) { iter
= head
; start
= head
; }
39 if (iter
== NULL
) return false; else return true;
46 //isRemove ->the node needs to deleted after returning
47 void* nextElement(bool isRemove
= false)
49 if (iter
== NULL
) return NULL
;
50 ListNode
*node
= iter
;
55 //index start with one, such that 1->first element in list
56 void* getElement(int index
)
58 ListNode
*localIter
= start
;
59 if (localIter
== NULL
) return NULL
;
60 for (int i
=0; i
<index
; i
++) {
61 localIter
= localIter
->next
;
62 if (localIter
== NULL
) break;
64 return localIter
->element
;
71 char name
[IDENTIFIER_LENGTH
];
78 List() { head
= NULL
;}
80 DbRetVal
append(void *elem
)
82 ListNode
*newNode
= new ListNode();
83 newNode
->element
= elem
;
85 //If this is the first node, set it as head
86 if (NULL
== head
) { head
= newNode
; return OK
; }
88 ListNode
*iter
= head
;
89 while (NULL
!= iter
->next
) iter
= iter
->next
;
93 //Warning:Try to avoid using this method while using the iterator.The behavior
94 //is undefined. Instead set flag isRemove to yes and call nextElement of iterator.
95 DbRetVal
remove(void *elem
)
99 printError(ErrNotExists
, "There are no elements in the list. Empty list");
102 ListNode
*iter
= head
, *prev
= head
;
105 if (elem
== iter
->element
)
107 prev
->next
= iter
->next
;
109 if (iter
== head
) { head
= NULL
; return OK
;}
115 printError(ErrNotFound
, "There are no elements in the list");
119 //index start with one, such that 1->first element in list
122 ListNode
*localIter
= head
;
123 if (localIter
== NULL
) return NULL
;
124 for (int i
=0; i
<index
-1; i
++) {
125 localIter
= localIter
->next
;
126 if (localIter
== NULL
) break;
128 return localIter
->element
;
132 bool exists(void *elem
)
134 ListNode
*iter
= head
;
137 if (elem
== iter
->element
)
146 ListIterator
getIterator()
148 ListIterator
iter(head
);
153 if (NULL
== head
) return;
154 ListNode
*iter
= head
, *prevIter
= head
;
155 while (iter
->next
!= NULL
)
169 if (NULL
== head
) return 0;
170 ListNode
*iter
= head
;
171 while (iter
->next
!= NULL
)
185 UniqueID() { startID
= 1; mutex
.init(); }
188 if (mutex
.getLock(-1, false) != 0) return 0;
190 mutex
.releaseLock(-1, false);