cLinkedList
A simple C library for doubly linked list creation and management
Loading...
Searching...
No Matches
cLinkedList.c
Go to the documentation of this file.
1
6#include <stdio.h>
7#include <stddef.h>
8#include <stdlib.h>
9#include "../include/cLinkedList.h"
10#include "../include/macrodef.h"
11
12#ifdef __cplusplus
13extern "C" {
14#endif
15
16/********************************/
17/* Linked list status functions */
18/********************************/
19
27unsigned long cListLength (cLinkedList *myList)
28{
29 if (myList == NULL) {
30 fprintf(ERRSTREAM, "cLinkedList (cListLength): WARNING - Quashed attempt to access NULL pointer.\n");
31 return 0;
32 }
33 return myList->num_elem;
34}
35
44{
45 if (myList == NULL) {
46 fprintf(ERRSTREAM, "cLinkedList (cListEmpty): WARNING - Quashed attempt to access NULL pointer.\n");
47 return FALSE;
48 }else if (myList->num_elem > 0) {
49 return FALSE;
50 }
51 return TRUE;
52}
53
54
55/******************************/
56/* Linked list read functions */
57/******************************/
67{
68 if (myList == NULL) {
69 fprintf(ERRSTREAM, "cLinkedList (cListFirstElem): WARNING - Quashed attempt to access NULL pointer.\n");
70 return NULL;
71 }
72 return myList->anchor.nextElem;
73}
74
84{
85 if (myList == NULL) {
86 fprintf(ERRSTREAM, "cLinkedList (cListLastElem): WARNING - Quashed attempt to access NULL pointer.\n");
87 return NULL;
88 }
89 return myList->anchor.prevElem;
90}
91
102{
103 if (myList == NULL || myElem == NULL) {
104 if (myList == NULL)
105 fprintf(ERRSTREAM, "cLinkedList (cListNextElem): WARNING - Quashed attempt to access NULL pointer.\n");
106 return NULL;
107 } else if ((myList->num_elem == 0) || (myList->anchor.prevElem == myElem)) {
108 return NULL;
109 }
110 return myElem->nextElem;
111}
112
123{
124 if (myList == NULL || myElem == NULL) {
125 if(myList == NULL)
126 fprintf(ERRSTREAM, "cLinkedList (cListPrevElem): WARNING - Quashed attempt to access NULL pointer.\n");
127 return NULL;
128 } else if ((myList->num_elem == 0) || (myList->anchor.nextElem == myElem)) {
129 return NULL;
130 }
131 return myElem->prevElem;
132}
133
145cListElem * cListFindElem (cLinkedList *myList, void *myData)
146{
147 if (myList == NULL) {
148 fprintf(ERRSTREAM, "cLinkedList (cListFindElem): WARNING - Quashed attempt to access NULL pointer.\n");
149 return NULL;
150 }
151 cListElem *myElem = NULL;
152 for (myElem = cListFirstElem(myList); myElem != NULL ; myElem = cListNextElem(myList, myElem)) {
153 if (myElem->obj == myData) {
154 return myElem;
155 }
156 }
157 return myElem;
158}
159
171void * cListFindData (cLinkedList *myList, void *myData)
172{
173 if (myList == NULL) {
174 fprintf(ERRSTREAM, "cLinkedList (cListFindData): WARNING - Quashed attempt to access NULL pointer.\n");
175 return NULL;
176 }
177 cListElem *elem = cListFindElem(myList, myData);
178 if (elem == NULL)
179 return NULL;
180 else
181 return elem->obj;
182}
183
193{
194 if (myList == NULL) {
195 fprintf(ERRSTREAM, "cLinkedList (cListFirstData): WARNING - Quashed attempt to access NULL pointer.\n");
196 return NULL;
197 }
198 cListElem *elem = (cListFirstElem(myList));
199 if (elem == NULL)
200 return NULL;
201 else
202 return elem->obj;
203}
204
214{
215 if (myList == NULL) {
216 fprintf(ERRSTREAM, "cLinkedList (cListLastData): WARNING - Quashed attempt to access NULL pointer.\n");
217 return NULL;
218 }
219 cListElem *elem = (cListLastElem(myList));
220 if (elem == NULL)
221 return NULL;
222 else
223 return elem->obj;
224}
225
236void * cListNextData (cLinkedList *myList, void *myData)
237{
238 if (myList == NULL) {
239 fprintf(ERRSTREAM, "cLinkedList (cListNextData): WARNING - Quashed attempt to access NULL pointer.\n");
240 return NULL;
241 }
242 cListElem *elem = cListNextElem(myList, cListFindElem(myList, myData));
243 if (elem == NULL)
244 return NULL;
245 else
246 return elem->obj;
247}
248
258void * cListPrevData(cLinkedList *myList, void *myData)
259{
260 if (myList == NULL) {
261 fprintf(ERRSTREAM, "cLinkedList (cListPrevData): WARNING - Quashed attempt to access NULL pointer.\n");
262 return NULL;
263 }
264 cListElem *elem = cListPrevElem(myList, cListFindElem(myList, myData));
265 if (elem == NULL)
266 return NULL;
267 else
268 return elem->obj;
269}
270
271
272/**********************************/
273/* Linked list creation functions */
274/**********************************/
283{
284 if (myList == NULL) {
285 fprintf(ERRSTREAM, "cLinkedList (cListInit): WARNING - Quashed attempt to access NULL pointer.\n");
286 return 0;
287 }
288
289 myList->num_elem = 0;
290
291 myList->anchor.prevElem = NULL;
292 myList->anchor.nextElem = NULL;
293 myList->anchor.obj = NULL;
294
295 return 1;
296}
297
307int cListInsertAfter (cLinkedList *myList, void *newData, cListElem *elem)
308{
309 if (myList == NULL) {
310 fprintf(ERRSTREAM, "cLinkedList (cListInsertAfter): WARNING - Quashed attempt to access NULL pointer.\n");
311 return 0;
312 }
313 cListElem *newElem = (cListElem *)malloc(sizeof(cListElem));
314 if (cListEmpty(myList)) {
315 myList->anchor.prevElem = newElem;
316 myList->anchor.nextElem = newElem;
317 newElem->prevElem = newElem;
318 newElem->nextElem = &(myList->anchor);
319 } else {
320 newElem->prevElem = elem;
321 newElem->nextElem = elem->nextElem;
322 (elem->nextElem)->prevElem = newElem;
323 elem->nextElem = newElem;
324 }
325 newElem->obj = newData;
326 myList->num_elem += 1;
327 return 1;
328}
329
339int cListInsertBefore (cLinkedList *myList, void *newData, cListElem *elem)
340{
341 if (myList == NULL) {
342 fprintf(ERRSTREAM, "cLinkedList (cListInsertAfter): WARNING - Quashed attempt to access NULL pointer.\n");
343 return 0;
344 }
345 cListElem *newElem = (cListElem *)malloc(sizeof(cListElem));
346 if (cListEmpty(myList)) {
347 myList->anchor.prevElem = newElem;
348 myList->anchor.nextElem = newElem;
349 newElem->prevElem = newElem;
350 newElem->nextElem = &(myList->anchor);
351 } else {
352 newElem->prevElem = elem->prevElem;
353 newElem->nextElem = elem;
354 (elem->prevElem)->nextElem = newElem;
355 elem->prevElem = newElem;
356 }
357 newElem->obj = newData;
358 myList->num_elem += 1;
359 return 1;
360}
361
370int cListAppend (cLinkedList *myList, void *newData)
371{
372 return cListInsertAfter(myList, newData, myList->anchor.prevElem);
373}
374
383int cListPrepend (cLinkedList *myList, void *newData) {
384 return cListInsertBefore(myList, newData, myList->anchor.nextElem);
385}
386
387/********************************/
388/* Linked list unlink functions */
389/********************************/
397 void cListUnlinkElem (cLinkedList *myList, cListElem *delElem)
398{
399 if (myList->num_elem > 0 && delElem != NULL) {
400 (delElem->prevElem)->nextElem = delElem->nextElem;
401 (delElem->nextElem)->prevElem = delElem->prevElem;
402 myList->num_elem -= 1;
403
404 free(delElem);
405 delElem = NULL;
406 } else if (delElem == NULL) {
407 fprintf(ERRSTREAM, "cListUnlinkElem (cLinkedList): WARNING - Quashed attempt to de-reference a NULL pointer!\n");
408 } else {
409 fprintf(ERRSTREAM, "cListUnlinkElem (cLinkedList): WARNING - Quashed attempt to access empty linked list.\n");
410 }
411
412 if (cListEmpty(myList)) {
413 cListInit(myList);
414 }
415}
416
425{
426 cListElem *elem = cListFirstElem(myList);
427 cListElem *nextElem = cListNextElem(myList, elem);
428
429 while (elem != NULL) {
430 cListUnlinkElem(myList, elem);
431 elem = nextElem;
432 nextElem = cListNextElem(myList, elem);
433 }
434}
435
443void cListUnlinkData (cLinkedList *delList, void *delData)
444{
445 cListUnlinkElem(delList, cListFindElem(delList, delData));
446}
447
448#ifdef __cplusplus
449}
450#endif
451
int cListAppend(cLinkedList *myList, void *newData)
Append a data point into a linked list.
Definition: cLinkedList.c:370
cListElem * cListNextElem(cLinkedList *myList, cListElem *myElem)
Returns the next element of a linked list when provided an element from the list.
Definition: cLinkedList.c:101
void * cListLastData(cLinkedList *myList)
Returns the last data point of a linked list.
Definition: cLinkedList.c:213
void * cListFirstData(cLinkedList *myList)
Returns the first data point of a linked list.
Definition: cLinkedList.c:192
int cListEmpty(cLinkedList *myList)
Definition: cLinkedList.c:43
cListElem * cListFindElem(cLinkedList *myList, void *myData)
Find an element in a linked list passed using the data linked in it.
Definition: cLinkedList.c:145
void * cListFindData(cLinkedList *myList, void *myData)
Find a data point in a linked list if it is present.
Definition: cLinkedList.c:171
cListElem * cListFirstElem(cLinkedList *myList)
Returns the first element of a linked list.
Definition: cLinkedList.c:66
void * cListPrevData(cLinkedList *myList, void *myData)
Returns the previous data point of a linked list, when provided a data point from the list.
Definition: cLinkedList.c:258
cListElem * cListPrevElem(cLinkedList *myList, cListElem *myElem)
Returns the previous element of a linked list, when provided an element from the list.
Definition: cLinkedList.c:122
int cListInsertAfter(cLinkedList *myList, void *newData, cListElem *elem)
Insert a data point into a linked list after an existing element in the list.
Definition: cLinkedList.c:307
unsigned long cListLength(cLinkedList *myList)
Definition: cLinkedList.c:27
int cListInsertBefore(cLinkedList *myList, void *newData, cListElem *elem)
Insert a data point into a linked list before an existing element in the list.
Definition: cLinkedList.c:339
void cListUnlinkElem(cLinkedList *myList, cListElem *delElem)
Unlinks an element from a linked list. This does not remove the actual data point linked.
Definition: cLinkedList.c:397
void * cListNextData(cLinkedList *myList, void *myData)
Returns the next data point of a linked list when provided a data point from the list.
Definition: cLinkedList.c:236
void cListUnlinkData(cLinkedList *delList, void *delData)
Unlinks a data point that is linked in a linked list.
Definition: cLinkedList.c:443
cListElem * cListLastElem(cLinkedList *myList)
Returns the last element of a linked list.
Definition: cLinkedList.c:83
void cListUnlinkAll(cLinkedList *myList)
Unlinks all the elements from a linked list. This does not remove the actual data points linked.
Definition: cLinkedList.c:424
int cListInit(cLinkedList *myList)
Initializes a doubly linked list.
Definition: cLinkedList.c:282
int cListPrepend(cLinkedList *myList, void *newData)
Prepend a data point into a linked list.
Definition: cLinkedList.c:383
#define NULL
Definition: macrodef.h:19
#define ERRSTREAM
Definition: macrodef.h:82
#define TRUE
Definition: macrodef.h:24
#define FALSE
Definition: macrodef.h:23
cListElem anchor
Definition: cLinkedList.h:38
unsigned long num_elem
Definition: cLinkedList.h:36
struct _cListElem * prevElem
Definition: cLinkedList.h:28
struct _cListElem * nextElem
Definition: cLinkedList.h:26
void * obj
Definition: cLinkedList.h:24