-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist.c
More file actions
80 lines (80 loc) · 1.81 KB
/
Copy pathlist.c
File metadata and controls
80 lines (80 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "list.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
extern FILE *fp;
wwList* wwList_CreateEmpty(size_t elemSize, int isMalloced, wwListFreeFunc freeFunc)
{
wwList *list = (wwList*) malloc(sizeof(wwList));
list->head = NULL;
list->numElems = 0;
list->elemSize = elemSize;
list->isMalloced = isMalloced;
list->freeFunc = freeFunc;
return list;
}
void wwList_Append(wwList *list, void *data)
{
Node *newNode = (Node*) malloc(sizeof(Node));
newNode->data = data;
newNode->next = NULL;
Node *node = list->head;
if(list->head == NULL)
{
list->head = newNode;
return;
}
while(node->next != NULL)
node = node->next;
node->next = newNode;
list->numElems++;
}
void wwList_Remove(wwList *list, void *data)
{
Node *node = list->head;
int removed = FALSE;
if(node == NULL)
return;
while(node->next != NULL)
{
if(memcmp(node->next->data, data, list->elemSize) == 0)
{
removed = TRUE;
Node *toDelete = node->next;
node->next = node->next->next;
if(list->freeFunc != NULL)
list->freeFunc(toDelete);
free(toDelete);
break;
}
node = node->next;
}
if(removed == TRUE)
list->numElems--;
}
void wwList_Delete(wwList *list)
{
if(list == NULL)
return;
Node *node = list->head;
while(node != NULL)
{
Node *nextNode = node->next;
if(list->isMalloced == TRUE)
{
if(list->freeFunc != NULL)
list->freeFunc(node->data);
free(node->data);
}
free(node);
node = nextNode;
list->numElems--;
}
free(list);
}