运用到的知识:(不分先后)
模板,类,循环数组,线性表,指针,异常,迭代器,输入输出等C++基础
using namespace std;
class EmptyListException :public exception
{
public:
EmptyListException(){}
};
class InvalidIteratorException :public exception
{
public:
InvalidIteratorException(){}
};
template<class T>
class ArrayList {
public:
class Iterator : public iterator <input_iterator_tag, T> {
public:
Iterator(T* p,T* elem,int cnt) {
_ptr = p;
selfElems = elem;
count = cnt;
}
bool operator != (const Iterator &iter) {
return _ptr!= iter._ptr;
}
bool operator == (const Iterator &iter) {
return _ptr == iter._ptr;
}
Iterator& operator ++ () {
_ptr++;
if(_ptr >= selfElems+count) _ptr = selfElems;
return *this;
}
Iterator& operator -- () {
_ptr--;
if(_ptr < 0) _ptr = selfElems+count-1;
return *this;
}
Iterator operator ++ (int) {
Iterator tmp= *this;
_ptr++;
if(_ptr >= selfElems+count) _ptr = selfElems;
return tmp;
}
Iterator operator -- (int) {
Iterator tmp= *this;
_ptr--;
if(_ptr < 0) _ptr = selfElems+count-1;
return tmp;
}
T& operator * () {
return *_ptr;
}
public:
T* _ptr;
T* selfElems;
int count;
};
ArrayList() {
_selfElems = new T[4];
_count = 4;
init();
}
ArrayList(int n) {
_selfElems = new T[n];
_count = n;
init();
}
~ArrayList() {
delete[] _selfElems;
}
void init() {
_front=0;
_back=-1;
nowsize = 0;
}
T& operator[](int i) {
return _selfElems[i];
}
Iterator begin() {
return Iterator(_selfElems+_front,_selfElems,_count);
}
Iterator end() {
return Iterator(_selfElems + (_back + 1)%_count,_selfElems,_count);
}
T& front() {
return _selfElems[_front];
}
T& back() {
return _selfElems[_back];
}
int size() const {
return nowsize;
}
void insertFront(T& e) {
if(nowsize == _count) {
T* _selfElems2 = new T[_count*2];
for(int i = 0; i<_count; i++) {
_selfElems2[i] = _selfElems[(_front+i)%_count];
}
delete[] _selfElems;
_selfElems = _selfElems2;
_front=0;_back=_count-1;
_count*=2;
}
if(_front == 0) _front=_count-1;
else _front--;
_selfElems[_front] = e;
nowsize++;
}
void insertBack(T&e) {
if(nowsize == _count) {
T* _selfElems2 = new T[_count*2];
for(int i = 0; i<_count; i++) {
_selfElems2[i] = _selfElems[(_front+i)%_count];
}
delete[] _selfElems;
_selfElems = _selfElems2;
_front=0;
_back=_count-1;
_count*=2;
}
_back = (_back+1) % _count;
_selfElems[_back] = e;
nowsize++;
}
void insert(Iterator p, T& e) {
if(p._ptr < _selfElems || p._ptr >= _selfElems+_count||(begin()._ptr < end()._ptr && (p._ptr < begin()._ptr || p._ptr >= end()._ptr)) || (begin()._ptr > end()._ptr && (p._ptr < begin()._ptr && p._ptr >= end()._ptr))) {
throw InvalidIteratorException();
}
if(nowsize == _count) {
T* _selfElems2 = new T[_count*2];
for(int i = 0; i<_count; i++) {
_selfElems2[i] = _selfElems[(_front+i)%_count];
}
delete[] _selfElems;
_selfElems = _selfElems2;
_front=0;_back=_count-1;
_count*=2;
}
int pos = p._ptr - begin().ptr;
int len = (_back-1)-(pos+_front);
for(int i = len; i>=0; i++) {
int tar = i + pos+_front;
_selfElems[(tar+1)%_count] = _selfElems[tar%_count];
}
_selfElems[pos+_front] = e;
nowsize++;
}
void removeFront() {
if(nowsize == 0) {
throw EmptyListException();
}
_front = (_front+1)%_count;
nowsize--;
}
void removeBack() {
if(nowsize == 0) {
throw EmptyListException();
}
_back = (_back+_count-1)%_count;
nowsize--;
}
void remove(Iterator p) {
if(nowsize == 0) {
throw EmptyListException();
}
if(p._ptr < _selfElems || p._ptr >= _selfElems+_count||(begin()._ptr < end()._ptr && (p._ptr < begin()._ptr || p._ptr >= end()._ptr)) || (begin()._ptr > end()._ptr && (p._ptr < begin()._ptr && p._ptr >= end()._ptr))) {
throw InvalidIteratorException();
}
int pos = p._ptr - begin()._ptr;
//pos+_front ~ (back-1)
int len = (_back-1)-(pos+_front);
for(int i = 0; i<len; i++) {
int tar = i + pos+_front;
_selfElems[tar%_count] = _selfElems[(tar+1)%_count];
}
nowsize--;
}
private:
int _count;
int _front,_back,nowsize;
T* _selfElems;
};
void testArrayListUnderflow() {
// the first try block
try {
ArrayList<int> arr;
int x = 1;
arr.insertBack(x);
arr.removeFront();
arr.removeFront();
printf("did not catch exception\n");
} catch(EmptyListException& e) {
printf("caught EmptyStackException\n");
}
// the second try block
try {
ArrayList<int> arr;
int x = 1;
arr.insertBack(x);
arr.removeBack() ;
arr.removeBack() ;
printf("did not catch exception\n");
} catch(EmptyListException& e) {
printf("caught EmptyStackException\n");
}
// the third try block
try {
ArrayList<int> arr;
int x = 1;
arr.insertBack(x);
ArrayList<int>::Iterator it = arr.begin();
arr.remove(it);
it = arr.begin();
arr.remove(it);
printf("did not catch exception\n");
} catch(EmptyListException& e) {
printf("caught EmptyStackException\n");
}
}
void testIntegerIterator() {
ArrayList<int> arr;
int a[6] = {1,2,3,4,5,6};
for(int i = 0; i<6; i++) arr.insertBack(a[i]);
for(ArrayList<int>::Iterator iter = arr.begin(); iter != arr.end(); iter++) {
cout << *iter << " ";
}
cout << endl;
arr.removeFront();
arr.removeFront();
arr.removeFront();
int b[3] = {7,8,9};
arr.insertBack(b[0]);
arr.insertBack(b[1]);
arr.insertBack(b[2]);
for(ArrayList<int>::Iterator iter = arr.begin(); iter != arr.end(); iter++) {
cout << *iter << " ";
}
cout << endl;
}
void testStringIterator() {
ArrayList<string> arr;
string a[6] = {"s1","s2","s3","s4","s5","s6"};
for(int i = 0; i<6; i++) arr.insertBack(a[i]);
for(ArrayList<string>::Iterator iter = arr.begin(); iter != arr.end(); iter++) {
cout << *iter << " ";
}
cout << endl;
arr.removeFront();
arr.removeFront();
arr.removeFront();
string b[3] = {"s7","s8","s9"};
arr.insertBack(b[0]);
arr.insertBack(b[1]);
arr.insertBack(b[2]);
for(ArrayList<string>::Iterator iter = arr.begin(); iter != arr.end(); iter++) {
cout << *iter << " ";
}
cout << endl;
}
int main() {
testArrayListUnderflow();
testIntegerIterator();
testStringIterator();
return 0;
}
参考资料:
http://c.biancheng.net/view/471.html