-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist.c
More file actions
134 lines (114 loc) · 2.9 KB
/
list.c
File metadata and controls
134 lines (114 loc) · 2.9 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//
// Created by josoder on 20.10.17.
//
#include "list.h"
#include "common.h"
/**
* Create a new linked list
* @param list
* @param elementSize
* @param freeFunction, Needed when storing strings
*/
void list_new(list *list, int elementSize, freeFunction freeFunction)
{
assert(elementSize > 0);
list->logicalLength = 0;
list->elementSize = elementSize;
list->head = list->tail = NULL;
list->freeFn = freeFunction;
}
/**
* Free memory
* @param list
*/
void list_destroy(list *list)
{
listNode *current;
while(list->head != NULL) {
current = list->head;
list->head = current->next;
if(list->freeFn){
list->freeFn(current->data);
}
free(current->data);
free(current);
}
}
/**
* Add element in front of the list, replacing head.
* @param list
* @param element
*/
void list_add_head(list *list, void *element){
// allocate memory for the node & the element it self
listNode *front = malloc(sizeof(listNode));
front->data = malloc(list->elementSize);
// copy the raw bytes sent in with element to front->data
memcpy(front->data, element, list->elementSize);
front->next = list->head;
list->head = front;
// if the list is empty, the tail and head pointers will point to the same node
if(list->logicalLength==0){
list->tail = list->head;
}
list->logicalLength++;
}
/**
* Add element after the last element in the list, replacing the tail.
* @param list
* @return
*/
void list_add_tail(list *list, void *element) {
listNode *back = malloc(sizeof(listNode));
back->data = malloc(sizeof(list->elementSize));
memcpy(back->data, element, list->elementSize);
if(list->logicalLength==0) {
list -> tail = list -> head = back;
} else {
list->tail->next = back;
list->tail = back;
}
list->logicalLength++;
}
/**
* Copy the content of the heads data into the space specified by *element and remove it if rm is true.
* @param list
* @param element
* @param rm
*/
void list_head(list *list, void *element, bool rm){
assert(list->head != NULL);
listNode *node = list->head;
memcpy(element, node->data, list->elementSize);
if (rm){
list->head = node->next;
list->logicalLength--;
free(node->data);
free(node);
}
}
/**
* Copy content from the tail of the list to element.
* @param list
* @param element
*/
void list_tail(list *list, void *element){
assert(list->tail != NULL);
memcpy(element, list->tail->data, list->elementSize);
}
void list_for_each(list *list, listIterator listIterator){
assert(listIterator != NULL);
listNode *current = list->head;
while (current!=NULL){
listIterator(current->data);
current = current->next;
}
}
/**
* Return size of the list
* @param list
* @return
*/
int list_size(list *list) {
return list->logicalLength;
}