-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathArrayADT.cpp
More file actions
184 lines (156 loc) · 4.88 KB
/
ArrayADT.cpp
File metadata and controls
184 lines (156 loc) · 4.88 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
#include<bits/stdc++.h>
using namespace std;
// #define deb(x) cout << #x << " " << x << endl;
// template<typename... T>
// void read(T&... args){
// ((cin >> args),...);
// }
// template<typename... T>
// void write(T&&... args){
// ((cout << args << " "),...);
// cout << endl;
// }
class Array{
private:
int size = 10, length = 0;
int* arr = new int[10];
void SizeDoubler(){
int *temp, *temp1;
temp = new int[size*2];
for(int i = 0; i < size; i++) temp[i] = arr[i];
temp1 = arr;
arr = temp;
free(temp1);
temp = NULL;
size *= 2;
}
//append
public:
void append(int num){
if(length >= size) SizeDoubler();
arr[length++] = num;
}
//insert
void insert(int psn, int val){
while(psn > size-1) SizeDoubler();
if(length+1 == size) SizeDoubler();
for(int i = length-1; i >= psn; i--) arr[i+1] = arr[i];
arr[psn] = val;
length++;
}
//display
void print(){
for(int i = 0; i < length; i++) cout << arr[i] << " ";
cout << endl;
}
// delete
void del(int psn){
for(int i = psn; i < length-1; i++) arr[i] = arr[i+1];
length--;
}
// search
//linear search
void search(int val){
int i;
for(i = 0; i < length; i++) if(arr[i] == val){ cout << val << " found at " << i << endl; break; }
if (i == length) cout << val << " not found" << endl;
}
// get
int get(int psn){ return arr[psn];}
//set
void set(int psn, int val){
while(psn > size) SizeDoubler();
arr[psn] = val;
if(length <= psn) length = psn+1;
}
// max
int max(){
int ans = INT_MIN;
for(int i = 0; i < length; i++) if(arr[i] > ans) ans = arr[i];
return ans;
}
// min
int min(){
int ans = INT_MAX;
for(int i = 0; i < length; i++) if(arr[i] < ans) ans = arr[i];
return ans;
}
// reverse
void reverse(){
for(int i = 0; i <= length/2; i++){
int temp = arr[i];
arr[i] = arr[length-1-i];
arr[length-i-1] = temp;
}
}
//size of array
int len(){return length;}
//findMissingin sorted
int findMissing1();
//findMissingin unsorted
int findMissing2(int a, int b);
//pair of sum with k
void sumK(int k);
};
// int main(){
// Array *a = new Array();
// int cs, temp1, temp2;
// cout << "select a option" << endl;
// cout << " 1 for append" << endl;
// cout << " 2 for insert" << endl;
// cout << " 3 for delete" << endl;
// cout << " 4 for search" << endl;
// cout << " 5 for get value at position" << endl;
// cout << " 6 for set value at position" << endl;
// cout << " 7 for finding min" << endl;
// cout << " 8 for finding max" << endl;
// cout << " 9 for print all" << endl;
// cout << " 10 for exit" << endl;
// while(cs != 10){
// cin >> cs;
// switch(cs){
// case 1:
// cout << "Enter value to append" << endl;
// cin >> temp1;
// a->append(temp1);
// break;
// case 2:
// write("Enter position and value");
// read(temp1, temp2);
// a->insert(temp1, temp2);
// break;
// case 3:
// write("Enter position to delete");
// read(temp1);
// a->del(temp1);
// break;
// case 4:
// write("Enter value to search");
// read(temp1);
// a->search(temp1);
// break;
// case 5:
// write("Which position do you want to access");
// read(temp1);
// write(a->get(temp1));
// break;
// case 6:
// write("At which position do u want to set value");
// read(temp1);
// write("Enter the value");
// read(temp2);
// a->set(temp1, temp2);
// break;
// case 7:
// write("Minimun value in array is",a->min());
// break;
// case 8:
// write("Maximun value in array is",a->max());
// break;
// case 9:
// a->print();
// break;
// }
// }
// return 0;
// }