-
Notifications
You must be signed in to change notification settings - Fork 0
/
array_list.cpp
197 lines (172 loc) · 3.86 KB
/
array_list.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
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
#include <iostream>
#include <cassert>
#include <vector>
#include <stdexcept>
#include <chrono> // for high_resolution_clock
#include <fstream> // for ofstream
#include<cmath>
using namespace std;
using namespace chrono;
//Here we create the class ArrayList
class ArrayList {
private:
int *data;
int growth = 2;
int capacity = 1;
public:
int size = 0;
ArrayList(){
data = new int[capacity];
}
ArrayList( vector<int> initial) {
size = 0;
capacity = 1024;
data = new int[capacity];
for (int e: initial) {
append(e);
}
}
public:
int length() {
return size;
}
//1b Adding a destructor
~ArrayList() {
delete[] data;
}
public:
void append(int n) {
if (size < capacity) {
data[size] = n;
} else {
resize();
data[size] = n;
}
size += 1;
};
private:
void resize(){
capacity *= growth;
int *tmp = new int[capacity];
for (int i=0; i<size; i++) {
tmp[i] = data[i];
}
delete[] data;
data = tmp;
}
public:
void print(){
cout << "[";
for (int i=0; i<size-1; i++){
cout << data[i];
cout << ", ";
}
cout << data[size-1] << "]" << endl;
}
int& operator[](int i){
if (i>=size){
throw out_of_range("index is out of range");
}
else{
return data[i];
}
}
void insert(int index,int val)
{
if(index<size)
{
if(data[index]==-1)
{
data[index]=val;
}
else{
int id;
id =data[index];
data[index]=val;
int id2;
for(int i=index+1;i<size; i++)
{
id2 = data[i];
data[i]=id;
id=id2;
if(id==-1)
break;
}
}
}
}
void remove(int index){
if (index > size){
throw out_of_range("index is out of range");
}
else{
size = size - 1;
for (int i=index; i<size; i++){
data[i] = data[i+1];
}
}
}
int pop(int index){
int val = data[index];
remove(index);
return val;
}
int pop(){
return pop(size-1);
}
//fikk ikke til shrink_to_fit
};
bool is_prime(int n) {
if (n == 1) {
return false;
}
for (int d=2; d<n; d++) {
if (n % d == 0) {
return false;
}
}
return true;
}
//test funksjon utenfor classen
void test_is_prime(){
int n=1;
ArrayList a;
while(a.length() <10){
if (is_prime(n)){
a.append(n);
}
n++;
}
a.print();
};
//int main(){
// cout << "Linked Array list - insert front \n";
// ofstream ofs{"array_list_insert.txt"};
// if (!ofs) {
// throw runtime_error("Unable to open file");
// }
// for (int N = 100; N < 1E6; N *= 10) {
// auto start = high_resolution_clock::now();
// ArrayList ll{};
// for (int i = 0; i < N; i++) {
// ll.insert(i, 0);
// }
//
// auto stop = high_resolution_clock::now();
// auto duration = duration_cast<microseconds>(stop - start);
// cout << N << " " << duration.count() / (double) N << "\n";
// ofs << N << " " << duration.count() / (double) N << "\n";
// }
// return 0;
//}
int main(){
ArrayList primes({2, 3, 5, 7, 11});
primes.print();
primes.insert(4, 2);
primes.append(99);
primes.print();
primes.remove(2);
primes.print();
cout << primes.pop() << endl;
return 0;
}