-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray_as_adt.cpp
89 lines (86 loc) · 1.84 KB
/
array_as_adt.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
#include<bits/stdc++.h>
using namespace std;
class Array{
int tsize,usize,*ptr;
public:
Array(){
tsize=0;
usize=0;
ptr=NULL;
}
Array(int n){
tsize=n;
usize=0;
ptr=new int[tsize];
}
int size(){
return tsize;
}
int& operator[](int i){
return ptr[i];
}
void resize(int n){
if(n<=tsize){
tsize=n;
return;
}
int *temp = new int(n);
for(int i=0;i<tsize;i++){
temp[i]=ptr[i];
}
delete[] ptr;
ptr=temp;
tsize=n;
}
void push_back(int n){
if(usize==tsize){
resize(2*tsize);
}
ptr[usize++]=n;
}
void pop_back(){
if(usize==0){
return;
}
usize--;
if(usize<tsize/4){
resize(tsize/2);
}
}
void insert(int i,int n){
if(usize==tsize){
resize(2*tsize);
}
for(int j=usize;j>i;j--){
ptr[j]=ptr[j-1];
}
ptr[i]=n;
usize++;
}
void erase(int i){
for(int j=i;j<usize-1;j++){
ptr[j]=ptr[j+1];
}
usize--;
if(usize<tsize/4){
resize(tsize/2);
}
}
void clear(){
usize=0;
}
void print(){
for(int i=0;i<usize;i++){
cout<<ptr[i]<<" ";
}
cout<<endl;
}
};
int main(){
Array arr(3);
arr.push_back(2);
arr.push_back(4);
arr.print();
cout<<arr[1]<<endl;
return 0;
}