-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamictable.cpp
68 lines (61 loc) · 1.21 KB
/
dynamictable.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
// using aggregate method
#include<iostream>
#include<stack>
#include<cstdlib>
#include <ctime>
using namespace std;
class DynamicArray {
private: int currSize;
int capacity;
int * a;
int cost;
void resizeArray() {
if (currSize == capacity) {
cout << "DOUBLING SIZE" << endl;
int * temp = new int(2 * capacity);
for (int i = 0; i < capacity; i++) {
temp[i] = a[i];
}
free(a);
a = temp;
cost = capacity + 1;
capacity = 2 * capacity;
}
}
public: DynamicArray() {
a = new int(1);
cost = 1;
currSize = 0;
capacity = 1;
}
void insertAtEnd(int x) {
if (currSize == capacity) {
resizeArray();
}
else cost = 1;
a[currSize] = x;
currSize++;
}
int getArraySize() {
return currSize;
}
int getCost(){
return cost;
}
int getCapacity() {
return capacity;
}
};
int main() {
DynamicArray a;
int n;
cout << "Enter the number of elements ";
cin >> n;
for (int i = 0; i < n; i++) {
a.insertAtEnd(i);
cout << "Curr Size: " << a.getArraySize() << endl;
cout << "Capacity: " << a.getCapacity() << endl;
cout << "Cost: "<< a.getCost()<<endl;
}
}
// Enter the number of elements 16