//ArrayCollection.h #ifndef ArrayCollection_h #define ArrayCollection_h template <class T> class ArrayCollection{ T* _data; int _size; public: ArrayCollection():_size(10){ _data = new T[_size]; } ArrayCollection(int size):_size(size){ _data = new T[_size]; } ArrayCollection(int size, T* data):_size(size){ _data = new T[_size]; for(int i = 0; i < size; i++) *(_data + i) = *(data + i); } ~ArrayCollection(){ delete[] _data; } int size(){ return _size; }//其实这个函数没有用上 T* begin(){ return _data; } T* end(){ return (_data + _size); } }; #endif //LinkedListIterator.h #ifndef LinkedListIterator_h #define LinkedListIterator_h template <class T> struct LinkedListNode{ T _data; LinkedListNode *next; LinkedListNode():next(NULL){} LinkedListNode(T data):_data(data), next(NULL){} }; template <class T> struct LinkedListIterator{ LinkedListNode<T> *pointer; LinkedListIterator(LinkedListNode<T> *p):pointer(p){} LinkedListIterator(const LinkedListIterator<T>& it):pointer(it.pointer){} LinkedListIterator<T>& operator++(){ pointer = pointer->next; return *this; } const LinkedListIterator<T> operator++(int){ LinkedListIterator<T> temp = *this; pointer = pointer->next; return temp; } T& operator*() const{ return pointer->_data; } T* operator->() const{ return &(pointer->_data); } bool operator!=(const LinkedListIterator<T> &other){ return pointer != other.pointer; } bool operator==(const LinkedListIterator<T> &other){ return pointer == other.pointer; } }; #endif //LinkedListCollection.h #ifndef LinkedListCollection_h #define LinkedListCollection_h template <class T> class LinkedListCollection{ LinkedListNode<T>* _head; public: LinkedListIterator<T> begin(){ return LinkedListIterator<T>(_head); } LinkedListIterator<T> end(){ return LinkedListIterator<T>(NULL); } LinkedListCollection():_head(NULL){} LinkedListCollection(int size, T* data){ //... _head = NULL; for(int i = 0; i < size; i++){ LinkedListNode<T>* Node_data = new LinkedListNode<T>; Node_data->_data = *(data + i); Node_data->next = _head; _head = Node_data; } }//这个函数视频里省略了,我自己写的 ~LinkedListCollection(){ //... while(_head){ LinkedListNode<T>* tmp = _head; _head = _head->next; delete tmp; } }//这个函数视频里省略了,我自己写的 }; #endif
