-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdouble_linked_list_opt.c
389 lines (329 loc) · 10.1 KB
/
double_linked_list_opt.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/**
* @file double_linked_list_opt.c
* @brief Breve descrizione del file.
*
*
*
* @authors Simone Arcari
* @date 2023-09-09 (nel formato YYYY-MM-DD)
*/
/*
NOTA: in this implementation of lists we use the assert.h library for
carry out all the checks on the parameters passed to the functions.
Furthermore, the list is formed by a sentinel node of type *node_t which
contains the pointer to the first node in the list.
*/
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <netinet/in.h> // includo la libreria per sockaddr_in
#include <arpa/inet.h> // includo la libreria per inet_ntoa() e ntohs()
#include "double_linked_list_opt.h"
bool is_empty(list_t list) {
/*******************************************************************************
*
* Description:
* true if the list is empty, false otherwise.
*
* Parameters:
* [in] head: pointer to the sentinel node
* [out] return bool
*
******************************************************************************/
node_t **head = list;
assert(head!=NULL);
return *head==NULL;
}
bool is_not_finish(node_t *pos) {
/*******************************************************************************
*
* Description:
* true if it is not the last node, false otherwise.
*
* Parameters:
* [in] pos: pointer to node
* [out] return bool
*
******************************************************************************/
assert(pos!=NULL);
return pos->next!=NULL;
}
bool is_finish(node_t *pos) {
/*******************************************************************************
*
* Description:
* true if it is the last node, false otherwise.
*
* Parameters:
* [in] pos: pointer to node
* [out] return bool
*
******************************************************************************/
assert(pos!=NULL);
return pos->next==NULL;
}
node_t *create_list(void) {
/*******************************************************************************
*
* Description:
* creates a list by allocating dynamic memory in the heap and returns
* the pointer to the allocated memory area.
*
* Parameters:
* [out] return head: pointer to the sentinel node
*
******************************************************************************/
node_t *head = malloc(sizeof(node_t*));
return head;
}
node_t *delete_list(list_t list) {
/*******************************************************************************
*
* Description:
* it deletes a list by deallocating its memory from the heap but without
* resetting the values of this memory area.
*
* Parameters:
* [in] head: pointer to the sentinel node
* [out] return: NULL pointer
*
******************************************************************************/
assert(list!=NULL);
node_t **head = list;
node_t *temp;
/* deallocates all nodes in the list */
while(*head ) {
temp = *head;
*head = (*head)->next; // increment the position
free(temp);
}
free(*head); // finally deallocates the sentinel node
return NULL;
}
node_t *insert_first(list_t list, struct sockaddr_in client_address) {
/*******************************************************************************
*
* Description:
* inserts a node in first position with its relative
* value. The inserted node is allocated on the heap.
*
* Parameters:
* [in] head: pointer to the sentinel node
* [in] value: value to be included in the list
* [out] return new: pointer to the inseted node
*
******************************************************************************/
assert(list!=NULL);
node_t **head = list;
node_t *new = malloc(sizeof(node_t));
if (!new) exit(EXIT_FAILURE);
new->next = *head;
*head = new;
new->client_address = client_address;
return new;
}
node_t *insert(list_t list, node_t *pos, struct sockaddr_in client_address) {
/*******************************************************************************
*
* Description:
* inserts a node in succession at the given position with its relative
* value. The inserted node is allocated on the heap.
*
* Parameters:
* [in] head: pointer to the sentinel node
* [in] pos: pointer to node
* [in] value: value to be included in the list
* [out] return new: pointer to the inseted node
*
******************************************************************************/
assert(list!=NULL);
node_t **head = list;
if(!pos) return insert_first(head, client_address);
while (head && *head != pos->next)
head = &(*head)->next; // increment the position
if(head) {
node_t *new = malloc(sizeof(node_t));
if (!new) exit(EXIT_FAILURE);
new->client_address = client_address;
new->next = *head;
*head = new;
return new;
}
return NULL;
}
node_t *remove_node(list_t list, node_t *pos) {
/*******************************************************************************
*
* Description:
* removes the node pointed to by pos, deallocating its memory area in the
* heap and returns the position to the previous node.
*
* Parameters:
* [in] head: pointer to the sentinel node
* [in] pos: pointer to node
* [out] return temp: pointer to the node preceding the one removed
*
******************************************************************************/
assert(list!=NULL);
assert(pos!=NULL);
node_t **head = list;
if(*head == pos) {
*head= pos->next;
free(pos);
return *head;
}
while(*head && (*head)->next != pos)
head = &(*head)->next; // increment the position
if(*head) {
(*head)->next = pos->next;
free(pos);
}
return *head;
}
node_t *next(node_t *pos) {
/*******************************************************************************
*
* Description:
* returns the next node to the given position.
*
* Parameters:
* [in] pos: pointer to node
* [out] return next: pointer to next node
*
******************************************************************************/
assert(pos!=NULL);
return pos->next;
}
node_t *prev(list_t list, node_t *pos) {
/*******************************************************************************
*
* Description:
* returns the previous node to the given position.
*
* Parameters:
* [in] head: pointer to the sentinel node
* [in] pos: pointer to node
* [out] return next: pointer to previous node
*
******************************************************************************/
assert(list!=NULL);
assert(pos!=NULL);
node_t **head = list;
if(*head == pos) return NULL;
while (*head && (*head)->next != pos)
head = &(*head)->next; // increment the position
return *head;
}
node_t *head(list_t list) {
/*******************************************************************************
*
* Description:
* returns the first node to the list.
*
* Parameters:
* [in] head: pointer to the sentinel node
* [out] return next: pointer to first node
*
******************************************************************************/
assert(list!=NULL);
node_t **head = list;
return *head;
}
node_t *tail(list_t list) {
/*******************************************************************************
*
* Description:
* returns the last node to the list.
*
* Parameters:
* [in] head: pointer to the sentinel node
* [out] return pos: pointer to last node
*
******************************************************************************/
assert(list!=NULL);
node_t **head = list;
while (*head && (*head)->next != NULL)
head = &(*head)->next; // increment the position
return *head;
}
struct sockaddr_in get_value(node_t *pos) {
/*******************************************************************************
*
* Description:
* returns the value contained in the node.
*
* Parameters:
* [in] pos: pointer to node
* [out] return value: value contained in the node
*
******************************************************************************/
assert(pos!=NULL);
return pos->client_address;
}
void set_value(node_t *pos, struct sockaddr_in client_address) {
/*******************************************************************************
*
* Description:
* sets the value provided in the node.
*
* Parameters:
* [in] pos: pointer to node
* [in] value: value to be set
*
******************************************************************************/
assert(pos!=NULL);
pos->client_address = client_address;
}
void load_list(list_t list, struct sockaddr_in client_address[], int n) {
/*******************************************************************************
*
* Description:
* populates the empty list by creating the nodes relative to the values
* of the array value[]
*
* Parameters:
* [in] head: pointer to the sentinel node
* [in] value[]: array containing values to insert
* [in] n: array dimension
*
******************************************************************************/
assert(list!=NULL);
assert(n>=0);
node_t **head = list;
node_t *pos = NULL;
for(int i=0; i<n; i++) {
pos = insert(head, pos, client_address[i]); // pos is incremented each time
}
}
void print_list(list_t list) {
/*******************************************************************************
*
* Description:
* print list
*
* Parameters:
* [in] head: pointer to the sentinel node
*
******************************************************************************/
assert(list!=NULL);
node_t **head = list;
printf("\n%s%sLista client connessi:%s\n", BOLDBLACK, BG_MAGENTA, RESET);
while(*head) {
printf("[ %s%s%s:", CYAN, inet_ntoa((*head)->client_address.sin_addr), RESET);
printf("%sUDP%u%s ]\n", MAGENTA, ntohs((*head)->client_address.sin_port), RESET);
head = &(*head)->next;
}
}
bool is_in_list(list_t list, struct sockaddr_in client_address) {
assert(list!=NULL);
node_t **head = list;
while(*head) {
if (memcmp(&client_address, &(*head)->client_address, sizeof(client_address)) == 0) {
return true;
}
head = &(*head)->next;
}
return false;
}