-
Notifications
You must be signed in to change notification settings - Fork 0
/
dinamic_list.c
239 lines (202 loc) · 4.34 KB
/
dinamic_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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#include "dinamic_list.h"
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
List *create_list() {
List *li = (List *)malloc(sizeof(List));
if (li != NULL) {
*li = NULL;
}
return li;
}
void free_list(List *li) {
if (li != NULL) {
Elem *node;
while ((*li) != NULL) {
node = *li;
*li = (*li)->next;
free(node);
}
free(li);
}
}
int list_length(List *li) {
if (li == NULL)
return -1;
int cont = 0;
Elem *node = *li;
while (node != NULL) {
cont++;
node = node->next;
}
return cont;
}
int is_empty(List *li) {
// list content
if (*li == NULL) {
return 1;
}
// list
if (li == NULL) {
return 1;
}
return 0;
}
int append_to_start(List *li, struct student st) {
if (li == NULL)
return 0;
Elem *new_node = (Elem *)malloc(sizeof(Elem));
if (new_node == NULL)
return 0;
new_node->data = st;
new_node->next = (*li);
*li = new_node;
return 1;
}
int append_to_end(List *li, struct student st) {
if (li == NULL)
return 0;
// verifiyng if the memory allocation was succeeded
Elem *new_node = (Elem *)malloc(sizeof(Elem));
if (new_node == NULL)
return 0;
new_node->data = st;
// as we know, this item will be the last item in the list, so it needs
// points to a null value, first we need to set new_node->next == NULL.
new_node->next = NULL;
if ((*li) == NULL) {
*li = new_node;
} else {
// saving the main head instance of the list
Elem *temp = *li;
// searching for the whole list for an NULL value, that means the end of the
// linked list.
while (temp->next != NULL) {
// step ahead
temp = temp->next;
}
// poiting the last value (NULL) to the new node.
temp->next = new_node;
}
return 1;
}
int append_sorting(List *li, struct student st) {
// sort by id ascendent count
if (li == NULL)
return 0;
Elem *new_node = (Elem *)malloc(sizeof(Elem));
if (new_node == NULL)
return 0;
new_node->data = st;
new_node->next = NULL;
if (is_empty(li)) {
new_node->next = (*li);
*li = new_node;
return 1;
} else {
Elem *_prev, *_curr = *li;
while (_curr != NULL && _curr->data.id < new_node->data.id) {
// step ahead
_prev = _curr;
_curr = _curr->next;
}
if (_curr == *li) {
new_node->next = (*li);
*li = new_node;
} else {
new_node->next = _prev->next;
_prev->next = new_node;
}
}
return 1;
}
int remove_from_start(List *li) {
if (li == NULL) {
return 0;
}
if ((*li) == NULL) {
return 0;
}
// getting the first item from the list
Elem *node = *li;
// step ahead
*li = node->next;
free(node);
return 1;
}
int remove_from_end(List *li) {
if (li == NULL) {
return 0;
}
if ((*li) == NULL) {
return 0;
}
Elem *_prev_node, *_curr_node = *li;
// Enquanto o próximo elemento for diferente de NULL, dê um passo para frente
while (_curr_node->next != NULL) {
_prev_node = _curr_node;
_curr_node = _curr_node->next;
}
// Caso a lista só tenha um item
if (_curr_node == (*li)) {
*li = _curr_node->next;
// caso a lista tenha mais de 1 item
} else {
_prev_node->next = _curr_node->next;
}
free(_curr_node);
return 1;
}
int remove_by_id(List *li, int id) {
if (li == NULL) {
return 0;
}
if ((*li) == NULL) {
return 0;
}
Elem *_prev_node, *_curr_node = *li;
while (_curr_node != NULL && _curr_node->data.id != id) {
_prev_node = _curr_node;
_curr_node = _curr_node->next;
}
if (_curr_node == NULL)
return 0; // empty list or item not found.
if (_curr_node == *li) {
*li = _curr_node->next;
} else {
_prev_node->next = _curr_node->next;
}
free(_curr_node);
return 1;
}
int get_element_by_index(List *li, struct student *st, int idx) {
if (li == NULL || idx < 0)
return 0;
if ((*li) == NULL)
return -1;
int count = 0;
Elem *node = *li;
while (node != NULL && count <= idx) {
if (count == idx) {
*st = node->data;
return 1;
}
node = node->next;
count++;
}
return 0;
}
int get_element_by_id(List *li, struct student *st, int id) {
if (li == NULL)
return -1;
Elem *node = *li;
while (node != NULL && node->data.id != id) {
node = node->next;
}
if (node == NULL) {
return 0;
} else {
*st = node->data;
return 1;
}
}