今天把List容器给实现了一遍。List容器的实现应该来说是比较简单的,以前学习C语言的时候就已经是实现过这个数据结构,这次主要是要与STL中的东西相互结合,比如,内存管理使用自己写的空间配置器,然后使用迭代器来封装其中的指针。在写LIST容器时候,我将代码的整体结构做了很大改动,与前面的有点不同,以前不同的就先不改了,等以后将所有容器实现后,会将所有代码放上面来。
/* ** iterator_list.h list的迭代器,继承基类,实现专属的迭代器,注意所有迭代器都是封装了指针,但是所有迭代器都不会用该指针new出一个新对象出来, 只会将已存在的内存空间赋值给它- */ #ifndef ITERATOR_LIST_H_INCLUDED #define ITERATOR_LIST_H_INCLUDED #include"simple_allocator.h" #include"my_iterator_base.h" namespace juine { template<class T> struct ListNode { T data; ListNode *next; ListNode *prex; ListNode(T _data,ListNode *_next=0,ListNode *_prex=0):data(_data),next(_next),prex(_prex){} }; template<class T> class Iterator_list { public: typedef ListNode<T>* pointer; typedef bidirectional_iterator_tag iterator_category; //构造函数和copy构造函数 Iterator_list(pointer item=0):point(item){} Iterator_list(const Iterator_list& iter):point(iter.point){} //析构函数 ~Iterator_list(){} Iterator_list operator++() { point=point->next; return *this; } Iterator_list operator++(int ) { Iterator_list iter=*this; this->point=point->next; return iter; } Iterator_list operator+(size_t n) { Iterator_list iter=*this; while(n) { iter++; n--; } return iter; } Iterator_list operator--() { this->point=point->prex; return *this; } Iterator_list operator--(int ) { Iterator_list iter=*this; this->point=point->prex; return iter; } Iterator_list operator-(size_t n) { Iterator_list iter=*this; while(n) { iter--; n--; } return iter; } //实现*和->的引用,!= T& operator*(){ return point->data; } pointer operator->(){ return point; } bool operator==(Iterator_list iter){ return point==iter.point; } bool operator!=(Iterator_list iter){ return point!=iter.point; } pointer& get_pointer() { return this->point; } protected: pointer point; }; } #endif // ITERATOR_LIST_H_INCLUDED
//my_list.h #ifndef MY_LIST_H_INCLUDED #define MY_LIST_H_INCLUDED #include"iterator_list.h" #include"simple_allocator.h" namespace juine { template<class T,class Alloc=my_alloc> class my_list { public: typedef T value_type; typedef T* pointer; typedef size_t size_type; typedef ListNode<T> item_type; typedef Iterator_list<T> iterator; typedef simple_allocator<ListNode<T>,Alloc> data_container; private: iterator start; iterator finish; //finish并不为空,而是将其中元素值设为0,next指针设为0 size_type _size; //创建节点,并用来初始化节点 item_type* uninitialized(value_type value,item_type* _prex,item_type* _next) { item_type* p=data_container::alloc(1); p->data=value; p->prex=_prex; p->next=_next; if(_prex!=0) _prex->next=p; if(_next!=0) _next->prex=p; return p; } void fill_uninitialized(size_type n,value_type value) { item_type* p=uninitialized(value,0,0); start.get_pointer()=p; while(n>1) { uninitialized(value,p,0); p=p->next; n--; } uninitialized(0,p,0); finish.get_pointer()=p->next; } template<class InputIterator> void fill_uninitialized(InputIterator first,InputIterator last) { item_type* p=uninitialized(*first,0,0); start.get_pointer()=p; first++; _size=1; while(first!=last) { uninitialized(*first,p,0); p=p->next; first++; _size++; } uninitialized(0,p,0); finish.get_pointer()=p->next; } void deallocate() { iterator temp; cout<<"List容器释放内存"<<endl; while(start!=finish) { temp=start; start++; destroy(temp.get_pointer()); data_container::dealloc(temp.get_pointer()); } } public: //构造函数,析构函数 my_list():start(0),finish(0),_size(0){} my_list(size_type n,value_type value):_size(n){ fill_uninitialized(n,value);} template<class InputIterator> my_list(InputIterator first,InputIterator last){ fill_uninitialized(first,last); } ~my_list(){ deallocate() ;} iterator begin() { return start; } iterator end() { return finish; } size_type size() { return _size; } bool empty() { return _size==0; } value_type& front() { return *start ;} value_type& back() { return *(finish-1); } void push_back(value_type value){ insert(finish,value); } void push_front(value_type value){ insert(start,value); } void insert(iterator position,value_type value) { item_type* temp=position->prex; uninitialized(value,temp,position.get_pointer()); if(position==start) //插头插尾不一样 start--; _size++; } template<class InputIterator> void insert(iterator position,InputIterator first,InputIterator last) { item_type* temp=position->prex; InputIterator _temp=last-1; if(first==_temp) //明显只插入一个,交给insert(position,value)处理 { insert(position,*first); return ; } int flag=1; while(true) { temp=uninitialized(*first,temp,0); if((position==start)&&flag) { flag=0; start.get_pointer()=temp; } first++; _size++; if(first==_temp) { //最后插入的一个元素单独处理 uninitialized(*first,temp,position.get_pointer()); _size++; break; } } } //删头删尾做的考虑都交给erase void pop_back(){ erase(start); } void pop_front(){ erase(finish-1); } void erase(iterator position) { if(position==start) //删头删尾不一样 { position->next->prex=0; start.get_pointer()=position->next; } else { position->prex->next=position->next; position->next->prex=position->prex; } destroy(position.get_pointer()); data_container::dealloc(position.get_pointer()); } void erase(iterator first,iterator last) { iterator temp=last-1; if(first==temp) { erase(first); return ; } if(first==start) { last->prex=0; start=last; } else { last->prex=first->prex; first->prex->next=last.get_pointer(); } } void clear() { erase(start,finish); } }; } #endif // MY_LIST_H_INCLUDED
#include<iostream> #define _USE_ALLOC #include"my_list.h" using namespace juine; using namespace std; template<class T,class U> void display(T a,U b) { cout<<"hello"<<endl; } template<> void display(size_t a,int b) { cout<<"world"<<endl; } //void test_list_funtion() int main() { int a[6]={1,2,3,4,5,6}; my_list<int> ll(a,a+6); ll.erase(ll.begin(),ll.end()); my_list<int>::iterator iter=ll.begin(); while(iter!=ll.end()) { cout<<*iter++<<endl; } }