-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclsDynamicArray.h
More file actions
200 lines (152 loc) · 3.8 KB
/
clsDynamicArray.h
File metadata and controls
200 lines (152 loc) · 3.8 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
#pragma once
#include "clsDblLinkedList.h"
template <class T>
class clsDynamicArray
{
protected:
int _Size=0;
T* _TempArray;
public:
T* OriginalArray;
clsDynamicArray(int size=0) {
if (size < 0)size = 0;
_Size = size;
OriginalArray = new T[size];
}
~clsDynamicArray() {
delete[] OriginalArray;
}
bool SetItem(int index, T Item) {
if (index >= _Size || _Size < 0)
return false;
OriginalArray[index] = Item;
return true;
}
void PrintList() {
cout << endl;
for (short i = 0; i < _Size; i++) {
cout << OriginalArray[i] << " ";
}
}
int Size() {
return _Size;
}
bool IsEmpty() {
return (_Size == 0);
}
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 Clear() {
_Size = 0;
_TempArray = new T[0];
delete[]OriginalArray;
OriginalArray = _TempArray;
}
void Reverse() {
_TempArray = new T[_Size];
int Counter = 0;
for (int i = _Size-1; i >=0; i--)
{
_TempArray[Counter] = OriginalArray[i];
Counter++;
}
delete[]OriginalArray;
OriginalArray = _TempArray;
}
bool DeleteItemAt(int Index) {
if (Index >= _Size || Index < 0)return false;
_Size--;
_TempArray = new T[(_Size)];
//Copy All Before Index
for (int i = 0; i <Index; i++)
{
_TempArray[i] = OriginalArray[i];
}
//Copy All After Index
for (int i =Index+1; i <_Size+1; i++)
{
_TempArray[i-1] = OriginalArray[i];
}
delete[]OriginalArray;
OriginalArray = _TempArray;
return true;
}
void DeleteFirstItem() {
DeleteItemAt(0);
}
void DeleteLastItem() {
DeleteItemAt(_Size - 1);
}
int Find(T value) {
for (int i = 0; i < _Size; i++)
{
if (OriginalArray[i] == value)
return i;
}
return -1;
}
bool DeleteItem(T value) {
int Index = Find(value);
if (Index==-1)
{
return false;
}
return DeleteItemAt(Index);
}
bool InsertAt(int index, T value) {
if (index > _Size || index < 0 ||index==NULL) {
return false;
}
_Size++;
_TempArray = new T[_Size];
//copy all before index
for (int i = 0; i < index; i++)
{
_TempArray[i] = OriginalArray[i];
}
_TempArray[index] = value;
//copy all after index
for (int i = index; i < _Size - 1; i++)
{
_TempArray[i + 1] = OriginalArray[i];
}
delete[] OriginalArray;
OriginalArray = _TempArray;
return true;
}
void InsertAtBeginning(T value) {
InsertAt(0, value);
}
bool InsertBefore(int index, T value) {
if (index < 1)
{
return InsertAt(0, value);
}
else
return InsertAt(index - 1, value);
}
void InsertAtEnd(T value) {
InsertAt(_Size, value);
}
bool InsertAfter(int index, T value) {
if (index >= _Size)
return InsertAt(_Size - 1, value);
else
return InsertAt(index+1, value);
}
};