Lis的实现:
/////////////////////////////////////////////////////////////////////////////// // // FileName : c2_list.h // Author : Jimmy Han // Date : N.A v1 // : 2014/07/13 12:30 v2 // /////////////////////////////////////////////////////////////////////////////// #ifndef C2_LIST_H #define C2_LIST_H #include <iostream> template <class object> class List { private: struct Node{ object data; Node *prev; Node *next; Node(const object &d = object(), Node *p = NULL, Node *n = NULL) : data(d), prev(p), next(n) {} }; public: class const_iterator { public: const_iterator() : current(NULL) {} const object& operator*() const {return retrieve();} const_iterator& operator++() { current = current->next; return *this; } const_iterator operator++(int) { const_iterator old = *this; ++(*this); return old; } const_iterator& operator--() { current = current->prev; return *this; } const_iterator operator--(int) { const_iterator old = *this; --(*this); return old; } bool operator==(const const_iterator &rhs) const {return current == rhs.current;} bool operator!=(const const_iterator &rhs) const {return !(*this == rhs);} protected: Node *current; object& retrieve() const {return current->data;} //not used by List, but used by iterator, so protected const_iterator( Node *p) : current(p) {} friend class List<object>; }; class iterator : public const_iterator { public: iterator() {} object& operator*() {return this->retrieve();} object& operator*() const {return const_iterator::operator*() ;} //return type changed, so redefine ++ iterator& operator++() { this->current = this->current->next; return *this; } const iterator operator++(int) { iterator old = *this; ++(*this); return old; } iterator& operator--() { this->current = this->current->prev; return *this; } const iterator operator--(int) { iterator old = *this; --(*this); return old; } protected: iterator(Node* p) : const_iterator(p) {} friend class List<object>; }; List() {init();} List(const List &rhs) { init(); *this = rhs; } ~List() { clear(); delete head; delete tail; } const List& operator=(const List &rhs) { if(this == &rhs) return *this; clear(); for (const_iterator iter=rhs.begin(); iter!=rhs.end(); ++iter) push_back(*iter); return *this; } iterator begin() {return iterator(head->next); } iterator end() {return iterator(tail);} const_iterator begin() const {return const_iterator(head->next); } const_iterator end() const {return const_iterator(tail);} int size() const {return _size;} bool empty() const {return size() == 0;} void clear() { while(!empty()) pop_front(); } object& front() {return *begin();} const object& front() const {return *begin();} object& back() {return *--end();} const object& back() const {return *--end();} void push_front(const object &x) {insert(begin(), x);} void push_back(const object &x) {insert(end(), x);} void pop_front() {erase(begin());} void pop_back() {erase(--end());} //insert object value as x before iter //return the position of the new inserted object iterator insert(iterator iter, const object &x) { //current is private, could be access cus of friend classs Node* p = iter.current; _size++; return iterator(p->prev = p->prev->next = new Node(x, p->prev, p)); } //erase the object iter point to //return the next object of iter iterator erase(iterator iter) { if (!empty()) { /* code */ Node* p = iter.current; iterator retVal(p->next); p->prev->next = p->next; p->next->prev = p->prev; delete p; --_size; return retVal; } else { cout << "Empty List!!" << endl; return iter; } } iterator erase(iterator start, iterator end) { for(iterator iter = start; iter != end; ) iter = erase(iter); return end; } private: int _size; Node *head; Node *tail; void init() { //loop list? _size = 0; head = new Node; tail = new Node; head->next = tail; tail->prev = head; } }; #endif
List的测试程序:
/////////////////////////////////////////////////////////////////////////////// // // FileName : c2_list.cpp // Author : Jimmy Han // Date : N.A v1 // : 2014/07/13 12:30 v2 // /////////////////////////////////////////////////////////////////////////////// #include "printCollection.h" #include "c2_list.h" using namespace std; int main() { //List(); List<string> slist; //List<string>::iterator giter; slist.erase(slist.begin()); //void push_back(const object &x) slist.push_back("a1"); slist.push_back("a2"); slist.push_front("b1"); slist.push_front("b2"); cout << "After initilization: " << endl; printCollection(slist); cout << "Front is: " << slist.front() << endl; //List(const List& rhs); List<string> slist2 = slist; //push_front(const object &x) slist2.push_front("c1"); slist2.push_front("c2"); //pop_back() slist2.pop_back(); //pop_front() slist2.pop_front(); //object & front() cout << slist2.front() << endl; //object & back() cout << slist2.back() << endl; //iterator insert(const iterator &iter, object &x) slist2.insert(++(slist2.begin()), "d1"); //erase(const_iterator iter) slist2.erase((slist2.begin())++); //erase(const_iterator start, const_iterator end) //begin(), end() for(List<string>::const_iterator iter = slist2.begin(); iter != slist2.end(); ++iter) cout << *iter << " "; cout << endl; printCollection(slist); return 0; }