Finish ArrayStack.

This commit is contained in:
2018-02-20 10:09:12 -08:00
parent 707d033d9f
commit 214142583b
4 changed files with 45 additions and 22 deletions

View File

@@ -23,4 +23,11 @@ main(void)
cout << "as[1] " << as.get(1) << endl;
as.remove(0);
cout << "as[0] " << as.get(0) << endl;
cout << "size: " << as.size() << ", cap: " << as.cap() << endl;
as.add(0, 47);
as.add(0, 11);
as.add(0, 17);
as.add(0, 1);
cout << "size: " << as.size() << ", cap: " << as.cap() << endl;
}

View File

@@ -11,9 +11,13 @@ template<typename T>
class ArrayStack {
public:
ArrayStack(int len) : n(0), a(Array<T>(len)) {}
int size(void) { return a.length; }
int size(void) { return n; }
int cap(void) { return a.length; }
T get(int i) { return a[i]; }
T set(int i, T x) {
T set(int i, T x) {
if (i > n) {
n = i+1;
}
T y = a[i];
a[i] = x;
return y;
@@ -35,7 +39,6 @@ public:
T remove(int i) {
T x = a[i];
for (int j = i; j < (n-1); j++) {
std::cout << j << "\t" << i << "\t" << a[j] << std::endl;
a[j] = a[j+1];
}
n--;
@@ -47,7 +50,26 @@ public:
return x;
}
void resize(void) {}
static inline int max(int x, int y) {
return x > y ? x : y;
}
void resize(void) {
int nlen = max(2 * n, 1);
Array<T> b(nlen);
for (int i = 0; i < n; i++) {
b[i] = a[i];
}
a = b;
}
T pop(void) {
return a.remove(n - 1);
}
void push(T x) {
return a.add(n, x);
}
private:
int n;
Array<T> a;