-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlist.cpp
More file actions
executable file
·105 lines (75 loc) · 1.7 KB
/
list.cpp
File metadata and controls
executable file
·105 lines (75 loc) · 1.7 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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdint.h>
#include <time.h>
#include "list.h"
LIST *L_last(LIST *list) {
while (list->next != NULL) {
list = list->next;
}
return list;
}
int L_count(LIST *ele) {
int count = 0;
while (ele != NULL) {
count++;
ele = ele->next;
}
return count;
}
void L_link(LIST **list, LIST *ele) {
ele->next = *list;
*list = ele;
}
// order the linking (so its FIFO) instead of LIFO
void L_link_ordered(LIST **list, LIST *ele) {
LIST *_last = NULL;
if (*list == NULL) {
*list = ele;
return;
}
_last = L_last(*list);
_last->next = ele;
}
LIST *L_add_0(LIST **list, int size, int ordered) {
LIST *newptr;
newptr = (LIST *)calloc(1,size);
if (newptr == NULL) return NULL;
if (!ordered)
L_link(list, newptr);
else
L_link_ordered(list, newptr);
return newptr;
}
LIST *L_add(LIST **list, int size) {
return L_add_0(list, size, 0);
}
LIST *L_add_ordered(LIST **list, int size) {
return L_add_0(list, size, 1);
}
void L_del(LIST **l_ptr, LIST *rem) {
if (rem->buf != NULL) {
// later we should keep track of sizes.. so we can zero the memory
free(rem->buf);
}
// close sock.. make this os independent for win32 , etc
if (rem->fd > 0)
close(rem->fd);
while ((*l_ptr) != rem) {
l_ptr = &(*l_ptr)->next;
}
*l_ptr = rem->next;
}
void L_del_next(LIST **l_ptr, LIST *rem, LIST **l_next) {
LIST *lnext = rem->next;
L_del((LIST **)l_ptr, rem);
*l_next = lnext;
}
void ListFree(LIST **qlist) {
LIST *qptr = *qlist;
while (qptr != NULL) {
L_del_next((LIST **)qlist, (LIST *)qptr, (LIST **)&qptr);
}
}