-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray ADT.cpp
More file actions
228 lines (205 loc) · 4.93 KB
/
array ADT.cpp
File metadata and controls
228 lines (205 loc) · 4.93 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
222
223
224
225
226
227
228
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
class Array {
int *a , size , length ;
public:
Array (int size ) {
this->size = size ;
a = new int [size] ;
length = 0 ;
}
void addelement(int e) {
if (length < size)
a[length++] = e;
else cout << "Not enough space to add " << e << endl ;
}
void insertelement(int index , int element) {
int i;
if (index > length) cout << cout << "Index out of bound.\n" ;
else if (length < size ) {
for (i = length - 1 ; i > index -1; i--)
a[i + 1] = a[i] ;
a[index] = element ;
length ++ ;
}
else cout << "Not enough space to insert " << element << endl ;
}
int deleteelement(int index) {
int i , element;
if (index > length) return -1 ;
else {
element = a[index] ;
for (i = index ; i < length - 1 ; i++)
a[i] = a[i+1] ;
length -- ;
return element ;
}
}
void reverse () {
int i , j , temp;
for (i = 0 , j = length - 1 ; i < j ; i++ , j--)
swap(a[i] , a[j]) ;
}
Array merge( Array &a1) {
Array arr(length + a1.length) ;
arr.length = length + a1.length ;
int i = 0 , j = 0 , k = 0;
while (i < length and j < a1.length ) {
if (a[i] < a1.a[j])
arr.a[k++] = a[i++] ;
else if (a[i] > a1.a[j]) arr.a[k++] = a1.a[j++] ;
else {
arr.a[k++] = a[i++] ;
arr.a[k++] = a1.a[j++] ;
}
}
for (;i < length ; i++)
arr.a[k++] = a[i] ;
for (;j < a1.length ; j++)
arr.a[k++] = a1.a[j] ;
return arr ;
}
Array unionarr( Array &a1) {
Array arr(length + a1.length) ;
int i , j , k ;
i = j = k = 0 ;
while (i < length and j < a1.length ) {
if (a[i] < a1.a[j])
arr.a[k++] = a[i++] ;
else if (a[i] > a1.a[j]) arr.a[k++] = a1.a[j++] ;
else {
arr.a[k++] = a[i++] ;
j++ ;
}
}
for (;i < length ; i++)
arr.a[k++] = a[i] ;
for (;j < a1.length ; j++)
arr.a[k++] = a1.a[j] ;
arr.length = k ;
return arr ;
}
Array intersection( Array &a1) {
Array arr(length) ;
int i , j , k ;
i = j = k = 0 ;
while (i < length ) {
if (a[i] == a1.a[j]) {
arr.a[k++] = a[i++] ;
j++;
}
else if (a[i] > a1.a[j]) j++ ;
else if (a[i] < a1.a[j]) i++ ;
}
arr.length = k ;
return arr ;
}
Array differenciation( Array &a1) {
Array arr(length + a1.length) ;
int i , j , k ;
i = j = k = 0 ;
while (i < length and j < a1.length ) {
if (a[i] == a1.a[j]) {
i++;
j++;
}
else if (a[i] < a1.a[j])
arr.a[k++] = a[i++] ;
else if (a[i] > a1.a[j])
arr.a[k++] = a1.a[j++] ;
}
for (;i < length ; i++)
arr.a[k++] = a[i] ;
for (;j < a1.length ; j++)
arr.a[k++] = a1.a[j] ;
arr.length = k ;
return arr ;
}
int binarysearch (int element) {
int low = 0 , high = length - 1 , mid ;
while (low <= high ) {
mid = (low + high) / 2;
if (a[mid] == element) return mid;
else if (a[mid] < element) low = mid + 1;
else high = mid - 1;
}
return -1;
}
void print() {
cout << "Elements of array are: " << endl ;
for (int i = 0 ; i < length ; i++)
cout << a[i] << ' ' ;
cout << endl ;
}
};
int main() {
int size , del , index , i , element , choice;
Array res(20);
cout << "Enter array size:" ;
cin >> size ;
Array a (size) ;
cout << "Enter elements of array. Enter -1 if you want to terminate before array is filled\n" ;
for (i = 0 ; i < size ; i++){
cin >> element;
if (element == -1 ) break ;
else
a.addelement(element) ;
}
a.print();
cout << "Enter size of second array:" ;
cin >> size ;
Array b(size);
cout << "Enter elements of array. Enter -1 if you want to terminate before array is filled\n" ;
for (i = 0 ; i < size ; i++){
cin >> element;
if (element == -1 ) break ;
else
b.addelement(element) ;
}
b.print();
cout << "MEnu\n" ;
cout << "1.Add element\n2.Delete element\n3.Insert element\n4.Search Element\n5.Reverse Array\n6.Merge Arrays\n7.Union of Arrays\n8.Intersection of Arrays\n" ;
cout << "9.Diffrenciation of arrays\nEnter choice" ;
cin >> choice ;
if (choice == 1) {
cout << "Enter element to add: " ;
cin >> element ;
a.addelement(element) ;
}
else if (choice == 2) {
cout << "Enter index of element to delete: " ;
cin >> index ;
del = a.deleteelement(index);
if (del == -1) cout << "Index out of bound\n" ;
else
cout << "Deleted element is " << del << endl;
}
if (choice == 3) {
cout << "Enter element to insert: " ;
cin >> element ;
cout << "Enter index: " ;
cin >> index;
a.insertelement(index,element) ;
}
if (choice == 4) {
cout << "Enter element to search: " ;
cin >> element ;
index = a.binarysearch(element) ;
if (index == -1) cout << "Element not found\n" ;
else cout << "Element is found at index " << index << endl ;
}
if (choice == 5)
a.reverse() ;
if (choice == 6)
res = a.merge(b) ;
if (choice == 7)
res = a.unionarr(b) ;
if (choice == 8)
res = a.intersection(b);
if (choice == 9)
res = a.differenciation(b);
if (choice <= 5) a.print();
else res.print();
}