forked from jankaluza/hamlog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.c
159 lines (134 loc) · 3.04 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
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
/**
* Hamlog
*
* Copyright (C) 2011, Jan Kaluza <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
*/
#include "list.h"
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <string.h>
#include <errno.h>
HAMList *ham_list_new() {
HAMList *list = malloc(sizeof(HAMList));
if (list == NULL)
return NULL;
list->first = NULL;
list->last = NULL;
list->free_func = NULL;
return list;
}
void ham_list_destroy(HAMList *list) {
if (list == NULL)
return;
HAMListItem *ptr = list->first;
HAMListItem *ptr2;
while (ptr) {
ptr2 = ptr->rptr;
if (list->free_func) {
list->free_func(ptr->data);
}
free(ptr);
ptr = ptr2;
}
free(list);
}
void ham_list_set_free_func(HAMList *list, HAMListItemDataFree func) {
list->free_func = func;
}
void ham_list_insert_first(HAMList *list, void *data) {
HAMListItem *ptr;
if ((ptr = malloc(sizeof(HAMListItem))) == NULL) {
return;
}
ptr->data = data;
ptr->lptr = NULL;
ptr->rptr = list->first;
if (list->first != NULL) {
list->first->lptr = ptr;
}
else {
list->last = ptr;
}
list->first = ptr;
}
void ham_list_insert_last(HAMList *list, void *data) {
HAMListItem *ptr;
if ((ptr = malloc(sizeof(HAMListItem))) == NULL) {
return;
}
ptr->data = data;
ptr->lptr = list->last;
ptr->rptr = NULL;
if (list->last != NULL) {
list->last->rptr = ptr;
}
else {
list->first = ptr;
}
list->last = ptr;
}
void ham_list_remove(HAMList *list, void *data) {
HAMListItem *act = list->first;
while(act) {
if (act->data == data) {
break;
}
act = act->rptr;
}
if (act == NULL)
return;
if (list->first == act) {
list->first = act->rptr;
}
else {
act->lptr->rptr = act->rptr;
}
if (list->last == act) {
list->last = act->lptr;
}
else {
act->rptr->lptr = act->lptr;
}
free(act);
}
void *ham_list_pop_first(HAMList *list) {
if (list->first == NULL)
return NULL;
void *data = list->first->data;
ham_list_remove(list, data);
return data;
}
void *ham_list_get_first(HAMList *list) {
if (list->first == NULL)
return NULL;
return list->first->data;
}
void *ham_list_get_last(HAMList *list) {
if (list->last == NULL)
return NULL;
return list->last->data;
}
HAMListItem *ham_list_get_first_item(HAMList *list) {
return list->first;
}
HAMListItem *ham_list_get_next_item(HAMListItem *item) {
return item->rptr;
}
void *ham_list_item_get_data(HAMListItem *item) {
return item->data;
}