• list容器的C++代码实现


    1. #include <iostream>  
    2. using namespace std;  
    3.   
    4. template  <class T>   
    5. class mylist;//前置声明  
    6.   
    7. template <class T>  
    8. class node  
    9.  {  
    10.     friend class mylist<T>;//友元  
    11.     template <class T1>  
    12.     friend ostream& operator <<(ostream & out , node<T1> _node);  
    13.     public:  
    14.         node(T _data)  
    15.         {  
    16.             data = _data;  
    17.             next = NULL;  
    18.             prev = NULL;  
    19.         }  
    20.   
    21.     private:  
    22.         T data;  
    23.         node<T> *next;//指向下一个节点  
    24.         node<T> *prev;//指向前一个节点  
    25.  };  
    26.   
    27. //输出符号 << 的重载  
    28.  template <class T>  
    29. ostream& operator <<(ostream & out , node<T> _node)  
    30. {  
    31.     out<<_node.data<<' ';  
    32.     return out;  
    33. }  
    34. /*类名:mylist*/  
    35. template<class T>  
    36. class mylist  
    37. {  
    38. public:  
    39.     typedef bool (*Unperd)(T value);  
    40.     typedef bool (*BinPred)(T value);  
    41.     typedef bool (*Comp)(T a,T b);//排序的函数指针  
    42.     /*类名:iterator 
    43.     作用:迭代器 
    44.     */  
    45.       class iterator  
    46.       {  
    47.           public:  
    48.             iterator()  
    49.             {  
    50.                 ptr = NULL;  
    51.             }  
    52.             iterator(node<T>* _ptr)  
    53.             {  
    54.                 ptr = _ptr;  
    55.             }  
    56.              /*符号 = 重载*/  
    57.             void operator =(iterator it)  
    58.             {  
    59.                this->ptr = it.ptr;  
    60.             }  
    61.              /*符号 != 重载*/  
    62.             bool operator !=(iterator it)  
    63.             {  
    64.                return this->ptr != it.ptr;  
    65.             }  
    66.              /*符号 == 重载*/  
    67.             bool operator == (iterator it)  
    68.             {  
    69.                 return this->ptr == it.ptr;  
    70.             }  
    71.              /*符号 ++ 重载*/  
    72.             iterator operator ++(int i)//这里的 int i 是无意义的只是区别前++  
    73.             {  
    74.                 iterator tmp;//中间迭代器变量tmp  
    75.                 tmp = *this;  
    76.                 this->ptr = this->ptr->next;  
    77.                 return tmp;  
    78.             }  
    79.              /*符号 * 重载*/  
    80.             node<T> operator *()  
    81.             {  
    82.                 return *(this->ptr);  
    83.             }  
    84.   
    85.           private:  
    86.             node<T> *ptr;  
    87.       };  
    88.        /*反向迭代器*/  
    89.       //反向迭代器就是++的重载不一样,其他是一样的  
    90.     class reserve_iterator  
    91.     {  
    92.            public:  
    93.             reserve_iterator()  
    94.             {  
    95.                 ptr = NULL;  
    96.             }  
    97.             reserve_iterator(node<T>* _ptr)  
    98.             {  
    99.                 ptr = _ptr;  
    100.             }  
    101.   
    102.             void operator =(reserve_iterator it)  
    103.             {  
    104.                this->ptr = it.ptr;  
    105.             }  
    106.             bool operator !=(reserve_iterator it)  
    107.             {  
    108.                return this->ptr != it.ptr;  
    109.             }  
    110.             bool operator == (reserve_iterator it)  
    111.             {  
    112.                 return this->ptr == it.ptr;  
    113.             }  
    114.             reserve_iterator operator ++(int i)  
    115.             {  
    116.                 reserve_iterator tmp;  
    117.                 tmp = *this;  
    118.                 this->ptr = this->ptr->prev;  
    119.                 return tmp;  
    120.             }  
    121.             node<T> operator *()  
    122.             {  
    123.                 return *(this->ptr);  
    124.             }  
    125.           private:  
    126.             node<T> *ptr;  
    127.     };  
    128.     /*无参缺省构造*/  
    129.     mylist()  
    130.     {  
    131.         head = NULL;  
    132.         curr = NULL;  
    133.     }  
    134.     /*list(int num,T value) 
    135.   函数名:mylist 
    136.   作用:创造一个list容器里面初始就有num个value值 
    137.   参数:int num,T value 
    138.   返回值:无 
    139.   */  
    140.     mylist(int num,T value)  
    141.     {  
    142.         head = new node<T>(value);  
    143.         curr = head;  
    144.         int i;  
    145.         node<T>* newnode = NULL;  
    146.         for (i = 0; i < num;i++)  
    147.         {  
    148.             newnode = new node<T>(value);  
    149.             curr->next = newnode;  
    150.             newnode->prev = curr;  
    151.              curr = newnode;   
    152.             newnode = NULL;       
    153.         }  
    154.         curr->next = head;//构成循环  
    155.         head->prev = curr;  
    156.     }  
    157.   /*list<T>(list) 
    158.   函数名:mylist 
    159.   作用:创造一个list容器里面拷贝另一个list容器的所有内容 
    160.   参数:mylist & list 
    161.   返回值:无 
    162.   */  
    163.     mylist(mylist & list)  
    164.     {  
    165.         node<T>* point = list.head->next;  
    166.         this->head = new node<T>(point->data);  
    167.         this->curr = this->head;  
    168.         node<T> * newnode = NULL;  
    169.         while (point != list.head)  
    170.         {  
    171.            newnode = new node<T>(point->data);  
    172.            this->curr->next = newnode;  
    173.            newnode->prev = this->curr;  
    174.            this->curr = newnode;  
    175.            newnode = NULL;  
    176.            point = point->next;  
    177.         }  
    178.         this->curr->next = this->head;  
    179.         this->head->prev = this->curr;   
    180.     }  
    181.   
    182.   
    183.   /*list<T>(list.begin(),list.end()) 
    184.   函数名:mylist 
    185.   作用:创造一个list容器里面拷贝另一个list容器的所有内容 
    186.   参数:mylist & list 
    187.   返回值:无 
    188.   */  
    189.     mylist(iterator start,iterator end)  
    190.     {  
    191.          iterator point = start;  
    192.         head = new node<T>(*point);  
    193.         curr = head;  
    194.         node<T> * newnode = NULL;  
    195.         while (point != end)  
    196.         {  
    197.            newnode = new node<T>(*point);  
    198.            curr->next = newnode;  
    199.            newnode->prev = curr;  
    200.            curr = newnode;  
    201.            newnode = NULL;  
    202.            point++;  
    203.         }  
    204.         curr->next = head;  
    205.         head->prev = curr;   
    206.     }  
    207.   
    208.     /*void assign( input_iterator start, input_iterator end ); 
    209.     assign()函数以迭代器start和end指示的范围为list赋值*/  
    210.       void assign(iterator start,iterator end)  
    211.       {  
    212.          iterator point = start;  
    213.          if (NULL == head)  
    214.          {  
    215.              this->head = new node<T>(*point);  
    216.              curr = head;  
    217.          }  
    218.          else  
    219.          {  
    220.              curr = head->prev;  
    221.          }  
    222.              
    223.             node<T> * newnode = NULL;  
    224.             while (point != end)  
    225.            {  
    226.              newnode = new node<T>(*point);  
    227.              curr->next = newnode;  
    228.              newnode->prev = curr;  
    229.              curr = newnode;  
    230.              newnode = NULL;  
    231.              point++;  
    232.            }  
    233.            curr->next = head;  
    234.            head->prev = curr;  
    235.       }  
    236.      /*void assign( size_type num, const TYPE &val );*/  
    237.       void assign(int num,const T & value)  
    238.       {  
    239.          if (NULL == head)  
    240.          {  
    241.             head = new node<T>(value);  
    242.             curr = head;  
    243.          }  
    244.          else  
    245.          {  
    246.             curr = head->prev;  
    247.          }  
    248.         int i;  
    249.         node<T>* newnode = NULL;  
    250.         for (i = 0; i < num;i++)  
    251.         {  
    252.             newnode = new node<T>(value);  
    253.             curr->next = newnode;  
    254.             newnode->prev = curr;  
    255.             curr = newnode;   
    256.             newnode = NULL;       
    257.         }  
    258.         curr->next = head;  
    259.         head->prev = curr;  
    260.       }  
    261.    /*back()函数返回一个引用,指向list的最后一个元素*/  
    262.      T& back()  
    263.     {  
    264.       return head->prev->data;  
    265.     }  
    266.     /*clear()函数删除list的所有元素*/  
    267.     void clear()  
    268.     {  
    269.         curr = head->next;  
    270.         head->next = NULL;  
    271.         node<T>* tmp;  
    272.         while (curr != head)  
    273.         {  
    274.             tmp = curr;  
    275.             curr = curr->next;  
    276.             head->next = curr;  
    277.             if (NULL != curr)  
    278.             {  
    279.                 curr->prev = head;  
    280.             }  
    281.             free(tmp);   
    282.             tmp = NULL;  
    283.         }  
    284.     }  
    285.     /*empty()函数返回真(true)如果链表为空,否则返回假*/  
    286.     bool empty()  
    287.     {  
    288.         if (head->next == head)  
    289.             return true;  
    290.         return false;  
    291.     }  
    292.    /*起始位置*/  
    293.     iterator begin()  
    294.     {  
    295.        return  iterator(head->next);  
    296.     }  
    297.     /*末尾的下一个*/  
    298.     iterator end()  
    299.     {  
    300.        return  iterator(head);  
    301.     }  
    302.   
    303.     /*rbegin()函数返回一个逆向迭代器,指向链表的末尾。*/  
    304.     reserve_iterator rebegin()  
    305.     {  
    306.         return reserve_iterator(head->prev);  
    307.     }  
    308.   
    309.     /*rend()函数迭代器指向链表的头部。*/  
    310.     reserve_iterator rend()  
    311.     {  
    312.         return reserve_iterator(head);  
    313.     }  
    314.   
    315.     /*find查找指定元素的第一个位置没找到则返回NULL*/  
    316.     iterator find(const T &value)  
    317.     {  
    318.         curr = head->next;  
    319.         int flag = 0;  
    320.         while (curr != head)  
    321.         {  
    322.             if (curr->data == value)  
    323.             {     
    324.                 flag = 1;  
    325.                 return iterator(curr);  
    326.                 break;  
    327.             }  
    328.             curr = curr->next;  
    329.         }  
    330.         if (flag == 0)  
    331.         {  
    332.             return iterator();  
    333.         }  
    334.     }  
    335.   
    336.   
    337.     /*erase()函数删除以pos指示位置的元素 
    338.       iterator erase( iterator pos );*/  
    339.     iterator erase(iterator pos)  
    340.     {  
    341.         node<T>* tmp = head;  
    342.         curr = head->next;  
    343.         while (curr != head)  
    344.         {  
    345.              if (iterator(curr) == pos)  
    346.              {  
    347.                 tmp->next = curr->next;  
    348.                 curr->next->prev = tmp;  
    349.                 return iterator(curr->next);  
    350.                 free(curr);  
    351.                 curr = NULL;  
    352.                 break;  
    353.              }  
    354.              else  
    355.              {  
    356.                 tmp = curr;  
    357.              }  
    358.              curr = curr->next;  
    359.         }  
    360.     }  
    361.      /* iterator erase( iterator start, iterator end ); 
    362.      删除start和end之间的元素。 返回值是一个迭代器, 
    363.      指向最后一个被删除元素的下一个元素*/  
    364.      iterator erase(iterator start,iterator end)  
    365.      {  
    366.         node<T>* tmp = head;  
    367.         curr = head->next;  
    368.         while (curr != head)  
    369.         {  
    370.             if (iterator(tmp) == start)  
    371.             {  
    372.                 while (iterator(curr) != end)  
    373.                 {  
    374.                     tmp->next = curr->next;  
    375.                     curr->next->prev = tmp;  
    376.                     free(curr);  
    377.                     curr = NULL;  
    378.                     curr = tmp->next;  
    379.                 }  
    380.                 return iterator(curr);  
    381.                 break;  
    382.             }  
    383.             else  
    384.             {  
    385.               tmp = curr;  
    386.             }  
    387.             curr = curr->next;  
    388.         }  
    389.      }  
    390.   
    391.      /*front()函数返回一个引用,指向链表的第一个元素。*/  
    392.      T & front()  
    393.      {  
    394.         return head->next->data;  
    395.      }  
    396.   
    397.      /*  iterator insert( iterator pos, const TYPE &val );*/  
    398.       iterator insert(iterator pos,const T& value)  
    399.       {  
    400.          node<T>* newnode = new node<T>(value);  
    401.          curr = head->next;  
    402.          while (curr != head)  
    403.          {  
    404.   
    405.             if (iterator(head) == pos)  
    406.             {  
    407.                  head->prev->prev->next = newnode;  
    408.                  newnode->prev = head->prev->prev;  
    409.                  head->prev->prev = newnode;  
    410.                  newnode->next = head->prev;  
    411.                  return iterator(head->prev);  
    412.                  break;  
    413.             }  
    414.             if (iterator(curr) == pos)  
    415.             {  
    416.                 curr->prev->next = newnode;  
    417.                 newnode->prev = curr->prev;  
    418.                 curr->prev = newnode;  
    419.                 newnode->next = curr;  
    420.                 break;  
    421.             }  
    422.             curr = curr->next;  
    423.          }  
    424.       }  
    425.       /*  void insert( iterator pos, size_type num, const TYPE &val );*/  
    426.       void insert(iterator pos,int num,const T &value)  
    427.       {  
    428.          node<T>* newnode;  
    429.          curr = head->next;  
    430.          node<T>* tmp = head;  
    431.          if (iterator(head) == pos)  
    432.          {     
    433.              tmp = head->prev->prev;  
    434.              curr = head->prev;  
    435.              while (0 != num)  
    436.              {  
    437.                 newnode = new node<T>(value);  
    438.                 tmp->next = newnode;  
    439.                 newnode->prev = tmp;  
    440.                 tmp = newnode;  
    441.                 newnode = NULL;  
    442.                 num--;   
    443.              }  
    444.              tmp->next = curr;  
    445.              curr->prev = tmp;  
    446.          }  
    447.          else  
    448.          {  
    449.            while (curr != head)  
    450.            {              
    451.               if (iterator(curr) == pos)  
    452.              {  
    453.                 while (0 != num)  
    454.                 {  
    455.                     newnode = new node<T>(value);  
    456.                     tmp->next = newnode;  
    457.                     newnode->prev = tmp;  
    458.                     tmp = newnode;  
    459.                     newnode = NULL;  
    460.                     num--;  
    461.                 }  
    462.                 tmp->next = curr;  
    463.                 curr->prev = tmp;  
    464.                 break;  
    465.              }  
    466.              else  
    467.              {  
    468.                 tmp =curr;  
    469.              }  
    470.             curr = curr->next;  
    471.           }  
    472.          }  
    473.       }  
    474.       /*pop_back()函数删除链表的最后一个元素。*/  
    475.       void pop_back()  
    476.       {  
    477.          curr = head->prev;  
    478.          node<T>* tmp = head->prev->prev;  
    479.          tmp->next = head;  
    480.          head->prev = tmp;  
    481.          free(curr);  
    482.          curr = NULL;  
    483.       }  
    484.       /*pop_front()函数删除链表的第一个元素。*/  
    485.       void pop_front()  
    486.       {  
    487.          curr = head->next;  
    488.          node<T>* tmp = head->next->next;  
    489.          head->next = tmp;  
    490.          tmp->prev = head;  
    491.          free(curr);  
    492.          curr = NULL;  
    493.       }  
    494.      /* push_back()将val连接到链表的最后*/  
    495.       void push_back(const T& value)  
    496.       {  
    497.         node<T>* newnode = new node<T>(value) ;  
    498.         if (head == NULL)  
    499.         {  
    500.            head = new node<T>(value);  
    501.            curr = head;  
    502.         }  
    503.         else  
    504.         {  
    505.             curr = head->prev;  
    506.         }  
    507.          curr->next = newnode;  
    508.          newnode->prev = curr;  
    509.          curr = newnode;  
    510.          newnode = NULL;  
    511.          curr->next = head;  
    512.          head->prev = curr;  
    513.       }  
    514.       /*push_front函数将val连接到链表的头部。*/  
    515.       void push_front(const T& value)  
    516.       {  
    517.         node<T>* newnode = new node<T>(value) ;  
    518.         if (head == NULL)  
    519.         {  
    520.            head = new node<T>(value);  
    521.            curr = head;  
    522.            curr->next = newnode;  
    523.            newnode->next = curr;  
    524.            curr = newnode;  
    525.            newnode = NULL;  
    526.            curr->next = head;  
    527.            head->prev = curr;  
    528.         }  
    529.         else  
    530.         {  
    531.            curr = head;  
    532.            newnode->next = curr->next;  
    533.            curr->next = newnode;  
    534.            curr = newnode;  
    535.            newnode = NULL;  
    536.            curr->prev = head;  
    537.            curr->next->prev = curr;  
    538.         }  
    539.       }  
    540.       /*remove()函数删除链表中所有值为val的元素*/  
    541.       void remove(const T & value)  
    542.       {   
    543.           node<T>* tmp = head;  
    544.           curr = head->next;  
    545.           while (curr != head)  
    546.           {  
    547.             if (curr->data == value)  
    548.             {  
    549.                 tmp->next = curr->next;  
    550.                 curr->next->prev = tmp;  
    551.                 free(curr);  
    552.                 curr = NULL;  
    553.                 curr = tmp;  
    554.             }  
    555.             else  
    556.             {  
    557.                 tmp = curr;  
    558.             }  
    559.             curr = curr->next;  
    560.           }  
    561.       }  
    562.       /*remove_if()以一元谓词pr为判断元素的依据,遍历整个链表 
    563.       如果pr返回true则删除该元素。*/  
    564.       void remove_if(Unperd pr)  
    565.       {  
    566.            node<T>* tmp = head;  
    567.           curr = head->next;  
    568.           while (curr != head)  
    569.           {  
    570.             if (pr(curr->data))  
    571.             {  
    572.                 tmp->next = curr->next;  
    573.                 curr->next->prev = tmp;  
    574.                 free(curr);  
    575.                 curr = NULL;  
    576.                 curr = tmp;  
    577.             }  
    578.             else  
    579.             {  
    580.                 tmp = curr;  
    581.             }  
    582.             curr = curr->next;  
    583.           }  
    584.       }  
    585.   
    586.       /*resize()函数把list的大小改变到num。被加入的多余的元素都被赋值为val*/  
    587.       void resize(int num,T value)  
    588.       {           
    589.         int record = 0;  
    590.         curr = head->next;  
    591.         iterator point;  
    592.         while (curr != head)  
    593.         {  
    594.              ++record;  
    595.             if (record == num)  
    596.             {  
    597.                 point = iterator(curr);  
    598.             }  
    599.            curr = curr->next;  
    600.         }  
    601.         if (record < num)  
    602.         {     
    603.             while (record != num)  
    604.             {  
    605.                 node<T>* newnode = new node<T>(value);  
    606.                 curr = head->prev;  
    607.                 curr->next = newnode;  
    608.                 newnode->prev = curr;  
    609.                 curr = newnode;  
    610.                 newnode = NULL;  
    611.                 curr->next = head;  
    612.                 head->prev = curr;  
    613.                 record++;  
    614.             }  
    615.         }  
    616.         if (record > num)  
    617.         {  
    618.            erase(point,iterator(head));  
    619.         }  
    620.       }  
    621.   
    622.       /*reverse()函数把list所有元素倒转。*/  
    623.       void reverse()  
    624.       {   
    625.          mylist<T> list(*this);  
    626.         node<T>* tmp = list.head->prev;  
    627.          this->curr = this->head->next;  
    628.          while (this->curr != this->head)  
    629.          {  
    630.             this->curr->data = tmp->data;  
    631.             tmp = tmp->prev;  
    632.             this->curr = this->curr->next;  
    633.          }  
    634.       }  
    635.   
    636.       /*size()函数返回list中元素的数量。*/  
    637.       int size()  
    638.       {  
    639.         curr = head->next;  
    640.         int num = 0;  
    641.         while (curr != head)  
    642.         {  
    643.             num++;  
    644.             curr = curr->next;  
    645.         }  
    646.         return num;  
    647.       }  
    648.   
    649.       /*sort()函数为链表排序,默认是升序*/  
    650.       void sort()  
    651.       {  
    652.         curr = head->next;  
    653.         node<T>* tmp;  
    654.         tmp =  head->next->next;  
    655.         int temp = 0;  
    656.         int n = size();  
    657.         int i = 0;  
    658.         int flag = 0;  
    659.         while (tmp != head)  
    660.         {                
    661.             if (curr->data > tmp->data)  
    662.             {  
    663.                 flag = 1;  
    664.                 temp = tmp->data;  
    665.                 tmp->data = curr->data;  
    666.                 curr->data = temp;  
    667.             }  
    668.             i++;  
    669.             curr = curr->next;  
    670.             tmp = tmp->next;  
    671.             if ((i == n-1)&&(flag == 1))  
    672.             {  
    673.                 flag = 0;  
    674.                 i = 0;  
    675.                 curr = head->next;  
    676.                 tmp = head->next->next;  
    677.             }   
    678.         }  
    679.       }  
    680.   
    681.       /* void sort( Comp compfunction ); 
    682.       如果指定compfunction的话,就采用指定函数来判定两个元素的大小*/  
    683.       void sort(Comp compfunction)  
    684.       {  
    685.         curr = head->next;  
    686.         node<T>* tmp;  
    687.         tmp =  head->next->next;  
    688.         int temp = 0;  
    689.         int n = size();  
    690.         int i = 0;  
    691.         int flag = 0;  
    692.         while (tmp != head)  
    693.         {                
    694.             if (compfunction(curr->data,tmp->data))  
    695.             {  
    696.                 flag = 1;  
    697.                 temp = tmp->data;  
    698.                 tmp->data = curr->data;  
    699.                 curr->data = temp;  
    700.             }  
    701.             i++;  
    702.             curr = curr->next;  
    703.             tmp = tmp->next;  
    704.             if ((i == n-1)&&(flag == 1))  
    705.             {  
    706.                 flag = 0;  
    707.                 i = 0;  
    708.                 curr = head->next;  
    709.                 tmp = head->next->next;  
    710.             }   
    711.         }  
    712.      }  
    713.       /*void splice( iterator pos, list &lst );splice()函数把lst连接到pos的位置*/  
    714.      void splice(iterator pos,mylist & list)  
    715.      {  
    716.         this->curr = this->head->next;  
    717.         node<T>* tmp = this->head;  
    718.         while (this->curr != this->head)  
    719.         {  
    720.             if (iterator(this->curr) == pos)  
    721.             {  
    722.                 tmp->next = list.head->next;  
    723.                 list.head->prev->next = this->curr->next;  
    724.                 this->curr->next->prev = list.head->prev;  
    725.                 list.head->prev = tmp;  
    726.                 free(this->curr);  
    727.                 this->curr = NULL;  
    728.                 free(list.head);  
    729.                 list.head = NULL;  
    730.                 break;  
    731.             }  
    732.             else  
    733.             {  
    734.                 tmp = this->curr;  
    735.             }  
    736.             this->curr = this->curr->next;  
    737.         }  
    738.      }  
    739.   
    740.      /* void splice( iterator pos, list &lst, iterator del );*/  
    741.      void splice(iterator pos,mylist & list,iterator del)  
    742.      {  
    743.         this->curr = this->head->next;  
    744.         list.curr = list.head->next;  
    745.         node<T>* tmp = list.head;  
    746.         while (this->curr != this->head)  
    747.         {  
    748.             if (iterator(this->curr) == pos)  
    749.             {  
    750.                  while (list.curr != list.head)  
    751.                  {  
    752.                     if (iterator(list.curr) == del)  
    753.                     {  
    754.                          this->curr->data = list.curr->data;  
    755.                          tmp->next = list.curr->next;  
    756.                          curr->next->prev = tmp;  
    757.                          free(list.curr);  
    758.                          list.curr;  
    759.                          break;  
    760.                     }  
    761.                     else  
    762.                     {  
    763.                         tmp = list.curr;  
    764.                     }  
    765.                   list.curr = list.curr->next;  
    766.                  }  
    767.                  break;  
    768.             }  
    769.             this->curr = this->curr->next;  
    770.         }  
    771.      }  
    772.   
    773.      /* void splice( iterator pos, list &lst, iterator start, iterator end );*/  
    774.      void splice(iterator pos,mylist & list,iterator start,iterator end)  
    775.      {    
    776.         list.curr = list.head;  
    777.         node<T>* point = list.head;  
    778.         node<T>* point1 = list.head;  
    779.         iterator it = this->erase(pos);  
    780.         while (iterator(list.curr) != start)  
    781.         {  
    782.             list.curr = list.curr->next;  
    783.         }  
    784.         point = list.curr->prev;  
    785.         while(iterator(list.curr->prev) != end)  
    786.         {  
    787.            this->insert(it,list.curr->data);  
    788.            list.curr = list.curr->next;  
    789.         }  
    790.         point1 = list.curr;  
    791.         list.erase(point,point1);  
    792.      }  
    793.      /*swap()函数交换lst和现链表中的元素*/  
    794.      void swap(mylist & list)  
    795.      {  
    796.         int flag1 = 0;//记录交换时那个链表过长需要删除  
    797.         int flag2 = 0;//这里其实只要一个标志  但是为了方便故设置两个  
    798.         mylist<T> tmp(list);  
    799.         list.curr = list.head->next;  
    800.         this->curr = this->head->next;  
    801.         while (this->curr != this->head)  
    802.         {     
    803.             if (list.curr != list.head)  
    804.             {  
    805.               list.curr->data =  this->curr->data;  
    806.               list.curr = list.curr->next;  
    807.             }  
    808.             else  
    809.             {  
    810.                 flag1 = 1;  
    811.                list.push_back(this->curr->data);  
    812.             }     
    813.             this->curr = this->curr->next;  
    814.         }  
    815.         if ((list.curr != list.head)&& (flag1 == 0))  
    816.         {  
    817.             list.erase(iterator(list.curr->prev),iterator(list.head));  
    818.         }  
    819.          tmp.curr = tmp.head->next;  
    820.         this->curr = this->head->next;  
    821.        while (tmp.curr != tmp.head)  
    822.         {     
    823.             if (this->curr != this->head)  
    824.             {  
    825.               this->curr->data =  tmp.curr->data;  
    826.               this->curr = this->curr->next;  
    827.             }  
    828.             else  
    829.             {  
    830.                 flag2 = 1;  
    831.                this->push_back(tmp.curr->data);  
    832.             }     
    833.             tmp.curr = tmp.curr->next;  
    834.         }  
    835.         if ((this->curr != this->head)&& (flag2 == 0))  
    836.         {  
    837.             this->erase(iterator(this->curr->prev),iterator(this->head));  
    838.         }  
    839.      }  
    840.   
    841.      /* void unique();unique()函数删除链表中所有重复的元素。*/  
    842.         void unique()  
    843.         {  
    844.            curr = head->next->next;  
    845.            node<T>* tmp = head->next;  
    846.            while (curr != head)  
    847.            {  
    848.               if (tmp->data == curr->data)  
    849.               {  
    850.                    tmp->next = curr->next;  
    851.                    curr->next->prev = tmp;  
    852.                    free(curr);  
    853.                    curr = NULL;  
    854.                    curr = tmp->next;  
    855.               }  
    856.               tmp = tmp->next;  
    857.               curr = curr->next;  
    858.            }  
    859.         }  
    860.         /* void unique( BinPred pr ); 
    861.      指定pr,则使用pr来判定是否删除*/  
    862.         void unique(BinPred pr)  
    863.         {  
    864.            curr = head->next->next;  
    865.            node<T>* tmp = head->next;  
    866.            while (curr != head)  
    867.            {  
    868.               if ((tmp->data == curr->data)&&pr(curr->data))  
    869.               {  
    870.                    tmp->next = curr->next;  
    871.                    curr->next->prev = tmp;  
    872.                    free(curr);  
    873.                    curr = NULL;  
    874.                    curr = tmp->next;  
    875.               }  
    876.               tmp = tmp->next;  
    877.               curr = curr->next;  
    878.            }  
    879.         }  
    880. private:  
    881.     node<T> *head;  
    882.     node<T> *curr;  
    883. };  
    884.   
    885. template <class T>  
    886. bool dis_tinet( T value)  
    887. {  
    888.     if (9 == value)  
    889.         return true;  
    890.     return false;  
    891. }  
    892. template <class T>  
    893. bool sortfunction(T a,T b)  
    894. {  
    895.     if (a < b)  
    896.         return true;  
    897.     return false;  
    898. }  
    899. template <class T>  
    900. bool unique_t( T value)  
    901. {  
    902.     if (8 == value)  
    903.         return true;  
    904.     return false;  
    905. }  
  • 相关阅读:
    【LOJ#10027】魔板
    【LOJ#2653】山峰和山谷
    【POJ2449】第k短路
    【HAOI2008】移动玩具
    【洛谷P1379】八数码难题
    【NOIP2002】字串变换
    【CH2501】矩阵距离
    【CH2601】电路维修
    【NOIP2009】靶形数独
    树的子结构
  • 原文地址:https://www.cnblogs.com/jdxn/p/6759303.html
Copyright © 2020-2023  润新知