-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclsDynamicArray.h
More file actions
221 lines (182 loc) · 4.02 KB
/
clsDynamicArray.h
File metadata and controls
221 lines (182 loc) · 4.02 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
#pragma once
#include <iostream>
using namespace std;
template <class T>
class clsDynamicArray
{
protected:
int _Size;
T *_TempArray;
public:
T *OriginalArray = nullptr;
clsDynamicArray(int Size = 0)
{
if (Size < 0)
_Size = 0;
_Size = Size;
OriginalArray = new T[_Size];
}
~clsDynamicArray()
{
delete[] OriginalArray;
}
void SetItem(int Index, T Value)
{
if (Index < 0 || Index >= _Size)
{
return;
}
OriginalArray[Index] = Value;
}
int Size()
{
return _Size;
}
bool IsEmpty()
{
return _Size == 0;
}
void PrintList()
{
for (int i = 0; i <= _Size - 1; i++)
{
cout << OriginalArray[i] << " ";
}
cout << "\n";
}
void Resize(int NewSize)
{
if (NewSize < 0)
{
NewSize = 0;
}
_TempArray = new T[NewSize];
if (NewSize < _Size)
{
_Size = NewSize;
}
for (int i = 0; i < _Size; i++)
{
_TempArray[i] = OriginalArray[i];
}
_Size = NewSize;
delete[] OriginalArray;
OriginalArray = _TempArray;
}
T GetItem(int Index)
{
return OriginalArray[Index];
}
void Reverse()
{
_TempArray = new T[_Size];
for (int i = 0; i < _Size; i++)
{
_TempArray[_Size - 1 - i] = OriginalArray[i];
}
delete[] OriginalArray;
OriginalArray = _TempArray;
}
void Clear()
{
_Size = 0;
_TempArray = new T[_Size];
delete[] OriginalArray;
OriginalArray = _TempArray;
}
void DeleteItemAt(int Index)
{
if (Index < 0 || Index >= _Size)
{
return;
}
_Size--;
_TempArray = new T[_Size];
// Copy array elements before index
for (int i = 0; i < Index; i++)
{
_TempArray[i] = OriginalArray[i];
}
// Skip the index and copy the elements after it
for (int i = Index + 1; i < _Size + 1; i++)
{
_TempArray[i - 1] = OriginalArray[i];
}
delete[] OriginalArray;
OriginalArray = _TempArray;
}
void DeleteFirstItem()
{
// Will skip the first while loop before the Idex as the condition will not be true
DeleteItemAt(0);
}
void DeleteLastItem()
{
// Will skip the Second while loop After the Idex as the condition will not be true
DeleteItemAt(_Size - 1);
}
int Find(T Value)
{
for (int i = 0; i < _Size; i++)
{
if (OriginalArray[i] == Value)
{
return i;
}
}
return -1;
}
void DeleteItem(T Value)
{
int Index = Find(Value);
if (Index == -1)
{
return;
}
DeleteItemAt(Index);
}
bool InsertAt(int Index, T Value)
{
if (Index < 0 || Index > _Size)
{
return false;
}
_TempArray = new T[_Size + 1];
int i = 0;
for (i; i < Index; i++)
{
_TempArray[i] = OriginalArray[i];
}
_TempArray[i] = Value;
for (i = Index; i < _Size; i++)
{
_TempArray[i + 1] = OriginalArray[i];
}
_Size++;
delete[] OriginalArray;
OriginalArray = _TempArray;
return true;
}
bool InsertAtBeginning(T Value)
{
return InsertAt(0, Value);
}
bool InsertBefore(int Index, T Value)
{
if (Index < 0)
return InsertAt(0, Value);
else
return InsertAt(Index - 1, Value);
}
bool InsertAfter(int Index, T Value)
{
if (Index > _Size)
return InsertAt(_Size, Value);
else
return InsertAt(Index + 1, Value);
}
bool InsertAtEnd(T Value)
{
return InsertAt(_Size, Value);
}
};