-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTStiva.c
108 lines (100 loc) · 1.52 KB
/
TStiva.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
/* COMAN ROBERT - 313CB*/
#include "utils.h"
ASt InitS(size_t dim) // Initializare stiva
{
ASt s;
s = malloc(sizeof(TStack));
if(!s)
return NULL;
s->size = dim;
s->vf = NULL;
return s;
}
int Push(ASt s, void *x)
{
ACelSt aux = malloc(sizeof(TCelSt));
if(!aux)
return 0;
aux->info = malloc(s->size);
if(!aux->info)
return 0;
memcpy(aux->info, x, s->size);
aux->next = s->vf;
s->vf = aux;
return 1;
}
int Pop(ASt s, void* ae, TFree f)
{
// Extrage element din stiva
if(s->vf == NULL) // stiva vida
return 0;
memcpy(ae, s->vf->info, s->size);
ACelSt aux = s->vf;
s->vf = aux->next;
f(aux->info);
free(aux);
return 1;
}
void DistrS(ASt s, TFree f)
{
//Elibereaza memoria ocupata de stiva
ACelSt l = s->vf;
while(l)
{
ACelSt aux = l;
l = l->next;
f(aux->info);
free(aux);
}
s->vf = NULL;
free(s);
}
void ResetS(ASt s, TFree f)
{
//reseteaza stiva
ACelSt l = s->vf;
while(l)
{
ACelSt aux = l;
l = l->next;
f(aux->info);
free(aux);
}
s->vf = NULL;
}
int EmptyS(ASt s)
{
if(s->vf == NULL)
return 1;
return 0;
}
ASt ReverseS(ASt s, TFree f)
{
if(EmptyS(s))
return NULL;
AQ auxQ = InitQ(s->size);
if(!auxQ)
return NULL;
//introduc elementele in coada auxiliara
while(!EmptyS(s))
{
void* ae = malloc(s->size);
if(!ae)
return NULL;
Pop(s, ae, f);
IntrQ(auxQ, ae);
free(ae);
}
//introduc elementele in stiva
while(!EmptyQ(auxQ))
{
void* ae = malloc(s->size);
if(!ae)
return NULL;
ExtrQ(auxQ, ae, f);
Push(s, ae);
free(ae);
}
DistrQ(auxQ, f);
return s;
}