陣列實現連結串列
阿新 • • 發佈:2019-02-19
#include <iostream> #include <string> using namespace std; class MyListException { public: MyListException(std::string _msg) :msg(_msg) {} friend ostream& operator<<(ostream& out, const MyListException& e); private: std::string msg; }; ostream& operator<<(ostream& out, const MyListException& e) { out << e.msg; return out; } class MyList { public: MyList(int _maxSize); void insert(const int e, const int pos); //insert an element at nth position void push_back(const int e); //push back void erase(const int num); //delete the nth element. int front() const; //access the first element int back() const; //access the last element int size() const; int max_size() const; bool empty() const; MyList(const MyList&) = delete; MyList& operator=(const MyList&) = delete; ~MyList(); private: int pos_available() const; //find the available pos int head, cur, list_size; const int maxSize, null; int *list, *next; }; MyList::MyList(int _maxSize) :head(0), cur(-1), list_size(0), maxSize(_maxSize), null(_maxSize + 1) { list = new int[maxSize]; next = new int[maxSize]; for (int i = 0; i < maxSize; ++i) next[i] = null; } //insert an element at nth position. void MyList::insert(const int e, const int num) { if (num < 1 || num > list_size + 1)throw MyListException("The position is invalid."); if (list_size == maxSize)throw MyListException("List is full."); //after the cur if (num == list_size + 1) { push_back(e); return; } list_size++; int index = head, pre, pos = pos_available(); for (int i = 1; i < num; ++i) { pre = index; index = next[index]; } //before the head if (num == 1) { list[pos] = e; next[pos] = head; head = pos; return; } //other trivial conditions next[pre] = pos; list[pos] = e; next[pos] = index; } void MyList::push_back(const int e) { if (list_size == maxSize)throw MyListException("List is full."); if (list_size++ == 0) { list[head] = e; cur = head; return; } int pos = pos_available(); next[cur] = pos; list[pos] = e; cur = pos; } //delete the nth element. void MyList::erase(const int num) { if (num < 1 || num > maxSize)throw MyListException("The position is invalid."); if (list_size < num)throw MyListException("Not enough elements."); if (list_size == 0)throw MyListException("List is empty."); list_size--; int index = head, pre; for (int i = 1; i < num; ++i) { pre = index; index = next[index]; } //delete head == cur if (list_size == 0) { cur = -1; return; } //delete head element. if (num == 1) { head = next[index]; next[index] = null; return; } //delete the last; if (index == cur) { cur = pre; next[pre] = null; return; } //other trivial conditions next[pre] = next[index]; next[index] = null; } int MyList::front() const { return list[head]; } int MyList::back() const { if (list_size == 0)throw MyListException("List is empty."); return list[cur]; } int MyList::size() const { return list_size; } int MyList::max_size() const { return maxSize; } bool MyList::empty() const { return list_size == 0; } int MyList::pos_available() const { int pos = 0; for (int i = 1; i < maxSize; ++i) { pos = (i + cur) % maxSize; if (next[pos] == null)return pos; } } MyList::~MyList() { delete[] list; delete[] next; } int main() { MyList list(5); try { //do something } catch (MyListException e) { cout << e << endl; } system("pause"); return 0; }