-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.c
213 lines (169 loc) · 4.88 KB
/
list.c
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "list.h"
/* 01 */
list* create_list()
{
list* to_return = malloc(sizeof(list));
if(!to_return)
{
fprintf(stderr, "\nERROR: memory allocation failed, list* creation aborted.\n\n");
to_return = NULL;
}
else
{
to_return->begin = NULL;
to_return->count = 0;
}
return to_return;
}
/* 02 */
size_t list_count(const list* _list)
{
return _list->count;
}
/* 03 */
list_node* list_get_node(list* _list, size_t _index)
{
int i = 0;
list_node* to_return = _list->begin;
/* loop until the node value is null or at the index */
while(to_return != NULL && i < _index)
{
to_return = to_return->next;
i++;
}
return to_return;
}
/* 04 */
size_t list_add(list* _list, void* _data)
{
list_node* to_add = NULL;
list_node* temp = NULL;
/* allocate node */
to_add = malloc(sizeof(list_node));
/* handle errors */
if(!to_add)
fprintf(stderr, "\nERROR: memory allocation failed, list_node* creation aborted.\n\n");
else
{
/* load data */
to_add->data = _data;
to_add->next = NULL;
/* reach last list node */
if(_list->begin != NULL)
{
temp = _list->begin;
while(temp->next != NULL)
{
temp = temp->next;
}
/* add the allocated node to list */
temp->next = to_add;
}
else
_list->begin = to_add;
/* increase list's count */
_list->count += 1;
}
return list_count(_list);
}
/* 05 */
void* list_get(list* _list, size_t _index)
{
list_node* temp = list_get_node(_list, _index);
if(temp != NULL)
return temp->data;
else
return NULL;
}
/* 06 */
bool list_set(list* _list, size_t _index, void* _data)
{
bool to_return = false;
list_node* temp = list_get_node(_list, _index);
if(temp)
{
temp->data = _data;
to_return = !to_return;
}
return to_return;
}
/* 07 */
bool list_insert(list* _list, size_t _index, void* _data)
{
bool to_return = false;
list_node* last_node = NULL;
list_node* temp = NULL;
if(_index < _list->count)
{
/* store last of the list */
last_node = list_get_node(_list, _list->count - 1);
/* add the new one */
list_add(_list, _data);
/* store the new one */
temp = list_get_node(_list, _list->count - 1);
/* remove link to list */
last_node->next = NULL;
/* link to following of the list */
temp->next = list_get_node(_list, _index);
/* link to previous elements */
if(_index == 0)
_list->begin = temp;
else
list_get_node(_list, _index - 1)->next = temp;
to_return = true;
}
/* in this case its just add -_- */
else if(_index == _list->count)
{
list_add(_list, _data);
to_return = true;
}
return to_return;
}
/* 08 */
bool list_remove(list* _list, size_t _index)
{
bool to_return = false;
list_node* temp = NULL;
if(_index < _list->count)
{
/* store the one to remove */
temp = list_get_node(_list, _index);
/* assign previous node to the next node */
if(_index == 0)
_list->begin = temp->next;
else
list_get_node(_list, _index - 1)->next = temp->next;
/* decrease list count */
_list->count -= 1;
/* free the one to delete */
free(temp);
to_return = true;
}
return to_return;
}
/* 09 */
void destroy_list(list* _list)
{
int i = 0;
while(i < _list->count)
list_remove(_list, 0);
free(_list);
}
/* 10 */
/*
The difference between dynamic array and list is that
each elemet of a list is not in line in the memory (pas a la suite).
witch means that with only one element of a list you can get all of them without manipulating all the list.
With the array, if I need to manipulate the values, I need to get them all before changing one.
With the list I can get only one and change it's values.
*/
/*
I think that in a video game for exemple, to store all the mobs we use a list instead of an array,
so that when you want to modify one, you dont have to manipulate all of them.
With an array, the problem is that you always have to import or get the all array. The problem is that in a video game, we can have a lot of ennemies at the same time.
And importing all of them everytime you need to access an enemie data, will be less optimised because it could take more time to the computer to get all of them.
*/