Repository navigation
Expand file tree
/
Copy pathmySTL.cpp
More file actions
341 lines (256 loc) · 4.82 KB
/
Copy pathmySTL.cpp
File metadata and controls
341 lines (256 loc) · 4.82 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#include <iostream>
using namespace std;
template <typename Item>
struct node{
Item item;
node<Item> *next;
node(Item _item, node<Item> *_next){
item = _item;
next = _next;
}
};
template <typename Item>
class list{
node<Item> *head;
node<Item> *tail;
public:
list(node<Item> *_head = NULL, node<Item> *_tail = NULL){
head = _head;
tail = _tail;
}
int longitud();
void insertaAlInicio(Item item);
void insertaAlFinal(Item item);
node<Item>* regresaNth(int N);
void limpiar();
node<Item>* primero();
node<Item>* ultimo();
void quitaPrimero();
void quitaUltimo();
~list(){
limpiar();
}
};
template<typename Item>
int list<Item>::longitud(){
int len=0;
node<Item> *copy = head;
while(copy!=NULL){
len++;
copy = copy->next;
}
return len;
}
template <typename Item>
void list<Item>::insertaAlInicio(Item item){
head = new node<Item>(item, head);
if(tail==NULL)
tail = head;
return;
}
template <typename Item>
void list<Item>::insertaAlFinal(Item item){
if(head == NULL){
insertaAlInicio(item);
return;
}
tail->next = new node<Item>(item, NULL);
tail = tail->next;
return;
}
template <typename Item>
node<Item>* list<Item>::regresaNth(int N){
if(N <= 0)
return NULL;
node<Item> *copy = head;
for(int i=1; i<N && copy != NULL; i++)
copy = copy->next;
return copy;
}
template <typename Item>
void list<Item>::limpiar(){
node<Item> *aux;
while (head != NULL){
aux = head;
head = aux->next;
delete aux;
}
tail=NULL;
return;
}
template <typename Item>
node<Item>* list<Item>::primero(){
return head;
}
template <typename Item>
node<Item>* list<Item>::ultimo(){
return tail;
}
template <typename Item>
void list<Item>::quitaPrimero(){
if(head == NULL)
return;
if(tail==head)
tail=NULL;
node<Item> *aux = head;
head = aux->next;
delete aux;
return;
}
template <typename Item>
void list<Item>::quitaUltimo(){
if(tail == NULL)
return;
if(tail==head){
tail=NULL;
delete head;
head=NULL;
return;
}
node<Item> *copy = head;
while(copy->next != tail)
copy = copy->next;
delete tail;
tail = copy;
tail->next = NULL;
return;
}
template <typename Item>
class Stack{
list<Item> stack;
public:
Stack(){
stack = list<Item>();
}
void push(Item item){
stack.insertaAlInicio(item);
return;
}
void pop(){
stack.quitaPrimero();
return;
}
int size(){
return stack.longitud();
}
Item peek(){
/*node<Item> *top = lista.regresaNth(1);
return top == NULL ? 0 : top->item;*/
return stack.regresaNth(1)->item;
}
bool empty(){
return (stack.regresaNth(1) == NULL) ? true : false;
}
~Stack(){
stack.limpiar();
}
};
template <typename Item>
class tree{
public:
Item item;
tree *left;
tree *right;
tree *dad;
tree(){
left=NULL; right=NULL; dad=NULL;
}
tree(Item _item, tree<Item> *_left=NULL, tree<Item> *_right=NULL, tree<Item> *_dad=NULL){
item=_item; left=_left; right=_right; dad=_dad;
}
void join(tree<Item> *tree1, tree<Item> *tree2){
left=tree1; right=tree2;
tree1->dad=this; tree2->dad=this;
}
bool operator>(const tree<Item> &right){
return item>right.item;
}
};
template <typename Item>
class priority_queue{
Item *pq;
int maxN, N;
void fixUp(int k){
while(k>1 && pq[k/2] > pq[k]){
Item temp=pq[k/2];
pq[k/2]=pq[k];
pq[k]=temp;
k=k/2;
}
}
void fixDown(int k){
while(2*k <= N){
int j=2*k;
if(j<N && pq[j] > pq[j+1])
j++;
if(!(pq[k]>pq[j]))
break;
Item temp=pq[k];
pq[k]=pq[j];
pq[j]=temp;
k=j;
}
}
public:
priority_queue(int _maxN){
maxN = _maxN;
pq=new Item[maxN+1];
N=0;
}
~priority_queue(){
delete[] pq;
}
void insert(Item x){
if(N>maxN){
cout << "priority queue overflow"; return;
}
pq[++N]=x;
fixUp(N);
}
Item delMin(){
Item min=pq[1];
pq[1]=pq[N];
pq[N]=min;
N--;
fixDown(1);
return min;
}
int size(){
return N;
}
};
class Mask{
long long unsigned int mask;
public:
Mask(){
mask=0;
}
void addBit(int n){
if(0<=n && n<=63){
long long unsigned int aux = 1LL<<n;
mask = mask | aux;
}
}
void quitBit(int n){
if(0<=n && n<=63){
long long unsigned int aux = 1LL<<n;
aux = ~aux;
mask = mask & aux;
}
}
bool ask(int n){
if(0<=n && n<=63){
Mask aux; aux.mask=mask;
aux.addBit(n);
if(aux.mask == mask)
return true;
}
return false;
}
void reset(){
mask=0;
}
long long unsigned int * pointerToMask(){
return &mask;
}
};