-
Notifications
You must be signed in to change notification settings - Fork 88
/
ARRAY ADT CODE.cpp
208 lines (199 loc) · 3.7 KB
/
ARRAY ADT CODE.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
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
//construction of array ADT
#include<bits/stdc++.h>
using namespace std;
class ArrayADT
{
private:
int capacity;
int lastIndex;
int *ptr;
public:
ArrayADT ()
{
capacity = 0;
lastIndex = -1;
ptr = NULL;
}
void createArray (int capacity);
int getItem (int index);
void setItem (int index, int value);
void editItem (int index, int value);
int countItem ();
void removeItem (int index);
int searchItem (int value);
void sortArray ();
friend ostream& operator<<(ostream&,ArrayADT);
~ArrayADT(){
delete []ptr;
}
ArrayADT(ArrayADT &List){ //copy constuctor making deep copy rather than making the shallow one .....
capacity = List.capacity;
lastIndex=List.lastIndex;
ptr=new int[capacity];
for (int i = 0; i <= List.lastIndex; i++) {
ptr[i]=List.ptr[i];
}
}
};
ostream& operator << (ostream &dout,ArrayADT l){ // insertion overloading
if(l.lastIndex==-1){
dout<<"\n list is empty";
}
else {
for (int i = 0; i < l.countItem(); i++) {
dout<<" "<<l.getItem(i);
}
}
return(dout);
}
void
ArrayADT::createArray (int capacity)
{
this->capacity = capacity;
lastIndex = -1;
ptr = new int[capacity];
}
int
ArrayADT::getItem (int index)
{
if (lastIndex == -1)
{
cout << "\n List is empty";
return (-1);
}
if (index > lastIndex || index < 0)
{
cout << "\n invalid index";
return (-1);
}
return (ptr[index]);
}
void
ArrayADT::setItem (int index, int value)
{
int i;
if (lastIndex == capacity - 1)
{
cout << "\n overflow";
}
else if (index > capacity - 1 || index < 0)
{
cout << "\n Invalid index";
}
else if (index > lastIndex + 1)
{
cout << "\ninvalid index";
}
else if (index <= lastIndex)
{
lastIndex++;
i = lastIndex;
while (i != index)
{
ptr[i] = ptr[i - 1];
i--;
}
ptr[index] = value;
}
else if (index == lastIndex + 1)
{
lastIndex++;
ptr[index] = value;
}
}
void
ArrayADT::editItem (int index, int value)
{
if (index < 0 || index > lastIndex)
{
cout << "\n invalid index";
}
else
{
ptr[index] = value;
}
}
int
ArrayADT::countItem ()
{
return (lastIndex + 1);
}
void
ArrayADT::removeItem (int index)
{
int i;
if (index < 0 || index > lastIndex)
{
cout << "\neither invalid or underflow ";
}
else if (index < lastIndex)
{
i = index;
while (i != lastIndex)
{
ptr[i] = ptr[i + 1];
i++;
}
lastIndex--;
}
else if (index == lastIndex)
{
lastIndex--;
}
}
int
ArrayADT::searchItem (int value)
{
if (lastIndex == -1)
{
cout << "\n list is empty";
return (-1);
}
int i;
for (int i = 0; i <= lastIndex; i++)
{
if (ptr[i] == value)
{
return (i);
}
}
return (-1);
}
void
ArrayADT::sortArray ()
{
int r, i, t;
for (r = 1; r < countItem (); r++)
{
for (i = 0; i < countItem () - r; i++)
{
if (ptr[i] > ptr[i + 1])
{
t = ptr[i];
ptr[i] = ptr[i + 1];
ptr[i + 1] = t;
}
}
}
}
int
main ()
{
ArrayADT l1;
l1.createArray (5);
l1.setItem (0, 10);
l1.setItem (1, 20);
l1.setItem (2, 34);
l1.setItem (3, 45);
l1.editItem (2, 35);
// int x = l1.countItem ();
// cout << x << endl;
// l1.removeItem (2);
// int y = l1.countItem ();
// cout << y << endl;
// int z = l1.searchItem (45);
// cout << z << endl;
// l1.sortArray ();
cout<<l1;
// cout<<l1.getItem(3);
}