-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollection.c
256 lines (225 loc) · 4.89 KB
/
collection.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
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <math.h>
#include "collection.h"
char *get_delim(FILE *fd, char d)
{
int i = 0, dim = 10;
char *str = malloc(sizeof(char) * dim);
do
{
str[i] = getc(fd);
i++;
if (i == dim)
{
dim += 10;
str = realloc(str, dim);
if (str == NULL)
{
perror("memory error"), exit(EXIT_FAILURE);
}
}
}
while (str[i - 1] != d && !feof(fd));
str[i - 1] = 0;
return str;
}
char *get_line(FILE *fd)
{
char *str;
int dim;
str = get_delim(fd, '\n');
dim = strlen(str);
dim--;
while (str[dim] == '\n' || str[dim] == '\r')
{
str[dim] = 0;
dim--;
}
dim++;
str = realloc(str, sizeof(char) * (dim + 1));
if (str == NULL)
perror("memory error"), exit(EXIT_FAILURE);
return str;
}
void list_insert_tail(list_t* list, void* elem)
{
/* se ho un solo elemento,
* devo avere testa e coda uguali. */
node_t* node = malloc(sizeof(node_t));
if (node == NULL)
{
perror("memoria finita\n"), exit(EXIT_FAILURE);
}
node->info = elem;
node->next = NULL;
node->prev = list->tail;
if(list->size > 0)
{
list->tail->next = node;
list->tail = node;
}
else
{
list->tail = list->head = node;
}
list->size++;
}
void list_insert_head(list_t* list, void* elem)
{
/* se ho un solo elemento,
* devo avere testa e coda uguali. */
node_t* node = malloc(sizeof(node_t));
if (node == NULL)
{
perror("memoria finita\n"), exit(EXIT_FAILURE);
}
node->info = elem;
node->next = list->head;
node->prev = NULL;
list->head = node;
list->size++;
if(list->size == 1) list->tail = node;
}
list_t* list_init()
{
list_t* list = malloc(sizeof(list_t));
if (list == NULL)
{
perror("memoria finita\n"), exit(EXIT_FAILURE);
}
list->head = list->tail = NULL;
list->size = 0;
return list;
}
int list_is_empty(list_t* l){
return l->size == 0;
}
void* list_head(list_t* list)
{
if (list->size > 0)
return list->head->info;
return (void*)-1;
}
void list_delete_head(list_t* list)
{
/* se c'era un solo elemento,
* testa e coda a null */
if(list->size == 0) return;
node_t* temp = list->head->next;
free(list->head);
list->head = temp;
if(list->head == NULL) list->tail = NULL;
list->size--;
}
void list_delete_tail(list_t* list)
{
/* se c'era un solo elemento,
* testa e coda a null */
if(list->size == 0) return;
node_t* temp = list->tail->prev;
free(list->tail);
list->tail = temp;
if(list->tail == NULL) list->head = NULL;
list->size--;
}
void list_print(list_t* list)
{
node_t* node = list->head;
while(node != NULL)
{
printf("%p ", (node->info));
node = node->next;
}
}
void list_delete_elem(list_t* list, void* elem){
node_t* node = list->head;
if(list->head == NULL) return;
while(node != NULL && node->info != elem){
node = node->next;
}
if(node != NULL){
node_t* temp = node->next;
node_t* temp2 = node->prev;
if(node == list->head) list->head = temp;
free(node);
if(temp2 != NULL) temp2->next = temp;
}
}
list_t* list_delete(list_t* list, int dealloc)
{
if(list == NULL) return list;
node_t* node = list->head, *tmp;
while(node)
{
tmp = node->next;
free(node);
node = tmp;
}
free(list);
return NULL;
}
void queue_push(queue_t* queue, void* elem)
{
/* se ho un solo elemento,
* devo avere testa e coda uguali. */
node_t* node = malloc(sizeof(node_t));
if (node == NULL)
{
perror("memoria finita\n"), exit(EXIT_FAILURE);
}
node->info = elem;
node->next = NULL;
node->prev = queue->tail;
if(queue->size > 0)
{
queue->tail->next = node;
queue->tail = node;
}
else
{
queue->tail = queue->head = node;
}
queue->size++;
}
queue_t* queue_init()
{
queue_t* queue = malloc(sizeof(queue_t));
if (queue == NULL)
{
perror("memoria finita\n"), exit(EXIT_FAILURE);
}
queue->head = queue->tail = NULL;
queue->size = 0;
return queue;
}
void* queue_top(queue_t* queue)
{
if(queue->size > 0)
return queue->head->info;
return NULL;
}
void queue_pop(queue_t* queue)
{
/* se c'era un solo elemento,
* testa e coda a null */
if(queue->size == 0) return;
node_t* temp = queue->head->next;
free(queue->head);
queue->head = temp;
if(queue->head == NULL) queue->tail = NULL;
queue->size--;
}
int queue_is_empty(queue_t* q){
return 0 == q->size;
}
void queue_delete(queue_t* queue)
{
while(queue->size > 0)
queue_pop(queue);
}