-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPQueue.cpp
200 lines (184 loc) · 5.05 KB
/
PQueue.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include "PQueue.h"
#include <iostream>
#include <exception>
using namespace std;
PQueue::PQueue() : capacity(50), size(0) { //constructor fara parametri
this->v = new valPr[this->capacity];
}
PQueue::PQueue(int x, int pr) : capacity(50), size(1) { //constructor cu parametri
this->v = new valPr[this->capacity];
if (pr <= 0)
throw invalid_argument("Prioritatea este nula sau negativa");
this->v[0].val = x;
this->v[0].pr = pr;
}
PQueue::PQueue(const PQueue& pq) { //constructor de copiere
*this = pq;
}
PQueue::~PQueue() { //destructor
if (this->size > 0) {
delete[] this->v;
this->size = this->capacity = 0;
}
}
void PQueue::push(int x, int pr) { //adaugare element
if (pr <= 0)
throw invalid_argument("Prioritatea este nula sau negativa");
if (this->size == this->capacity) {
this->capacity += 10;
valPr* aux;
aux = new valPr[this->capacity];
for (int i = 0; i < capacity - 10; i++) {
aux[i].val = this->v[i].val;
aux[i].pr = this->v[i].pr;
}
delete[] this->v;
this->v = aux;
}
int i = this->size;
this->size++;
this->v[i].val = x;
this->v[i].pr = pr;
while ((i - 1) / 2 >= 0 and this->v[i].pr > this->v[(i - 1) / 2].pr) {
int auxval = this->v[i].val, auxpr = this->v[i].pr;
this->v[i].val = this->v[(i - 1) / 2].val;
this->v[i].pr = this->v[(i - 1) / 2].pr;
this->v[(i - 1) / 2].val = auxval;
this->v[(i - 1) / 2].pr = auxpr;
i = (i - 1) / 2;
}
}
void PQueue::pop(int i) { //eliminare element
if (this->size == 0)
throw underflow_error("Coada este vida");
if (i < 0 or i >= this->size)
throw out_of_range("Index out of range");
this->v[i].val = this->v[this->size - 1].val;
this->v[i].pr = this->v[this->size - 1].pr;
this->size--;
while ((2 * i + 1 < this->size and this->v[i].pr < this->v[2 * i + 1].pr) or (2 * i + 2 < this->size and this->v[i].pr < this->v[2 * i + 2].pr)) {
if (2 * i + 1 < this->size and 2 * i + 2 < this->size) {
if (this->v[2 * i + 1].pr > this->v[2 * i + 2].pr) {
int auxval = this->v[i].val, auxpr = this->v[i].pr;
this->v[i].val = this->v[2 * i + 1].val;
this->v[i].pr = this->v[2 * i + 1].pr;
this->v[2 * i + 1].val = auxval;
this->v[2 * i + 1].pr = auxpr;
i = 2 * i + 1;
}
else {
int auxval = this->v[i].val, auxpr = this->v[i].pr;
this->v[i].val = this->v[2 * i + 2].val;
this->v[i].pr = this->v[2 * i + 2].pr;
this->v[2 * i + 2].val = auxval;
this->v[2 * i + 2].pr = auxpr;
i = 2 * i + 2;
}
}
else if (2 * i + 1 < this->size) {
int auxval = this->v[i].val, auxpr = this->v[i].pr;
this->v[i].val = this->v[2 * i + 1].val;
this->v[i].pr = this->v[2 * i + 1].pr;
this->v[2 * i + 1].val = auxval;
this->v[2 * i + 1].pr = auxpr;
i = 2 * i + 1;
}
else {
int auxval = this->v[i].val, auxpr = this->v[i].pr;
this->v[i].val = this->v[2 * i + 2].val;
this->v[i].pr = this->v[2 * i + 2].pr;
this->v[2 * i + 2].val = auxval;
this->v[2 * i + 2].pr = auxpr;
i = 2 * i + 2;
}
}
}
int PQueue::getSize() const { //obtinere numar elemente
return this->size;
}
int PQueue::getMax() const { //obtinere valoare maxima
if (this->size == 0)
throw underflow_error("Coada este vida");
else {
int max = this->v[0].val;
for (int i = 1; i < this->size; i++)
if (v[i].val > max)
max = v[i].val;
return max;
}
}
int PQueue::getPrMax() const { //obtinere prioritate maxima
if (this->size == 0)
throw underflow_error("Coada este vida");
else
return this->v[0].pr;
}
int PQueue::getPrMin() const { //obtinere prioritate minima
if (this->size == 0)
throw underflow_error("Coada este vida");
else
return this->v[this->size - 1].pr;
}
PQueue& PQueue::operator =(const PQueue& pq) { //supraincarcare operator =
if (this != &pq) {
this->~PQueue();
this->capacity = pq.capacity;
this->size = pq.size;
this->v = new valPr[this->capacity];
for (int i = 0; i < pq.size; i++) {
this->v[i].val = pq.v[i].val;
this->v[i].pr = pq.v[i].pr;
}
return *this;
}
}
PQueue PQueue::operator +(const PQueue& pq) { //supraincarcare operator +
PQueue pqNou(*this);
for (int i = 0; i < pq.size; i++)
pqNou.push(pq.v[i].val, pq.v[i].pr);
return pqNou;
}
PQueue& PQueue::operator ++() { //supraincarcare operator ++
if (this->size == 0)
throw underflow_error("Coada este vida");
for (int i = 0; i < this->size; i++)
this->v[i].pr++;
return *this;
}
PQueue& PQueue::operator --() { //supraincarcare operator --
if (this->size == 0)
throw underflow_error("Coada este vida");
int i;
for (i = 0; i < this->size; i++) {
this->v[i].pr--;
if (this->v[i].pr == 0)
break;
}
int j = size - 1;
while (j >= i) {
this->pop(j);
j--;
}
return *this;
}
ostream& operator <<(ostream& out, const PQueue& pq) { //supraincarcare operator <<
if (pq.getSize() == 0)
out << "Coada vida";
else {
PQueue aux(pq);
while (aux.getSize()) {
out << "(" << aux.v[0].val << ", " << aux.v[0].pr << ")" << " ";
aux.pop();
}
}
return out;
}
istream& operator >>(istream& in, PQueue& pq) { //supraincarcare operator >>
int n, val, pr;
in >> n;
for (int i = 0; i < n; i++) {
in >> val >> pr;
pq.push(val, pr);
}
return in;
}