C++ List 双向链表 实现 会用也要会写

转载自:http://blog.csdn.net/u011408355/article/details/47731021

这次的代码比较长,原因是比之前的Vector实现增加了许多的操作,而且毕竟指针操作嘛,处理起来稍微麻烦一点。

List实现中非常重要的一点,就是定义一个头指针和一个尾指针,这样可以避免对很多的特殊情况的处理,当链表为空时就是头指针指向尾指针,另外一个就是迭代器的实现,

list的迭代器的实现比vector要麻烦许多,因为内存不连续,所有采用了内部嵌套类的方式,重载了*(解引用),++EXP(前置++),++EXP(后置++),==和!=等操作符。

具体的大家可以看代码。

[cpp]  view plain copy
  1. <span style="font-size:14px;">//链表  
  2. template<typename T>  
  3. class List{  
  4. private:  
  5.      //定义节点,并设置一个节点的初始化函数  
  6.     struct Node{  
  7.         T t;  
  8.         Node* pre;  
  9.         Node* next;  
  10.         Node(T tVal=T(),Node* preVal=NULL,Node* nextVal=NULL):t(tVal),pre(preVal),next(nextVal){}  
  11.     };  
  12.   
  13. public:  
  14.      /*const迭代器和非const迭代器的实现,因为const迭代器只是作为对数据的保护,很多操作都是相同的因此这里采用了继承*/  
  15.     class const_iterator{  
  16.     public:  
  17.         const_iterator():current(NULL){}  
  18.         const T& operator*() const{  
  19.             return retrieve();  
  20.         }  
  21.         //重载相关的操作符  
  22.         const_iterator& operator++(){  
  23.             current=current->next;  
  24.             return *this;  
  25.         }  
  26.         const_iterator operator++(int){  
  27.             const_iterator old=*this;  
  28.             ++(*this);  
  29.             return old;  
  30.         }  
  31.         bool operator==(const_iterator& rhs) const{  
  32.             return current==rhs.current;  
  33.         }  
  34.         bool operator!=(const_iterator& rhs)const{  
  35.             return current!=rhs.current;  
  36.         }  
  37.     protected:  
  38.         Node* current;  
  39.         T& retrieve() const{  
  40.             return current->t;  
  41.         }  
  42.         const_iterator(Node* p):current(p){}  
  43.         friend class List<T>;  
  44.     };  
  45.   
  46.     class iterator:public const_iterator{  
  47.     public:  
  48.         iterator():current(NULL){}  
  49.         T& operator*(){  
  50.             return retrieve();  
  51.         }  
  52.         iterator& operator++(){  
  53.             current=current->next;  
  54.             return *this;  
  55.         }  
  56.         iterator operator++(int){  
  57.             iterator old=*this;  
  58.             current=current->next;  
  59.             return old;  
  60.         }  
  61.         const T& operator*()const{  
  62.             return const_iterator::operator*();  
  63.         }  
  64.   
  65.     protected:  
  66.         iterator(Node* p):const_iterator(p){}  
  67.         friend class List<T>;  
  68.     };  
  69.   
  70. public:  
  71.     /* 构造函数和析构函数,init函数属于private成员,因此放在下面*/  
  72.     List(){  
  73.         init();  
  74.     }  
  75.     List(const List& rhs){  
  76.         init();  
  77.         operator=(rhs);  
  78.     }  
  79.     ~List(){  
  80.         clear();  
  81.         delete head;  
  82.         delete tail;  
  83.     }  
  84.     //重载操作符  
  85.     const List& operator=(const List& rhs){  
  86.         for(const_iterator cite=rhs.begin();cite!=rhs.end();cite++){  
  87.             push_back(*cite);  
  88.         }  
  89.         return *this;  
  90.     }  
  91.       
  92.     T& operator[](int index){  
  93.         iterator ite=begin();  
  94.         for(int i=0;i<index;i++)  
  95.             ite++;  
  96.         return *ite;  
  97.     }  
  98.     //相关的操作函数  
  99.     T& front(){  
  100.         return *begin();  
  101.     }  
  102.     const T& front()const{  
  103.         return *begin();  
  104.     }  
  105.     T& back(){  
  106.         return *(--end());  
  107.     }  
  108.     const T& back()const{  
  109.         return *(--end());  
  110.     }  
  111.   
  112.     void push_front(T t){  
  113.         Node* p=new Node(t);  
  114.         p->next=head->next;  
  115.         p->pre=head;  
  116.         p->next->pre=p;  
  117.         head->next=p;  
  118.         theSize++;  
  119.     }  
  120.     void push_back(T t){  
  121.         Node* p=new Node(t);  
  122.         p->pre=tail->pre;  
  123.         p->next=tail;  
  124.         p->pre->next=p;  
  125.         tail->pre=p;  
  126.         theSize++;  
  127.     }  
  128.     void pop_front(){  
  129.         Node* p=head->next;  
  130.         head->next=p->next;  
  131.         p->next->pre=head;  
  132.         delete p;  
  133.         theSize--;  
  134.     }  
  135.     void pop_back(){  
  136.         Node* p=tail->pre;  
  137.         tail->pre=p->pre;  
  138.         p->pre->next=tail;  
  139.         delete p;  
  140.         theSize--;  
  141.     }  
  142.   
  143.     void insert(iterator itr,T t){  
  144.         Node* p=new Node(t);  
  145.         p->next=itr.current;  
  146.         p->pre=itr.current->pre;  
  147.         itr.current->pre->next=p;  
  148.         itr.current->pre=p;  
  149.         theSize++;  
  150.     }  
  151.     iterator erase(iterator itr){  
  152.         Node* p=itr.current->next;  
  153.         p->pre=itr.current->pre;  
  154.         p->pre->next=p;  
  155.         delete itr.current;  
  156.         return iterator(p);  
  157.         theSize--;  
  158.     }  
  159.     void erase(iterator start,iterator end){  
  160.         for(iterator ite=start;ite!=end){  
  161.             ite=erase(ite);  
  162.             theSize--;  
  163.         }  
  164.     }  
  165.   
  166.     iterator begin(){  
  167.         return iterator(head->next);  
  168.     }  
  169.   
  170.     const_iterator begin() const{  
  171.         return const_iterator(head->next);  
  172.     }  
  173.   
  174.     iterator end(){  
  175.         return iterator(tail);  
  176.     }  
  177.   
  178.     const_iterator end() const{  
  179.         return const_iterator(tail);  
  180.     }  
  181.   
  182.     int size()const{  
  183.         return theSize;  
  184.     }  
  185.     bool empty()const{  
  186.         return theSize==0;  
  187.     }  
  188.     void clear(){  
  189.         while(theSize!=0){  
  190.             pop_back();  
  191.         }  
  192.     }  
  193. private:  
  194.     int theSize;  
  195.     Node* head;  
  196.     Node* tail;  
  197.     void init(){  
  198.         theSize=0;  
  199.         head=new Node();  
  200.         tail=new Node();  
  201.         head->next=tail;  
  202.         tail->pre=head;  
  203.     }  
  204. };</span>  

有疑问或是发现错误,欢迎留言。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值