-
Notifications
You must be signed in to change notification settings - Fork 0
/
array structure.cpp
166 lines (148 loc) · 3.11 KB
/
array structure.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
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
#include <iostream>
using namespace std;
class array
{
private:
int capacity;
int lastIndex = -1;
int *ptr;
public:
array(int);
array(array &);
bool isEmpty();
void appendElement(int);
void insertData(int, int);
void delElement(int);
bool isFull();
int getElementbyIndex(int);
int countElement();
void edit(int, int);
int elementFound(int);
int getcapacity();
~array();
void operator=(array &);
};
void array::operator=(array &arr) //operator overloading
{
capacity = arr.capacity;
lastIndex = arr.lastIndex;
if(ptr!=NULL)
delete[] ptr;
ptr = new int[capacity];
for (int i = 0; i <= lastIndex; i++)
ptr[i] = arr.ptr[i];
}
int array::getcapacity()
{
return capacity;
}
array::array(array &arr)
{
capacity = arr.capacity;
lastIndex = arr.lastIndex;
ptr = new int[capacity];
for (int i = 0; i <= lastIndex; i++)
ptr[i] = arr.ptr[i];
}
array::array(int size)
{
ptr = new int[size];
capacity = size;
}
bool array::isEmpty()
{
return lastIndex == -1;
}
void array::appendElement(int value)
{
if (lastIndex == capacity - 1)
cout << "array is full" << endl;
else
{
lastIndex++;
ptr[lastIndex] = value;
}
}
void array::insertData(int element, int index)
{
if (lastIndex == capacity - 1)
cout << "Array is full : no insertion can done" << endl;
else if (index < 0 || index > lastIndex + 1)
cout << "Invalid Index" << endl;
else
{
for (int i = lastIndex; i >= index; i--)
{
ptr[i] = ptr[i - 1];
}
ptr[index] = element;
lastIndex++;
}
}
array::~array()
{
delete[] ptr;
}
void array::delElement(int index)
{
if (isEmpty())
cout << "Array is empty" << endl;
else if (index < 0 || index > lastIndex)
cout << "invalid index" << endl;
else
{
for (int i = index; i < lastIndex; i++)
{
ptr[i] = ptr[i + 1];
}
lastIndex = -1;
}
}
void array::edit(int index, int data)
{
if (index >= 0 && index <= lastIndex)
ptr[index] = data;
}
bool array::isFull()
{
return lastIndex == capacity - 1;
}
int array::getElementbyIndex(int index)
{
if (index >= 0 && index <= lastIndex)
return ptr[index];
cout << "Invalid index or empty array" << endl;
return -1;
}
int array::countElement()
{
return lastIndex + 1;
}
int array::elementFound(int element)
{
int i = 0;
for (i; i == capacity - 1; i++)
{
if (ptr[i] == element)
return i;
}
return -1;
}
// int main()
// {
// array obj(5);
// if (obj.isEmpty())
// cout << "Empty Array";
// obj.appendElement(20);
// obj.appendElement(40);
// obj.appendElement(60);
// obj.insertData(1, 30);
// for (int i = 0; i < obj.countElement(); i++)
// cout << obj.getElementbyIndex(i) << " ";
// obj.delElement(2);
// cout << endl;
// for (int i = 0; i < obj.countElement(); i++)
// cout << obj.getElementbyIndex(i) << " ";
// cout << endl;
// return 0;
// }