-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdynamic_array.h
More file actions
124 lines (109 loc) · 2.21 KB
/
dynamic_array.h
File metadata and controls
124 lines (109 loc) · 2.21 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
#include <iostream>
using namespace std;
template <typename T>
class DynamicArray
{
private:
int mySize; // Size of the array
int initial; // Number of elements in the array
T *myArray; // Pointer to the array
public:
DynamicArray(int size) : mySize(size), initial(0)
{
myArray = new T[mySize];
}
~DynamicArray()
{
delete[] myArray;
}
const void display_element()
{
if (this->initial == 0)
{
cout << "No elements.";
}
for (int i = 0; i < this->initial; i++)
{
cout << this->myArray[i] << " ";
}
cout << endl;
}
void add_element(T input)
{
if (this->initial == this->mySize)
{
const int new_size = this->mySize * 2;
T *new_array = new T[new_size];
for (int i = 0; i < this->mySize; i++)
{
new_array[i] = this->myArray[i];
}
delete[] this->myArray;
this->myArray = new_array;
this->mySize = new_size;
}
this->myArray[this->initial] = input;
this->initial++;
}
// Multiple elements can be added at once
void add_element(const initializer_list<T> &inputs)
{
for (auto input : inputs)
{
add_element(input);
}
}
const void return_size()
{
cout << "Size: " << this->mySize << ", Elements: " << this->initial << endl;
}
void pop_element(T get)
{
if (this->initial == 0)
{
throw out_of_range("The dynamic array is empty.");
return;
}
int count = 0;
bool present = false;
for (int i = 0; i < this->initial; i++)
{
if (this->myArray[i] == get)
{
for (int j = i; j < this->initial; j++)
{
this->myArray[j] = this->myArray[j + 1];
present = true;
count++;
}
this->initial--;
i--;
if (this->initial <= this->mySize / 2 && this->initial > 1)
{
int shrink_size = this->mySize / 2;
T *shrink_array = new T[shrink_size];
for (int i = 0; i < this->initial; i++)
{
shrink_array[i] = this->myArray[i];
}
delete[] this->myArray;
this->myArray = shrink_array;
this->mySize = shrink_size;
}
break;
}
}
if (present != true)
cout << "Not there." << endl;
}
T& operator[](int index){
return this->myArray[index];
}
void delete_all()
{
delete[] this->myArray;
this->myArray = new T[2];
this->mySize = 1;
this->initial = 0;
}
};