-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbllist.c
142 lines (130 loc) · 2.81 KB
/
dbllist.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
#include <stdio.h>
#include <stdlib.h>
#include "dbllist.h"
void dbllist_init(dbllist_t* l)
{
dbllist_head(l) = NULL;
dbllist_tail(l) = NULL;
dbllist_size(l) = 0;
}
void dbllist_destroy(dbllist_t *l, dbllist_destroy_t dest)
{
if (dest==DBLLIST_LEAVE_DATA || dbllist_head(l)==NULL) //0
{
free(l);
}
if (dest==DBLLIST_FREE_DATA) //1
{
dbllist_node_t* n = dbllist_head(l);
dbllist_node_t* tmp;
while (n!=NULL)
{
tmp = n;
n = dbllist_next(n);
free(tmp->data);
free(tmp);
}
free(l);
}
}
int dbllist_append(dbllist_t *l, void *data)
{
dbllist_node_t* new_node = (dbllist_node_t*) malloc(sizeof(dbllist_node_t));
new_node->data = data;
if (l==NULL) return -1;
if (dbllist_head(l)==NULL)
{
dbllist_head(l) = new_node;
dbllist_tail(l) = new_node;
}
else
{
new_node->next = dbllist_head(l);
dbllist_head(l)->prev = new_node;
}
new_node->prev = NULL;
dbllist_head(l) = new_node;
dbllist_size(l)++;
return 0;
}
int dbllist_prepend(dbllist_t *l, void *data)
{
dbllist_node_t* new_node = (dbllist_node_t*) malloc(sizeof(dbllist_node_t));
new_node->data = data;
if (l==NULL) return -1;
if (dbllist_tail(l)==NULL)
{
dbllist_tail(l) = new_node;
dbllist_head(l) = new_node;
}
else
{
new_node->next = NULL;
dbllist_tail(l)->next = new_node;
}
new_node->prev = dbllist_tail(l);
dbllist_tail(l) = new_node;
dbllist_size(l)++;
return 0;
}
int dbllist_remove(dbllist_t *l, dbllist_node_t* n, dbllist_destroy_t data)
{
if( dbllist_next(n) == NULL && dbllist_prev(n) != NULL )
{
dbllist_prev(n)->next = NULL;
dbllist_tail(l) = dbllist_prev(n);
if(data == DBLLIST_LEAVE_DATA)
{
free(n);
}
else if(data ==DBLLIST_FREE_DATA)
{
free(dbllist_data(n));
free(n);
}
}
else if(dbllist_next(n) != NULL && dbllist_prev(n) == NULL )
{
dbllist_next(n)->prev = NULL;
dbllist_head(l) = dbllist_next(n);
if(data == DBLLIST_LEAVE_DATA)
{
free(n);
}
else if(data ==DBLLIST_FREE_DATA)
{
free(dbllist_data(n));
free(n);
}
}
else if(dbllist_next(n) == NULL && dbllist_prev(n) == NULL )
{
dbllist_tail(l) = NULL;
dbllist_head(l) = NULL;
if(data == DBLLIST_LEAVE_DATA)
{
free(n);
}
else if(data ==DBLLIST_FREE_DATA)
{
free(dbllist_data(n));
free(n);
}
}
else
{
dbllist_prev(n)->next = dbllist_next(n);
dbllist_next(n)->prev = dbllist_prev(n);
if(data == DBLLIST_FREE_DATA)
{
free(dbllist_data(n));
free(n);
}
else if(data == DBLLIST_LEAVE_DATA)
{
free(n);
}
}
dbllist_size(l)--;
return 0;
}