-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayList (Vector).cpp
More file actions
266 lines (264 loc) · 7.19 KB
/
ArrayList (Vector).cpp
File metadata and controls
266 lines (264 loc) · 7.19 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
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
// Array-list is known as vector in cpp
#include<iostream>
#include<cstdio>
#include<windows.h>
using namespace std;
template <class TYPE>
class MyArray
{
TYPE *ptr;
int Size,cap,icap; //cap: Capacity, icap: Initial capacity
public:
MyArray()
{
Size=0;
ptr=(TYPE*)malloc(10*sizeof(TYPE));
cap=icap=10;
}
MyArray(int Cap) //Constructor
{
Size=0;
ptr=(TYPE*) malloc(Cap*sizeof(TYPE));
cap=icap=Cap;
}
MyArray(const MyArray &x) //Copy Constructor
{
Size=x.Size;
cap=x.cap;
icap=x.icap;
ptr=(TYPE*) malloc(cap*sizeof(TYPE));
for(int i=0;i<Size;i++)
{
ptr[i]=x.ptr[i];
}
}
void push_front(TYPE x)
{
if(Size==cap) //Increasing capacity
{
cap=cap+icap;
ptr=(TYPE*)realloc(ptr,cap*sizeof(TYPE));
}
for(int i=Size;i>0;i--) //shifting right
{
ptr[i]=ptr[i-1];
}
ptr[0]=x;
Size++;
}
void push_back(TYPE x)
{
if(Size==cap) //Increasing capacity
{
cap=cap+icap;
ptr=(TYPE*)realloc(ptr,cap*sizeof(TYPE));
}
ptr[Size]=x;
Size++;
}
void push_at(int pos)
{
if(pos>0 && pos<=(Size+1)) //bound checking
{
TYPE x;
printf("Enter value: ");
cin>>x;
if(Size==cap) //increasing capacity
{
cap=cap+icap;
ptr=(TYPE*)realloc(ptr,cap*sizeof(TYPE));
}
for(int i=Size;i>=pos;i--) //shifting right
{
ptr[i]=ptr[i-1];
}
ptr[pos-1]=x;
Size++;
}
else printf("Entered position is not a valid!\n");
}
TYPE pop_front()
{
if(Size>0) // bound checking
{
TYPE x;
x=ptr[0];
for(int i=0;i<Size-1;i++) // shifting left
{
ptr[i]=ptr[i+1];
}
Size--;
if(cap-icap==Size+1) // decreasing capacity
{ // Size+1 for 1 more space
cap=cap-icap;
ptr=(TYPE*)realloc(ptr,cap*sizeof(TYPE));
}
return x;
}
printf("Array is Empty!\n");
return -1;
}
TYPE pop_back()
{
if(Size>0) // bound checking
{
TYPE x=ptr[Size-1];
Size--;
if(cap-icap==Size+1) // decreasing capacity
{
cap=cap-icap;
ptr=(TYPE*)realloc(ptr,cap*sizeof(TYPE));
}
return x;
}
printf("Array is Empty!!\n");
return -1;
}
TYPE pop_at(int pos)
{
if(pos>0 && Size>=pos) // bound checking
{
TYPE x=ptr[pos-1];
for(int i=pos-1;i<Size;i++) ptr[i]=ptr[i+1]; //shifting left
Size--;
if(cap-icap==Size+1) // decreasing capacity
{
cap=cap-icap;
ptr=(TYPE*)realloc(ptr,cap*sizeof(TYPE));
}
return x;
}
printf("Entered position is not a valid!\n");
return -1;
}
void update(int pos)
{
if(pos>0 && pos<=Size) // bound checking
{
TYPE x;
printf("Enter Value: ");
cin>>x;
ptr[pos-1]=x; // pos-1: position and index is not same
}
else printf("Entered position is not a valid!\n");
}
void display()
{
for(int i=0;i<Size;i++) cout<<ptr[i]<<" ";
printf("\n");
}
void shrink() //No free space will remain
{
if(Size<cap)
{
ptr=(TYPE*)realloc(ptr,Size*sizeof(TYPE));
cap=Size;
}
}
bool empty() // IsEmpty?
{
if(Size==0) return true;
return false;
}
int capacity()
{
return cap;
}
int size()
{
return Size;
}
int search(TYPE x) // Return Index
{
for(int i=0;i<Size;i++)
if(ptr[i]==x)
return i;
return -1;
}
~MyArray()
{
free(ptr);
ptr=NULL;
}
};
int main()
{
int ans,Siz,pos;
int x;
printf("Enter the size of the array: ");
scanf("%d",&Siz);
MyArray<int> ls(Siz);
system("cls");
printf("Choose one of the option:\n");
printf("1. Insert element at the first position of the array\n");
printf("2. Insert element at the last position of the array\n");
printf("3. Insert element at any middle position of the array\n");
printf("4. Delete an element from the first position of the array\n");
printf("5. Delete an element from the last position of the array\n");
printf("6. Delete an element from any position of the array\n");
printf("7. Update any value of any position of the array\n");
printf("8. Print the array\n");
printf("9. Search\n");
printf("10. Quit\n");
while(1)
{
printf("Enter Option: ");
cin>>ans;
if(ans==1)
{
printf("Enter Value: ");
cin>>x;
ls.push_front(x);
}
else if(ans==2)
{
printf("Enter Value: ");
cin>>x;
ls.push_back(x);
}
else if(ans==3)
{
printf("Enter the position: ");
cin>>pos;
ls.push_at(pos);
}
else if(ans==4)
{
x=ls.pop_front();
if(x!=-1) cout<<x<<" is popped!\n";
}
else if(ans==5)
{
x=ls.pop_back();
if(x!=-1) cout<<x<<" is popped!\n";
}
else if(ans==6)
{
printf("Enter the position: ");
cin>>pos;
x=ls.pop_at(pos);
if(x!=-1) cout<<x<<" is popped!\n";
}
else if(ans==7)
{
printf("Enter the position: ");
cin>>pos;
ls.update(pos);
}
else if(ans==8)
{
ls.display();
}
else if(ans==9){
printf("Value: ");
cin>>x;
pos=ls.search(x);
if(pos!=-1) cout<<x<<" is at "<<pos<<" position\n";
else printf("Not found!\n");
}
else break;
}
system("cls");
printf("Thank You!!!\n\n\n\n");
return 0;
}