-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharrayBasedList.cpp
More file actions
90 lines (88 loc) · 1.77 KB
/
Copy patharrayBasedList.cpp
File metadata and controls
90 lines (88 loc) · 1.77 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
#include <iostream>
using namespace std;
#define MAX_SIZE 10
class arrayList{
int *Elements;
int capacity;
int n;
public:
arrayList(int capacity = MAX_SIZE){
this->capacity = capacity;
Elements = new int[capacity];
n=0;
}
void resize(){
int newcapacity;
if(capacity>0){
newcapacity = capacity*2;
}
else{
newcapacity = 1;
}
int *Temp = new int[newcapacity];
for(int i=0; i<n; i++){
Temp[i] = Elements[i];
}
delete[] Elements;
Elements = Temp;
capacity = newcapacity;
}
void add(int data){
if(n == capacity){
resize();
}
Elements[n] = data;
n++;
}
int get(int pos){
if(pos>n || pos < 0){
throw exception();
}
else{
return Elements[pos];
}
}
void remove(int pos){
if(pos>n || pos < 0) throw exception();
n--;
for(int i = pos; i<n; i++){
Elements[i] = Elements[i+1];
}
}
int length(){
return n;
}
void clear(){
delete[] Elements;
}
void insert(int data, int pos){
n++;
if(n==capacity){
resize();
}
for(int i=n-1;i>=pos; i--){
Elements[i+1] = Elements[i];
}
Elements[pos]=data;
}
~arrayList(){//destructor
delete[] Elements;
}
void print(){
for(int i=0; i<n;i++)
cout<<Elements[i]<<" ";
}
};
int main(){
arrayList array1;
array1.add(5);
array1.add(6);
array1.add(7);
array1.add(45);
array1.add(89);
array1.insert(15,2);
array1.print();
array1.remove(1);
cout<<endl;
array1.print();
}