-
Notifications
You must be signed in to change notification settings - Fork 0
/
stiva.c
164 lines (151 loc) · 3.79 KB
/
stiva.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
/* OLARU Gabriel-312CB */
/*-- functiiStiva-V.c -- elementele stivei sunt memorate intr-o lista generica simplu inlantuita --*/
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "lista.h"
#include "stiva.h"
#include "coada.h"
/* creeaza stiva vida cu elemente de dimensiune d; */
void *InitS(size_t d)
{
ASt s = (ASt)malloc(sizeof (TStiva)); /* adresa descriptor alocat */
if (!s)
return NULL;
s->dime = d;
/* actualizeaza adrese din descriptor */
s->vf = NULL;
return (void *)s;
}
int AdaugaSezonS(void *s, char *numeS, TCoada *c, int index)
{
TLG p;
p = VF(s);
TCoada *cs;
for(; p != NULL; p = p->urm)
if( strcmp( ((TSerial*)(p->info))->numes,numeS) == 0)
{
cs = ((TSerial*)(p->info))->sezoane;
TSezon * sz = malloc(sizeof(TSezon));
if (sz == NULL) return 1;
sz->nr_ep = index;
sz->TEp = (TCoada*)c;
cs = ((TSerial*)(p->info))->sezoane;
if(InsCoadaSz(cs, sz) == 0) return 1;
return 0;
}
return 1; //nu s-a gasit
}
//actualizeaza in stiva currently_watching durata vizionata + d
//data este mai mare ca durata serialului muta in stiva history
int AddDS(void *s, char *numeS, int d, TStiva *sh) //intoarce nr. de minute vizionate
{
TLG p, ant, q;
p = VF(s);
TSerial * sr;
int d_viz = 0, d_serial = 0;
for(ant = NULL; p != NULL; ant = p, p = p->urm)
if( strcmp( ((TSerial*)(p->info))->numes, numeS) == 0)
{
((TSerial*)(p->info))->vizionat += d;
d_viz = ((TSerial*)(p->info))->vizionat;
d_serial = DurataSerial(((TSerial*)(p->info))->sezoane);
if (d_serial <= d_viz) //muta in stiva history
{
sr = (TSerial*)(p->info);
if(Push(sh, sr) == 0) //in history
return -1;
if(ant == NULL) //stiva are 1 element
VF(s) = p->urm;
else
ant->urm = p->urm;
return 1;
}
else
{
if(ant == NULL) return 0;
if(p->urm == NULL)
{
q = VF(s);
p->urm = q;
VF(s) = p;
ant->urm = NULL;
}
else
{
q = VF(s);
ant->urm = p->urm;
p->urm = q;
VF(s) = p;
}
}
}
return 0; //nu s-a gasit
}
//cauta in stiva numeS
int GasitS(void *s, char *numeS)
{
TLG p;
p = VF(s);
if(VF(s) == NULL) return 0;
for(; p != NULL; p = p->urm)
if( strcmp( ((TSerial*)(p->info))->numes, numeS) == 0)
return 1; //s-a gasit
return 0; //nu s-a gasit
}
/* pune element in varful stivei */
int Push(void *s, void *ae)
{
TLG aux;
aux = (TLG)malloc(sizeof(TCelulaG));
if(!aux)
return 0;
aux->info = ae;
if( ( (ASt)s )->vf == NULL)
{
( (ASt)s)->vf = aux;
return 1;
}
aux->urm = ( (ASt)s )->vf;
( (ASt)s)->vf = aux;
return 1;
}
/* extrage elementul din varful stivei la adresa ae */
int Pop(void *s, void *ae)
{
TLG aux = VF(s);
if(aux == NULL)
return 0;
memcpy(ae, aux->info, DIMES(s));
VF(s) = aux->urm;
free(aux);
return 1;
}
/* test stiva vida */
int VidaS(void *a)
{
return VF(a) == NULL;
}
/* transforma stiva in stiva vida */
void ResetS(void *a)
{
Distruge(&VF(a), free);
VF(a) = NULL; /* = NULL */
}
/* elibereaza intregul spatiu ocupat de stiva */
void DistrS(void **aa)
{
ResetS(*aa); /* elibereaza spatiul */
free(*aa); /* elibereaza spatiul alocat pentru descriptor */
*aa = NULL;
}
/* prelucreaza de la Varf spre Baza */
size_t Prel_V_B(void *a, TF1 f)
{
size_t k = 0; /* contor elemente pentru care f != 0 */
TLG p;
for(p = VF(a); p != NULL; p = p->urm ) /* incepand de la varf */
if(f(p->info)) k++; /* aplica functia f si, eventual, creste contor */
return k;
}