自定义数组类(动态数组实现)

#include <cstddef>
#define DEFAULT_MAX_SIZE 64
typedef int E;

class ArrayList {
 public:
  ArrayList();
  ~ArrayList();

  void add(E e);
  void remove(E e);
  void clear();

  bool contain(E e);
  bool isEmpty();
  E& operator[](int index);
  E& get(int index);
  int indexOf(E element);
  int size();

 private:
  E* storage;
  int _size;
  int _maxsize;
  static const int extend_factor = 2;
  void extend();
};

ArrayList::ArrayList(){
  storage = new E[DEFAULT_MAX_SIZE];
  _maxsize = DEFAULT_MAX_SIZE;
  _size=0;
}
ArrayList::~ArrayList(){
  delete[]storage;
}
void ArrayList::extend(){
  E*a=new E[_maxsize*extend_factor];
  for(int i=0;i<_maxsize;++i){
    a[i]=storage[i];
  }
  _maxsize *= extend_factor;
  E*t=a;
  a=storage;
  storage=t;
  delete[]a;
}
void ArrayList::clear(){
  delete[]storage;
  storage=new E[_maxsize];
  _size=0;
}

bool ArrayList::isEmpty(){
  return _size==0;
}

void ArrayList::add(E e){
  if(_size==_maxsize)extend();
  storage[_size++]=e;
}
void ArrayList::remove(E e){
  E*t=new E[_maxsize];
  int tot = 0; 
  for(int i=0;i<_size;i++){
    if(storage[i]==e)continue;
	t[tot++]=storage[i]; 
  }
  E*temp = t;
  t = storage;
  storage = temp;
  delete []t;
  _size = tot;
}
int ArrayList::indexOf(E element){
  for(int i=0;i<_size;++i){
    if(storage[i]==element)return i;
  }
  return -1;
}
int ArrayList::size(){
  return _size;
}
bool ArrayList:: contain(E e){
  for(int i=0;i<_size;++i){
    if(e==storage[i])return true;
  }
  return false;
}

E& ArrayList:: operator[](int index){
  if(index<_size)return storage[index];
}

E& ArrayList:: get(int index){
  if(index<_size)return storage[index];
}

#include <iostream>
#include <cstdlib>

using std::cin;
using std::cout;
using std::endl;

class TEST {
 private:
  int *testData;
  int data_size;

 public:
  TEST() {
#if defined(_GLIBCXX_ALGORITHM) || defined(_GLIBCXX_LIST) || defined(_GLIBCXX_VECTOR)
    cout << "FORBIDDEN!" << endl;
#endif
    cin >> data_size;
    cout << "test data size:" << data_size << endl;
    testData = new int[data_size];
    for (int i = 0; i < data_size; i++) {
      cin >> testData[i];
    }
  }

  ~TEST() { delete[] testData; }

  void testArrayList(ArrayList *c) {
    cout << (c->isEmpty() ? "true" : "false") << endl;

    int n = data_size;

    for (int i = 0; i < n; i++) {
      c->add(testData[i]);
    }

    for (int i = 0; i < c->size(); i++) {
      cout << (*c)[i] << " ";
    }

    cout << endl;

    cout << (c->isEmpty() ? "true" : "false") << endl;

    for (int i = 0; i < n / 2; i++) {
      cout << "(" << i << ", "
           << (c->contain(i) ? "true" : "false");
      cout << ","
           << c->indexOf(i)
           << ") ";
      c->remove(i);
    }

    cout << endl;

    for (int i = 0; i < c->size(); i++) {
      cout << (*c)[i] << " ";
    }
    cout << endl;

    cout << "remaining size: " << c->size() << endl;

    c->clear();
    cout << (c->isEmpty() ? "true" : "false") << endl;
  }

  void runAllTests() {
    ArrayList *c = new ArrayList();
    testArrayList(c);
    delete c;
  }
};

int main() {
  TEST t;
  t.runAllTests();
  return 0;
}

//良心附有main函数,测试类是原来就有的,hihi,就是作业题,做个笔记。被发现了,逃。哈哈哈哈哈

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值