-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathearray.h
277 lines (265 loc) · 5.24 KB
/
earray.h
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include <iostream>
#include <exception>
using namespace std;
class Exception : public std::exception
{
std::string _msg;
public:
Exception(const std::string& msg) : _msg(msg){}
virtual const char* what() const noexcept override
{
return _msg.c_str();
}
};
class EArray
{
public:
EArray();
~EArray();
int *arr = new int[size];
/**
* @brief number of item in the array.
*
*/
int length;
int indexOf(int item);
int at(int index);
void print();
void pushBack(int n);
int isEmpty();
void insert(int value,int index);
bool contains(int n);
void prepend(int item);
int pop();
void deleteAt(int index);
void remove(int item);
int find(int item);
void sort();
private:
/**
* @brief actual size of the array.
*
*/
int size;
void resize(int n);
};
EArray::EArray()
{
length = 0;
size = 5;
arr[0] = 1;
}
EArray::~EArray()
{
delete[] arr;
}
/**
* @brief Sorting in ascending order.
*
*/
void EArray::sort(){
for (int i = 0; i <= length-2; i++)
{
for (int j = 0; j <= length-(i+2); j++)
{
if(arr[j]>arr[j+1]){
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
/**
* @brief Find if a item is present inside the array or not.
*
* @param item
* @return The index of the item if found else returns -1.
*/
int EArray::find(int item){
for (int i = 0; i < length; i++)
{
if(arr[i]==item){
return i;
}
}
return -1;
}
/**
* @brief removes the first matching item.
*
* @param item
*/
void EArray::remove(int item){
deleteAt(indexOf(item));
}
/**
* @brief Deletes the item present at the index
*
* @param index index of the item
*/
void EArray::deleteAt(int index){
if(index>length-1){
throw Exception("index out of range");
}else{
for (int i = index; i < length-1; i++)
{
arr[i]=arr[i+1];
}
arr[length-1]=0;
length--;
}
}
/**
* @brief remove item from the end of the array.
*
* @return the item removed.
*/
int EArray::pop(){
if(length==0){
throw Exception("array is empty");
}else{
int pop_value = arr[length-1];
arr[length-1]=0;
length--;
if(length<size/4){
resize(size/2);
}
return pop_value;
}
}
/**
* @brief Append a new item at the 0th position in the array.
*
* @param item item to append at 0th position.
*/
void EArray::prepend(int item){
insert(item,0);
}
/**
* @brief Check if the item is present in the array or not
*
* @param item
* @return true if item is present
* @return false if item is not present
*/
bool EArray::contains(int item){
for (int i = 0; i < length; i++)
{
if(arr[i]==item){
return true;
}
}
return false;
}
/**
* @brief check if the array is empty.
*
* @return 1 if the array is empty else -1.
*/
int EArray::isEmpty(){
if(length==0){
return 1;
}else{
return -1;
}
}
/**
* @brief insert a new item at any index.
*
* @param value item to be inserted.
* @param index index of item.
*/
void EArray::insert(int value,int index){
if(index>length-1){
throw Exception("Index out of range.");
}else{
if(length<size){
for (int i = length; i > index; i--)
{
arr[i]=arr[i-1];
}
arr[index]=value;
length++;
}else{
resize(size+1);
insert(value,index);
}
}
}
/**
* @brief print the whole array.
*
*/
void EArray::print(){
cout << "Array containes: [";
for (int i = 0; i < length; i++)
{
i == length - 1 ? cout << arr[i] : cout << arr[i] << ","<< " ";
}
cout << "]" << endl;
}
/**
* @brief resize the array.
*
* @param n new size
*/
void EArray::resize(int n)
{
int *temparr = new int[n];
for (int i = 0; i < length; i++)
{
temparr[i] = arr[i];
}
delete[] arr;
size = n;
arr = temparr;
}
/**
* @brief Return the index of the given item if present in the array.
*
* @param item item in the array.
* @return int index
*/
int EArray::indexOf(int item)
{
for (int i = 0; i < length; i++)
{
if(arr[i]==item){
return i;
}
}
throw Exception("Item not found");
}
/**
* @brief Access items from the array.
*
* @param index
* @return integer at the index.
*/
int EArray::at(int index)
{
if(index<length){
return arr[index];
}else{
throw Exception("index out of range");
}
}
/**
* @brief add a new item in the end of the array.
*
* @param n item to be inserted
*/
void EArray::pushBack(int n)
{
if (length == size)
{
resize(size*2);
pushBack(n);
}
else
{
arr[length] = n;
length++;
}
}