-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusredefineddatastructure.cpp
More file actions
65 lines (63 loc) · 1.32 KB
/
Copy pathusredefineddatastructure.cpp
File metadata and controls
65 lines (63 loc) · 1.32 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
#include<iostream>
using namespace std;
class Vector{
public:
int size;
int capacity;
int* arr;
Vector(){
size=0;
capacity=1;
arr=new int[1];
}
void add(int ele){
if(size==capacity){
capacity*=2;
int* arr2=new int[capacity];
for(int i=0;i<size;i++){
arr2[i]=arr[i];
}
arr=arr2;
}
arr[size++]=ele;
}
void print(){
for(int i=0;i<size;i++){
cout<<arr[i]<<" ";
}
cout<<endl;
}
int get(int idx){
if(size==0){
cout<<"Array is empty"<<endl;
return -1;
}
if(idx>=size || idx<0){
cout<<"Invalid Index"<<endl;
return -1;
}
return arr[idx];
}
void remove(){
if(size==0){
cout<<"Array is empty"<<endl;
}
size--;
}
};
int main(){
Vector v;
cout<<v.size<<" "<<v.capacity<<endl;
v.add(10);
v.print();
cout<<v.size<<" "<<v.capacity<<endl;
v.add(6);
v.print();
cout<<v.size<<" "<<v.capacity<<endl;
v.add(8);
v.print();
cout<<v.size<<" "<<v.capacity<<endl;
cout<<v.get(10)<<endl;
v.remove();
v.print();
}