144.链表库以及迭代算法原理

  • Node.h
     1 #pragma once
     2 
     3 template<class T>
     4 class Node
     5 {
     6 public:
     7     T t;
     8     Node *pNext;
     9 
    10 };
  • forwart_list.h
     1 #pragma once
     2 #include "Node.h"
     3 #include <iostream>
     4 #include "it.h"
     5 using namespace std;
     6 
     7 template<class T>
     8 class forwart_list
     9 {
    10 private:
    11     //头结点
    12     Node<T> *phead;
    13 public:
    14     forwart_list();
    15     ~forwart_list();
    16     void push_front(T &t);
    17     void push_front(T &&t);
    18     void push_back(T &t);
    19     void push_back(T &&t);
    20     void show();
    21     void merge(forwart_list<T>&mylist);
    22     void insert(T t, T newt);
    23     Node<T>*find(T t);
    24     void clear();
    25     void sort();
    26     void deleteit(T t);
    27     int getsize();
    28     void change(T t,T newt);
    29     //链表反转
    30     void rev();
    31     it<T> begin();
    32     it<T> end();
    33 };
  • forward_list.cpp
      1 #include "forwart_list.h"
      2 
      3 
      4 
      5 template<class T>
      6 forwart_list<T>::forwart_list():phead(nullptr)
      7 {
      8     
      9 }
     10 
     11 
     12 template<class T>
     13 forwart_list<T>::~forwart_list()
     14 {
     15     
     16 }
     17 
     18 template<class T>
     19 void forwart_list<T>::push_front(T & t)
     20 {
     21     //开辟节点
     22     Node<T> *pnew = new Node<T>;
     23     //赋值
     24     pnew->t = t;
     25     pnew->pNext = nullptr;
     26     if (phead == nullptr)
     27     {
     28         this->phead = pnew;
     29     }
     30     else
     31     {
     32         pnew->pNext = phead;
     33         this->phead = pnew;
     34     }
     35 }
     36 
     37 template<class T>
     38 void forwart_list<T>::push_front(T && t)
     39 {
     40     //开辟节点
     41     Node<T> *pnew = new Node<T>;
     42     //赋值
     43     pnew->t = t;
     44     pnew->pNext = nullptr;
     45     if (phead == nullptr)
     46     {
     47         this->phead = pnew;
     48     }
     49     else
     50     {
     51         pnew->pNext = phead;
     52         this->phead = pnew;
     53     }
     54 }
     55 
     56 template<class T>
     57 void forwart_list<T>::push_back(T & t)
     58 {
     59     //开辟节点
     60     Node<T> *pnew = new Node<T>;
     61     //赋值
     62     pnew->t = t;
     63     pnew->pNext = nullptr;
     64     if (phead == nullptr)
     65     {
     66         this->phead = pnew;
     67     }
     68     else
     69     {
     70         Node<T>*ptemp = phead;
     71         while (ptemp->pNext != nullptr)
     72         {
     73             ptemp = ptemp->pNext;
     74         }
     75         ptemp->pNext = pnew;
     76     }
     77 }
     78 
     79 template<class T>
     80 void forwart_list<T>::push_back(T && t)
     81 {
     82     //开辟节点
     83     Node<T> *pnew = new Node<T>;
     84     //赋值
     85     pnew->t = t;
     86     pnew->pNext = nullptr;
     87     if (phead == nullptr)
     88     {
     89         this->phead = pnew;
     90     }
     91     else
     92     {
     93         Node<T>*ptemp = phead;
     94         while (ptemp->pNext != nullptr)
     95         {
     96             ptemp = ptemp->pNext;
     97         }
     98         ptemp->pNext = pnew;
     99     }
    100 }
    101 
    102 template<class T>
    103 void forwart_list<T>::show()
    104 {
    105     Node<T>*ptemp = phead;
    106     while (ptemp != nullptr)
    107     {
    108         cout << ptemp->t << endl;
    109         ptemp = ptemp->pNext;
    110     }
    111 }
    112 
    113 template<class T>
    114 void forwart_list<T>::merge(forwart_list<T>& mylist)
    115 {
    116     Node<T> *ptemp = phead;
    117     while (ptemp->pNext != nullptr)
    118     {
    119         ptemp = ptemp->pNext;
    120     }
    121     //归并
    122     ptemp->pNext = mylist.phead;
    123 }
    124 
    125 template<class T>
    126 void forwart_list<T>::insert(T t, T newt)
    127 {
    128     Node<T> *p = find(t);
    129 
    130     if (p != nullptr)
    131     {
    132         //p1记录前一个遍历的位置,p2一直遍历
    133         Node<T>*p1, *p2;
    134         p1 = phead;
    135         p2 = phead;
    136         while (p2 != p)
    137         {
    138             p1 = p2;
    139             p2 = p2->pNext;
    140         }
    141 
    142         Node<T>* pnew = new Node<T>;
    143         pnew->t = newt;
    144         pnew->pNext = p2;
    145         p1->pNext = pnew;
    146     }
    147 }
    148 
    149 template<class T>
    150 Node<T>* forwart_list<T>::find(T t)
    151 {
    152     Node<T> *ptemp = phead;
    153     while (ptemp != nullptr)
    154     {
    155         if (ptemp->t == t)
    156         {
    157             return ptemp;
    158         }
    159         ptemp = ptemp->pNext;
    160     }
    161     return nullptr;
    162 }
    163 
    164 //清空
    165 template<class T>
    166 void forwart_list<T>::clear()
    167 {
    168     if (phead == nullptr)
    169     {
    170         return;
    171     }
    172     else
    173     {
    174         //p2保存后一个结点,一直删除p1
    175         Node<T>*p1, *p2;
    176         p1 = phead;
    177         while (p1 != nullptr)
    178         {
    179             p2 = p1->pNext;
    180             delete p1;
    181             p1 = nullptr;
    182             p1 = p2;
    183         }
    184     }
    185 }
    186 
    187 template<class T>
    188 void forwart_list<T>::sort()
    189 {
    190     for (Node<T>*p1 = head; p1 != nullptr; p1 = p1->pNext)
    191     {
    192         for (Node<T>*p2 = head; p2->pNext->pNext != nullptr; p2 = p2->pNext)
    193         {
    194             if (p2->t > p2->pNext->t)
    195             {
    196                 T temp;
    197                 temp = p1->t;
    198                 p1->t = p2->t;
    199                 p2->t = temp;
    200             }
    201         }
    202     }
    203 }
    204 
    205 template<class T>
    206 void forwart_list<T>::deleteit(T t)
    207 {
    208     Node<T> *p = find(t);
    209 
    210     if (p != nullptr)
    211     {
    212         //p1记录前一个遍历的位置,p2一直遍历
    213         Node<T>*p1, *p2;
    214         p1 = phead;
    215         p2 = phead;
    216         while (p2 != p)
    217         {
    218             p1 = p2;
    219             p2 = p2->pNext;
    220         }
    221 
    222         if (p2 == phead)
    223         {
    224             phead = p->pNext;
    225             delete p;
    226         }
    227         else
    228         {
    229             p1->pNext = p2->pNext;
    230             delete p2;
    231         }
    232         p2 = nullptr;
    233     }
    234 }
    235 
    236 template<class T>
    237 int forwart_list<T>::getsize()
    238 {
    239     int count = 0;
    240     Node<T>*p;
    241     while (p != nullptr)
    242     {
    243         p = p->pNext;
    244         count++;
    245     }
    246     return count;
    247 }
    248 
    249 template<class T>
    250 void forwart_list<T>::change(T t, T newt)
    251 {
    252     Node<T> *p = find(t);
    253     if (p != nullptr)
    254     {
    255         p -> t = newt;
    256     }
    257 }
    258 
    259 template<class T>
    260 void forwart_list<T>::rev()
    261 {
    262     if (phead == nullptr || phead->pNext == nullptr)
    263     {
    264         return;
    265     }
    266     else
    267     {
    268         //p1,p2,p3分别是相对第一个位置,相对第二个位置,相对第三个位置
    269         Node<T>*p1, *p2, *p3;
    270         p1 = phead;
    271         p2 = phead->pNext;
    272         while (p2 != nullptr)
    273         {
    274             p3 = p2->pNext;
    275             p2->pNext = p1;
    276 
    277             p1 = p2;
    278             p2 = p3;
    279         }
    280         phead->pNext = nullptr;
    281         phead = p1;
    282     }
    283 }
    284 
    285 template<class T>
    286 it<T> forwart_list<T>::begin()
    287 {
    288     return it<T>(this->phead);
    289 }
    290 
    291 template<class T>
    292 it<T> forwart_list<T>::end()
    293 {
    294     Node<T>*ptemp = phead;
    295     while (ptemp != nullptr)
    296     {
    297         ptemp = ptemp->pNext;
    298     }
    299     return it<T>(ptemp);
    300 }
  • 迭代it.h
     1 #pragma once
     2 #include "Node.h"
     3 #include <iostream>
     4 using namespace std;
     5 
     6 //通过,bgein(),end()实现迭代器,重载++
     7 template<class T>
     8 class it
     9 {
    10 public:
    11     Node<T>*p;
    12 
    13 public:
    14     it():p(nullptr)
    15     {
    16 
    17     }
    18 
    19     it(Node<T>* pnew) :p(pnew)
    20     {
    21 
    22     }
    23 
    24     ~it()
    25     {
    26         if (p != nullptr)
    27         {
    28             //不能删除
    29             //delete p;
    30         }
    31     }
    32 
    33     void operator++()
    34     {
    35         if (p != nullptr)
    36         {
    37             p = p->pNext;
    38         }
    39     }
    40 
    41     void operator++(int)
    42     {
    43         if (p != nullptr)
    44         {
    45             p = p->pNext;
    46         }
    47     }
    48 
    49     T operator *()
    50     {
    51         return p->t;
    52     }
    53 
    54     Node<T>* operator ->()
    55     {
    56         return p;
    57     }
    58 
    59     void showdata()
    60     {
    61         cout << this->p->t << endl;
    62     }
    63 
    64     template<class T>
    65     friend ostream& operator<<(ostream &out, it<T> &itt)
    66     { 
    67         out << itt.p->t;
    68         return out;
    69     }
    70 
    71     bool operator != (it<T> &myit)
    72     {
    73         if (this->p != myit.p)
    74         {
    75             return true;
    76         }
    77         else
    78         {
    79             return false;
    80         }
    81     }
    82 };
  • main.cpp
     1 #include <iostream>
     2 #include "forwart_list.h"
     3 #include "forwart_list.cpp"
     4 #include "it.h"
     5 using namespace std;
     6 
     7 template<class T,class F>
     8 void for_every(T begin,T end,F f)
     9 {
    10     for (auto ib = begin; ib != end;ib++ )
    11     {    
    12         f(*ib);
    13     }
    14 }
    15 
    16 void main()
    17 {
    18     forwart_list<int> mylist;
    19     for (int i = 0; i < 10; i++)
    20     {
    21         mylist.push_back(i);
    22     }
    23     //mylist.show();
    24 
    25     //for (auto i : mylist)
    26     //{
    27     //    //cout << i << endl;
    28     //}
    29 
    30     /*for (auto i = mylist.begin(); i != mylist.end(); i++)
    31     {
    32         cout << i << endl;
    33     }*/
    34 
    35     /*for (auto i : mylist)
    36     {
    37         cout << i << endl;
    38     }*/
    39     //反转
    40     //mylist.rev();
    41     mylist.insert(3, 5);
    42     mylist.deleteit(3);
    43     mylist.deleteit(5);
    44     /*mylist.insert(7, 5);
    45     mylist.deleteit(2);*/
    46     for_every(mylist.begin(), mylist.end(), [](int num) {cout << num << endl; });
    47     cin.get();
    48 }
原文地址:https://www.cnblogs.com/xiaochi/p/8712905.html