-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZaruri masluite.cpp
184 lines (168 loc) · 3.68 KB
/
Zaruri masluite.cpp
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
#include<iostream>
using namespace std;
class DATA
{
int a;
public:
DATA()
{
a = 0;
}
DATA(int a1)
{
a = a1;
}
bool operator==(DATA x2)
{
return (a == x2.a);
}
friend ostream& operator<<(ostream& os, DATA d)
{
os << d.a << " ";
return os;
}
friend istream& operator>>(istream& input, DATA& D)
{
input >> D.a;
return input;
}
};
template<typename T>class LinkedList;
template<typename T>
class Node
{
T data;
Node<T>* urm; // legatura la nodul urmator
Node<T>* ant; // legatura la nodul anterior
public:
Node()
{
urm = ant = nullptr;
}
Node(T x)
{
data = x;
urm = ant = nullptr;
}
~Node()
{
if (urm)
{
delete urm; // APEL destructor clasa Nod
} // Se apelează recursiv (repetitiv)
// destructorul clasei Nod, pana se
// ajunge la ultimul element al listei
}
friend class LinkedList<T>;
};
template<typename T>
class LinkedList
{
Node<T>* head, * sf;
public:
LinkedList()
{
head = new Node<T>;
sf = new Node<T>();
head->urm = sf;
sf->ant = head;
head->ant = nullptr; //santinela
sf->urm = nullptr; //santinela
}
~LinkedList()
{
delete head; // distruge prima celula (santinela de inceput)
} // apelare destructor clasa Nod
int add(const T&); //adaug la finalul listei
int lungime(); //lungime lista
void verificare();
};
template <class T>
int LinkedList<T>::add(const T& d)
{
if (!(sf->urm = new Node<T>())) // [2] creare nod nou
return 1; // eroare alocare memorie
sf->urm->ant = sf; // [3] legatura inversa nod nou -> sf
sf->data = d; // [3] adaugare info nou in fosta santinela
sf = sf->urm; // [4] actualizare sfarsit lista (santinela)
sf->urm = nullptr; // [4] anulare legatura nod urmator
return 0; // operatie terminata cu succes
}
template <class T>
int LinkedList<T>::lungime()
{
Node<T>* crt;
int lung = 0;
if (head)
{
crt = head->urm;
while (crt != sf)
{
crt = crt->urm;
lung++;
}
return lung;
}
return -1; //eroare
}
template <class T>
void LinkedList<T>::verificare()
{
Node<T>* tmp=head->urm;
Node<T>* tmp2=head->urm;
float l=lungime();
int aux=0, nr_max=-9999, nr_min=9999;
while(tmp->urm)
{
while(tmp2->urm)
{
if(tmp->data==tmp2->data) //verific daca elementul curent este egal cu tmp
{
aux++; //contorizez
//scot elementul din lista
tmp2->ant->urm=tmp2->urm;
tmp2->urm->ant=tmp2->ant;
//trec la urmatorul element
tmp2=tmp2->urm;
}
else
{
//trec la urmatorul element
tmp2=tmp2->urm;
}
}
if(nr_max<aux)
{
nr_max=aux;
}
if(nr_min>aux)
{
nr_min=aux;
}
aux=0;
tmp=head->urm; //actualizez tmp
tmp2=tmp;
}
if(nr_max==nr_min)
{
nr_min=0;
}
if(nr_max-nr_min > 0.1*l)
cout << 1;
else
cout<< 0;
}
int main()
{
LinkedList<DATA> zaruri;
int n;
DATA fata;
cin>>n;
for(int i=0;i<n;i++)
{
cin >> fata;
zaruri.add(DATA(fata));
}
zaruri.verificare();
return 0;
}