-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.c
56 lines (51 loc) · 1.16 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
//
// Created by georgii on 24.03.2021.
//
#include <malloc.h>
#include "list.h"
list_item_t *push(list_item_t *pp, void *data) {
list_item_t *p = malloc(sizeof(list_item_t));
p->next = pp;
p->data = data;
return p;
}
list_item_t *get(list_item_t *pp, size_t idx) {
list_item_t *item = pp;
size_t i = 0;
while (item != NULL) {
if (item->data != NULL) {
if (i == idx) {
return item;
}
i++;
}
item = item->next;
}
return NULL;
}
list_item_t *remove_el(list_item_t *pp, list_item_t *p) {
list_item_t *item = pp;
if (item == p) {
return item->next;
}
while (item != NULL) {
if (item->next == p) {
item->next = p->next;
p->next = NULL;
return pp;
}
item = item->next;
}
return NULL;
}
void destroy_list(list_item_t* list, int (*destroy_data)(void*)) {
list_item_t *item = list;
while (item != NULL) {
if (item->data != NULL) {
destroy_data(item->data);
}
list_item_t *prev = item;
item = item->next;
free(prev);
}
}