-
Notifications
You must be signed in to change notification settings - Fork 2
/
DA.cpp
57 lines (53 loc) · 988 Bytes
/
DA.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
#include <bits/stdc++.h>
using namespace std;
class DynamicArray
{
public:
int *data;
int nextindex = 0;
int capacity = 5;
DynamicArray()
{
data = new int[5];
nextindex = 0;
capacity = 5;
}
void add(int element)
{
if (nextindex == capacity)
{
int *newdata = new int[2 * capacity];
for (int i = 0; i < capacity; i++)
{
newdata[i] = data[i];
}
capacity = capacity * 2;
delete[] data;
data = newdata;
}
data[nextindex] = element;
nextindex++;
}
void print()
{
for (int i = 0; i < nextindex; i++)
{
cout << data[i] << endl;
}
}
};
int main()
{
DynamicArray d1;
d1.add(90);
d1.add(80);
d1.add(70);
d1.add(60);
d1.add(50);
d1.add(40);
d1.add(30);
d1.add(20);
d1.add(10);
d1.print();
return 0;
}