#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,就是作业题,做个笔记。被发现了,逃。哈哈哈哈哈