-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTCoada.c
112 lines (104 loc) · 1.68 KB
/
TCoada.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
/* COMAN ROBERT - 313CB*/
#include "utils.h"
AQ InitQ(size_t dim)
{
AQ q = malloc(sizeof(TQueue));
if(!q)
return NULL;
q->size = dim;
q->ic = NULL;
q->sc = NULL;
return q;
}
int IntrQ(AQ q, void* x)
{
//insereaza element in coada
if(EmptyQ(q)) // coada vida
{
// alocare celula
ACelQ aux = malloc(sizeof(TCelQ));
if(!aux)
return 0;
aux->info = malloc(q->size);
memcpy(aux->info, x, q->size);
aux->next = NULL;
q->ic = aux;
q->sc = aux;
return 1;
}
else // NEVIDA
{
ACelQ aux = malloc(sizeof(TCelQ));
if(!aux)
return 0;
aux->info = malloc(q->size);
memcpy(aux->info, x, q->size);
aux->next = NULL;
q->sc->next = aux;
q->sc = aux;
return 1;
}
}
int ExtrQ(AQ q, void* x, TFree f)
{
if(q->ic == NULL && q->sc == NULL)
return 0; // coada vida
ACelQ aux = q->ic;
q->ic = aux->next;
memcpy(x, aux->info, q->size);
f(aux->info); //eliberare info
free(aux);
if(q->ic == NULL) // coada cu un element
q->sc = NULL;
return 1;
}
void DistrQ(AQ q, TFree f)
{
// ELibereaza memoria ocupata de coada
ACelQ p = q->ic;
q->ic = NULL;
q->sc = NULL;
while(p)
{
ACelQ aux = p;
p = p->next;
f(aux->info);
free(aux);
}
free(q);
}
int EmptyQ(AQ q)
{
if(q->ic == NULL && q->sc == NULL)
return 1;
return 0;
}
int ResetQ(AQ q)
{
if(EmptyQ(q))
return -1;
while(!EmptyQ(q))
{
void* ae = malloc(q->size);
if(!ae)
return -1;
ExtrQ(q, ae, free);
free(ae);
}
return 1;
}
int ResetQ_first_n_entries(AQ q, int nr_entries)
{
// sterge primele nr_entries elemente din coada
if(EmptyQ(q))
return -1;
for(int i = 0; i < nr_entries; i++)
{
void* ae = malloc(q->size);
if(!ae)
return -1;
ExtrQ(q, ae, free);
free(ae);
}
return 1;
}