1 | #include "cr_list.h"
|
---|
2 | #include <assert.h>
|
---|
3 | #include <stdio.h>
|
---|
4 | #include <stdlib.h>
|
---|
5 | #include <string.h>
|
---|
6 |
|
---|
7 | struct CRListIterator {
|
---|
8 | void *element;
|
---|
9 | CRListIterator *prev;
|
---|
10 | CRListIterator *next;
|
---|
11 | };
|
---|
12 |
|
---|
13 | struct CRList {
|
---|
14 | CRListIterator *head;
|
---|
15 | CRListIterator *tail;
|
---|
16 | unsigned size;
|
---|
17 | };
|
---|
18 |
|
---|
19 | CRList *crAllocList( void )
|
---|
20 | {
|
---|
21 | CRList *l = malloc( sizeof( CRList ) );
|
---|
22 | assert( l );
|
---|
23 |
|
---|
24 | l->head = malloc( sizeof( CRListIterator ) );
|
---|
25 | assert( l->head );
|
---|
26 |
|
---|
27 | l->tail = malloc( sizeof( CRListIterator ) );
|
---|
28 | assert( l->tail );
|
---|
29 |
|
---|
30 | l->head->prev = NULL;
|
---|
31 | l->head->next = l->tail;
|
---|
32 |
|
---|
33 | l->tail->prev = l->head;
|
---|
34 | l->tail->next = NULL;
|
---|
35 |
|
---|
36 | l->size = 0;
|
---|
37 |
|
---|
38 | return l;
|
---|
39 | }
|
---|
40 |
|
---|
41 | void crFreeList( CRList *l )
|
---|
42 | {
|
---|
43 | CRListIterator *t1;
|
---|
44 |
|
---|
45 | assert( l != NULL );
|
---|
46 | t1 = l->head;
|
---|
47 | while ( t1 != NULL )
|
---|
48 | {
|
---|
49 | CRListIterator *t2 = t1;
|
---|
50 | t1 = t1->next;
|
---|
51 | t2->prev = NULL;
|
---|
52 | t2->next = NULL;
|
---|
53 | t2->element = NULL;
|
---|
54 | free( t2 );
|
---|
55 | }
|
---|
56 | l->size = 0;
|
---|
57 | free( l );
|
---|
58 | }
|
---|
59 |
|
---|
60 | unsigned crListSize( const CRList *l )
|
---|
61 | {
|
---|
62 | return l->size;
|
---|
63 | }
|
---|
64 |
|
---|
65 | int crListIsEmpty( const CRList *l )
|
---|
66 | {
|
---|
67 | assert( l != NULL );
|
---|
68 | return l->size == 0;
|
---|
69 | }
|
---|
70 |
|
---|
71 | void crListInsert( CRList *l, CRListIterator *iter, void *elem )
|
---|
72 | {
|
---|
73 | CRListIterator *p;
|
---|
74 |
|
---|
75 | assert( l != NULL );
|
---|
76 | assert( iter != NULL );
|
---|
77 | assert( iter != l->head );
|
---|
78 |
|
---|
79 | p = malloc( sizeof( CRListIterator ) );
|
---|
80 | assert( p != NULL );
|
---|
81 | p->prev = iter->prev;
|
---|
82 | p->next = iter;
|
---|
83 | p->prev->next = p;
|
---|
84 | iter->prev = p;
|
---|
85 |
|
---|
86 | p->element = elem;
|
---|
87 | l->size++;
|
---|
88 | }
|
---|
89 |
|
---|
90 | void crListErase( CRList *l, CRListIterator *iter )
|
---|
91 | {
|
---|
92 | assert( l != NULL );
|
---|
93 | assert( iter != NULL );
|
---|
94 | assert( iter != l->head );
|
---|
95 | assert( iter != l->tail );
|
---|
96 | assert( l->size > 0 );
|
---|
97 |
|
---|
98 | iter->next->prev = iter->prev;
|
---|
99 | iter->prev->next = iter->next;
|
---|
100 |
|
---|
101 | iter->prev = NULL;
|
---|
102 | iter->next = NULL;
|
---|
103 | iter->element = NULL;
|
---|
104 | free( iter );
|
---|
105 |
|
---|
106 | l->size--;
|
---|
107 | }
|
---|
108 |
|
---|
109 | void crListClear( CRList *l )
|
---|
110 | {
|
---|
111 | assert( l != NULL );
|
---|
112 | while ( !crListIsEmpty( l ) )
|
---|
113 | {
|
---|
114 | crListPopFront( l );
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | void crListPushBack( CRList *l, void *elem )
|
---|
119 | {
|
---|
120 | assert( l != NULL );
|
---|
121 | crListInsert( l, l->tail, elem );
|
---|
122 | }
|
---|
123 |
|
---|
124 | void crListPushFront( CRList *l, void *elem )
|
---|
125 | {
|
---|
126 | assert( l != NULL );
|
---|
127 | crListInsert( l, l->head->next, elem );
|
---|
128 | }
|
---|
129 |
|
---|
130 | void crListPopBack( CRList *l )
|
---|
131 | {
|
---|
132 | assert( l != NULL );
|
---|
133 | assert( l->size > 0 );
|
---|
134 | crListErase( l, l->tail->prev );
|
---|
135 | }
|
---|
136 |
|
---|
137 | void crListPopFront( CRList *l )
|
---|
138 | {
|
---|
139 | assert( l != NULL );
|
---|
140 | assert( l->size > 0 );
|
---|
141 | crListErase( l, l->head->next );
|
---|
142 | }
|
---|
143 |
|
---|
144 | void *crListFront( CRList *l )
|
---|
145 | {
|
---|
146 | assert( l != NULL );
|
---|
147 | assert( l->size > 0 );
|
---|
148 | assert( l->head != NULL );
|
---|
149 | assert( l->head->next != NULL );
|
---|
150 | return l->head->next->element;
|
---|
151 | }
|
---|
152 |
|
---|
153 | void *crListBack( CRList *l )
|
---|
154 | {
|
---|
155 | assert( l != NULL );
|
---|
156 | assert( l->size > 0 );
|
---|
157 | assert( l->tail != NULL );
|
---|
158 | assert( l->tail->prev != NULL );
|
---|
159 | return l->tail->prev->element;
|
---|
160 | }
|
---|
161 |
|
---|
162 | CRListIterator *crListBegin( CRList *l )
|
---|
163 | {
|
---|
164 | assert( l != NULL );
|
---|
165 | assert( l->head != NULL );
|
---|
166 | assert( l->head->next != NULL );
|
---|
167 | return l->head->next;
|
---|
168 | }
|
---|
169 |
|
---|
170 | CRListIterator *crListEnd( CRList *l )
|
---|
171 | {
|
---|
172 | assert( l != NULL );
|
---|
173 | assert( l->tail != NULL );
|
---|
174 | return l->tail;
|
---|
175 | }
|
---|
176 |
|
---|
177 | CRListIterator *crListNext( CRListIterator *iter )
|
---|
178 | {
|
---|
179 | assert( iter != NULL );
|
---|
180 | assert( iter->next != NULL );
|
---|
181 | return iter->next;
|
---|
182 | }
|
---|
183 |
|
---|
184 | CRListIterator *crListPrev( CRListIterator *iter )
|
---|
185 | {
|
---|
186 | assert( iter != NULL );
|
---|
187 | assert( iter->prev != NULL );
|
---|
188 | return iter->prev;
|
---|
189 | }
|
---|
190 |
|
---|
191 | void *crListElement( CRListIterator *iter )
|
---|
192 | {
|
---|
193 | assert( iter != NULL );
|
---|
194 | return iter->element;
|
---|
195 | }
|
---|
196 |
|
---|
197 | CRListIterator *crListFind( CRList *l, void *element, CRListCompareFunc compare )
|
---|
198 | {
|
---|
199 | CRListIterator *iter;
|
---|
200 |
|
---|
201 | assert( l != NULL );
|
---|
202 | assert( compare );
|
---|
203 |
|
---|
204 | for ( iter = crListBegin( l ); iter != crListEnd( l ); iter = crListNext( iter ) )
|
---|
205 | {
|
---|
206 | if ( compare( element, iter->element ) == 0 )
|
---|
207 | {
|
---|
208 | return iter;
|
---|
209 | }
|
---|
210 | }
|
---|
211 | return NULL;
|
---|
212 | }
|
---|
213 |
|
---|
214 | void crListApply( CRList *l, CRListApplyFunc apply, void *arg )
|
---|
215 | {
|
---|
216 | CRListIterator *iter;
|
---|
217 |
|
---|
218 | assert( l != NULL );
|
---|
219 | for ( iter = crListBegin( l ); iter != crListEnd( l ); iter = crListNext( iter ) )
|
---|
220 | {
|
---|
221 | apply( iter->element, arg );
|
---|
222 | }
|
---|
223 | }
|
---|
224 |
|
---|
225 | #if CR_TESTING_LIST
|
---|
226 |
|
---|
227 | static void printElement( void *elem, void *arg )
|
---|
228 | {
|
---|
229 | char *s = elem;
|
---|
230 | FILE *fp = arg;
|
---|
231 |
|
---|
232 | assert( s != NULL );
|
---|
233 | assert( fp != NULL );
|
---|
234 | fprintf( fp, "%s ", s );
|
---|
235 | }
|
---|
236 |
|
---|
237 | static void printList( CRList *l )
|
---|
238 | {
|
---|
239 | assert( l != NULL );
|
---|
240 | crListApply( l, printElement, stderr );
|
---|
241 | fprintf( stderr, "\n" );
|
---|
242 | }
|
---|
243 |
|
---|
244 | static int elementCompare( void *a, void *b )
|
---|
245 | {
|
---|
246 | return strcmp( a, b );
|
---|
247 | }
|
---|
248 |
|
---|
249 | int main( void )
|
---|
250 | {
|
---|
251 | char *names[] = { "a", "b", "c", "d", "e", "f" };
|
---|
252 | CRList *l;
|
---|
253 | CRListIterator *iter;
|
---|
254 | int i, n;
|
---|
255 |
|
---|
256 | n = sizeof( names ) / sizeof( names[0] );
|
---|
257 | fprintf( stderr, "n=%d\n", n );
|
---|
258 |
|
---|
259 | l = crAllocList( );
|
---|
260 | for ( i = 0; i < n; ++i )
|
---|
261 | {
|
---|
262 | crListPushBack( l, names[i] );
|
---|
263 | }
|
---|
264 | printList( l );
|
---|
265 |
|
---|
266 | crListPushFront( l, "x" );
|
---|
267 | printList( l );
|
---|
268 |
|
---|
269 | crListPushBack( l, "y" );
|
---|
270 | printList( l );
|
---|
271 |
|
---|
272 | crListPopFront( l );
|
---|
273 | printList( l );
|
---|
274 |
|
---|
275 | crListPopBack( l );
|
---|
276 | printList( l );
|
---|
277 |
|
---|
278 | iter = crListFind( l, "c", elementCompare );
|
---|
279 | assert( iter != NULL );
|
---|
280 | crListInsert( l, iter, "z" );
|
---|
281 | printList( l );
|
---|
282 |
|
---|
283 | iter = crListFind( l, "d", elementCompare );
|
---|
284 | assert( iter != NULL );
|
---|
285 | crListErase( l, iter );
|
---|
286 | printList( l );
|
---|
287 |
|
---|
288 | crListClear( l );
|
---|
289 | printList( l );
|
---|
290 | fprintf( stderr, "size: %d\n", crListSize( l ) );
|
---|
291 | fprintf( stderr, "is empty: %d\n", crListIsEmpty( l ) );
|
---|
292 |
|
---|
293 | crListPushBack( l, "w" );
|
---|
294 | crListPushBack( l, "t" );
|
---|
295 | crListPushBack( l, "c" );
|
---|
296 | printList( l );
|
---|
297 |
|
---|
298 | fprintf( stderr, "front: %s\n", ( char * ) crListFront( l ) );
|
---|
299 | fprintf( stderr, "back: %s\n", ( char * ) crListBack( l ) );
|
---|
300 | fprintf( stderr, "size: %d\n", crListSize( l ) );
|
---|
301 | fprintf( stderr, "is empty: %d\n", crListIsEmpty( l ) );
|
---|
302 |
|
---|
303 | crFreeList( l );
|
---|
304 | return 0;
|
---|
305 | }
|
---|
306 |
|
---|
307 | #endif // CR_TESTING_LIST
|
---|