-
Notifications
You must be signed in to change notification settings - Fork 27
/
heap.cpp
168 lines (119 loc) · 2.69 KB
/
heap.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
#include <iostream>
#include <cstring>
#include <cassert>
char* _concatenate (const char *s1, const char *s2)
{
//strlen (s1), strlen (s2), stcpy, strcat
char *newBuffer = new char[strlen(s1) + strlen(s2) + 1];
strcpy (newBuffer,s1);
strcat (newBuffer, s2);
return newBuffer;
}
struct intarray
{
int *pa;
size_t size;
};
void add_unique (intarray &result, intarray a)
{
for (size_t i = 0; i < a.size; ++i)
{
//a1[i] се среща в result или не?
size_t ri = 0;
while (ri < result.size && a.pa[i] != result.pa[ri])
{
++ri;
}
//сигурно ли е, че a[i] не се среща в result?
if (ri == result.size)
{
result.pa[result.size] = a.pa[i];
++result.size;
}
}
}
void printAll (intarray a)
{
std::cout << "[";
for (int i = 0; i < a.size-1; i++)
std::cout << a.pa[i] << ",";
if (a.size > 0)
std::cout << a.pa[a.size-1];
std::cout << "]" << std::endl;
}
//a1 <- a2, n елемента
void copy (int a1[], int a2[], size_t n)
{
for (size_t i = 0; i < n; ++i)
{
a1[i] = a2[i];
}
}
intarray _union (intarray a1, intarray a2)
{
intarray result;
result.pa = new int [a1.size+a2.size];
result.size = 0;
add_unique (result,a1);
add_unique (result,a2);
int *end_result = new int [result.size];
copy (end_result,result.pa,result.size);
delete []result.pa;
result.pa = end_result;
return result;
}
void push (intarray &a, int x)
{
//a.pa = {1,2,3,4};
//s.size = 4;
//x = 5;
int *newarray = new int [a.size + 1];
copy (newarray,a.pa,a.size);
newarray[a.size] = x;
delete a.pa;
++a.size;
a.pa = newarray;
}
void remove (intarray &a, size_t index)
{
assert (index < a.size);
int *newarray = new int [a.size - 1];
copy (newarray,a.pa,index);
copy (newarray+index,a.pa+index+1,a.size-index-1);
delete a.pa;
--a.size;
a.pa = newarray;
}
/*
_strduplicate (const char *s)
{
}
*/
int main ()
{
char *s = _concatenate ("Hello ", "world!");
std::cout << s << std::endl;
delete s;
int _a1[] = {1,2,5,9,10};
int _a2[] = {3,4,9,8,7};
intarray a1{_a1,5};
intarray a2{_a2,5};
intarray un = _union (a1,a2);
printAll (un);
delete un.pa;
intarray test {new int[0],0};
push (test,20);
push (test,30);
push (test,40);
push (test,50);
push (test,60);
push (test,70);
printAll (test);
remove (test,0);
printAll (test);
remove (test,4);
printAll (test);
remove (test,2);
printAll (test);
return 0;
}