zoukankan      html  css  js  c++  java
  • 01--数据结构——动态链表(C++)

     

    数据结构——动态链表(C++)

     

    定义一个节点:

     

    [cpp] view plain copy
     
     print?在CODE上查看代码片派生到我的代码片
    1. #include <iostream>  
    2. using namespace std;  
    3.   
    4. typedef int T;  
    5.   
    6. struct Node{  
    7.     T data;  
    8.     Node* next;  
    9.     Node(const T& d):data(d), next(NULL){}  
    10.     operator T(){  
    11.         return data;  
    12.     }  
    13. };  
    14.   
    15. int main(){  
    16.     Node a(10), b(20);  
    17.     cout << "a=" << a << ", b=" << b << endl;  
    18.     return 0;  
    19. }  
    上面的运算符重载,先将a类型强转为T类型(也就是int),Java中的toString实际上就是类似的强转成string类型的。

     

    输出一段简单的链表

     

    [cpp] view plain copy
     
     print?在CODE上查看代码片派生到我的代码片
    1. #include <iostream>  
    2. using namespace std;  
    3.   
    4. typedef int T;  
    5.   
    6. struct Node{  
    7.     T data;  
    8.     Node* next;  
    9.     Node(const T& d):data(d), next(NULL){}  
    10.     operator T(){  
    11.         return data;  
    12.     }     
    13. };  
    14.   
    15. int main(){  
    16.     Node a(10), b(20), c(30), d(40), e(50);  
    17.     a.next = &b;   
    18.     b.next = &c;   
    19.     c.next = &d;   
    20.     Node *p = &a;   
    21.     while(p != NULL){  
    22.         cout << *p << ' ';  
    23.         p = p->next;  
    24.     }  
    25.     cout << endl;  
    26.     return 0;  
    27. }  
    给链表添加一个元素

     

     

    [cpp] view plain copy
     
     print?在CODE上查看代码片派生到我的代码片
    1. #include <iostream>  
    2. using namespace std;  
    3.   
    4. typedef int T;  
    5.   
    6. struct Node{  
    7.     T data;  
    8.     Node* next;  
    9.     Node(const T& d):data(d), next(NULL){}  
    10.     operator T(){  
    11.         return data;  
    12.     }  
    13. };  
    14.   
    15. //输出链表  
    16. void showlist(Node* p){  
    17.     while(p != NULL){  
    18.         cout << *p << ' ';  
    19.         p = p->next;  
    20.     }  
    21.     cout << endl;  
    22. }  
    23.   
    24. int main(){  
    25.     Node a(10), b(20), c(30), d(40), e(50);  
    26.     a.next = &b;  
    27.     b.next = &c;  
    28.     c.next = &d;  
    29.     showlist(&a);  
    30.     //添加一个节点  
    31.     Node* & p = b.next;//取b.next指针的别名  
    32.     e.next = p;  
    33.     p = &e;  
    34.     showlist(&a);  
    35.     //再添加一个节点  
    36.     Node* k = new Node(70);  
    37.     Node*& r = c.next;  
    38.     k->next = r;  
    39.     r = k;  
    40.   
    41.     return 0;  
    42. }  

    一个C++实现的链表如下:

     

    [cpp] view plain copy
     
     print?在CODE上查看代码片派生到我的代码片
    1. #include <iostream>  
    2. using namespace std;  
    3.   
    4. typedef int T;  
    5.   
    6. class List{  
    7.     struct Node{  
    8.         T data;  
    9.         Node * next;  
    10.         //T()零初始化  
    11.         Node(const T& d=T()):data(d), next(0){}  
    12.     };  
    13.     Node * head; //头指针  
    14.     int len;  
    15. public:  
    16.     List():head(NULL),len(0){ }  
    17.   
    18.     //插入到任何位置  
    19.     //1、在链表里找到指向那个位置的指针pn  
    20.     //2、让新节点的next成员和pn指向同一个地方  
    21.     //3、再让pn指向新节点  
    22.     void insert(const T&d, int pos){  
    23.         Node*& pn = getptr(pos);  
    24.         Node* p = new Node(d);  
    25.         p->next = pn;  
    26.         pn = p;  
    27.         len++;    
    28.     }  
    29.       
    30.     //返回链表长度  
    31.     int size()const{  
    32.         return len;  
    33.     }  
    34.   
    35.     //尾插  
    36.     void push_back(const T& d){  
    37.         insert(d, size());  
    38.     }  
    39.   
    40.     //找链表中指向指定位置的指针  
    41.     Node*& getptr(int pos){  
    42.         if(pos<0 || pos>size()) pos = 0;  
    43.         if(pos==0) return head;  
    44.         Node* p = head;  
    45.         for(int i=1; i<pos; i++)  
    46.             p = p->next;  
    47.         return p->next;  
    48.     }  
    49.     //前插  
    50.     void push_front(const T& d){  
    51.         insert(d, 0);  
    52.     }  
    53.   
    54.     //遍历  
    55.     void travel()const{  
    56.         Node* p = head;  
    57.         while(p!=NULL){  
    58.             cout << p->data << ' ';  
    59.             p = p->next;  
    60.         }  
    61.         cout << endl;  
    62.     }  
    63.   
    64.     //清空  
    65.     void clear(){  
    66.         while(head!=NULL){  
    67.             Node * p = head->next;  
    68.             delete head;  
    69.             head = p;  
    70.         }  
    71.         len = 0;  
    72.     }  
    73.   
    74.     ~List(){  
    75.         clear();  
    76.     }  
    77.   
    78.     //按照位置删除  
    79.     //1、找到链表中指向那个位置的指针  
    80.     //2、把那个指针另存一份  
    81.     //3、让那个指针指向下一个节点  
    82.     //4、释放那个指针的动态内存  
    83.     void erase(int pos){  
    84.         if(pos<0 || pos>=size()) return;  
    85.         Node *& pn = getptr(pos);  
    86.         Node * p = pn;  
    87.         pn = pn->next;  
    88.         delete p;  
    89.         --len;  
    90.     }  
    91.       
    92.     //根据元素查找位置  
    93.     int find(const T& d)const{  
    94.         int pos = 0;  
    95.         Node* p = head;  
    96.         while(p){  
    97.             if(p->data==d) return pos;  
    98.             p = p->next;  
    99.             ++pos;  
    100.         }  
    101.         return -1;  
    102.     }  
    103.   
    104.     //根据元素删除  
    105.     void remove(const T& d){  
    106.         int pos;  
    107.         while((pos = find(d)) != -1)  
    108.             erase(pos);  
    109.     }  
    110.   
    111. };  
    112.   
    113. int main(){  
    114.     List l;  
    115.     l.push_front(5);  
    116.     l.push_front(8);  
    117.     l.push_front(20);  
    118.     //在第2个位置插入9  
    119.     l.insert(9, 2);  
    120.     l.travel();  
    121.   
    122.     return 0;  
    123. }  

     

    通过上图可以看出来,如果我们要插入一个节点,就需要找到指向该位置的指针(或者前一个结点),比如上图的p->next指针就是我们需要找到的。删除一个节点也一样,需要找到指向该节点的指针。

     

    数据结构——动态链表(C++)

    标签: 数据结构链表C++
     958人阅读 评论(0) 收藏 举报
     分类:
     

    定义一个节点:

     

    [cpp] view plain copy
     
     print?在CODE上查看代码片派生到我的代码片
    1. #include <iostream>  
    2. using namespace std;  
    3.   
    4. typedef int T;  
    5.   
    6. struct Node{  
    7.     T data;  
    8.     Node* next;  
    9.     Node(const T& d):data(d), next(NULL){}  
    10.     operator T(){  
    11.         return data;  
    12.     }  
    13. };  
    14.   
    15. int main(){  
    16.     Node a(10), b(20);  
    17.     cout << "a=" << a << ", b=" << b << endl;  
    18.     return 0;  
    19. }  
    上面的运算符重载,先将a类型强转为T类型(也就是int),Java中的toString实际上就是类似的强转成string类型的。

     

    输出一段简单的链表

     

    [cpp] view plain copy
     
     print?在CODE上查看代码片派生到我的代码片
    1. #include <iostream>  
    2. using namespace std;  
    3.   
    4. typedef int T;  
    5.   
    6. struct Node{  
    7.     T data;  
    8.     Node* next;  
    9.     Node(const T& d):data(d), next(NULL){}  
    10.     operator T(){  
    11.         return data;  
    12.     }     
    13. };  
    14.   
    15. int main(){  
    16.     Node a(10), b(20), c(30), d(40), e(50);  
    17.     a.next = &b;   
    18.     b.next = &c;   
    19.     c.next = &d;   
    20.     Node *p = &a;   
    21.     while(p != NULL){  
    22.         cout << *p << ' ';  
    23.         p = p->next;  
    24.     }  
    25.     cout << endl;  
    26.     return 0;  
    27. }  
    给链表添加一个元素

     

     

    [cpp] view plain copy
     
     print?在CODE上查看代码片派生到我的代码片
    1. #include <iostream>  
    2. using namespace std;  
    3.   
    4. typedef int T;  
    5.   
    6. struct Node{  
    7.     T data;  
    8.     Node* next;  
    9.     Node(const T& d):data(d), next(NULL){}  
    10.     operator T(){  
    11.         return data;  
    12.     }  
    13. };  
    14.   
    15. //输出链表  
    16. void showlist(Node* p){  
    17.     while(p != NULL){  
    18.         cout << *p << ' ';  
    19.         p = p->next;  
    20.     }  
    21.     cout << endl;  
    22. }  
    23.   
    24. int main(){  
    25.     Node a(10), b(20), c(30), d(40), e(50);  
    26.     a.next = &b;  
    27.     b.next = &c;  
    28.     c.next = &d;  
    29.     showlist(&a);  
    30.     //添加一个节点  
    31.     Node* & p = b.next;//取b.next指针的别名  
    32.     e.next = p;  
    33.     p = &e;  
    34.     showlist(&a);  
    35.     //再添加一个节点  
    36.     Node* k = new Node(70);  
    37.     Node*& r = c.next;  
    38.     k->next = r;  
    39.     r = k;  
    40.   
    41.     return 0;  
    42. }  

    一个C++实现的链表如下:

     

    [cpp] view plain copy
     
     print?在CODE上查看代码片派生到我的代码片
    1. #include <iostream>  
    2. using namespace std;  
    3.   
    4. typedef int T;  
    5.   
    6. class List{  
    7.     struct Node{  
    8.         T data;  
    9.         Node * next;  
    10.         //T()零初始化  
    11.         Node(const T& d=T()):data(d), next(0){}  
    12.     };  
    13.     Node * head; //头指针  
    14.     int len;  
    15. public:  
    16.     List():head(NULL),len(0){ }  
    17.   
    18.     //插入到任何位置  
    19.     //1、在链表里找到指向那个位置的指针pn  
    20.     //2、让新节点的next成员和pn指向同一个地方  
    21.     //3、再让pn指向新节点  
    22.     void insert(const T&d, int pos){  
    23.         Node*& pn = getptr(pos);  
    24.         Node* p = new Node(d);  
    25.         p->next = pn;  
    26.         pn = p;  
    27.         len++;    
    28.     }  
    29.       
    30.     //返回链表长度  
    31.     int size()const{  
    32.         return len;  
    33.     }  
    34.   
    35.     //尾插  
    36.     void push_back(const T& d){  
    37.         insert(d, size());  
    38.     }  
    39.   
    40.     //找链表中指向指定位置的指针  
    41.     Node*& getptr(int pos){  
    42.         if(pos<0 || pos>size()) pos = 0;  
    43.         if(pos==0) return head;  
    44.         Node* p = head;  
    45.         for(int i=1; i<pos; i++)  
    46.             p = p->next;  
    47.         return p->next;  
    48.     }  
    49.     //前插  
    50.     void push_front(const T& d){  
    51.         insert(d, 0);  
    52.     }  
    53.   
    54.     //遍历  
    55.     void travel()const{  
    56.         Node* p = head;  
    57.         while(p!=NULL){  
    58.             cout << p->data << ' ';  
    59.             p = p->next;  
    60.         }  
    61.         cout << endl;  
    62.     }  
    63.   
    64.     //清空  
    65.     void clear(){  
    66.         while(head!=NULL){  
    67.             Node * p = head->next;  
    68.             delete head;  
    69.             head = p;  
    70.         }  
    71.         len = 0;  
    72.     }  
    73.   
    74.     ~List(){  
    75.         clear();  
    76.     }  
    77.   
    78.     //按照位置删除  
    79.     //1、找到链表中指向那个位置的指针  
    80.     //2、把那个指针另存一份  
    81.     //3、让那个指针指向下一个节点  
    82.     //4、释放那个指针的动态内存  
    83.     void erase(int pos){  
    84.         if(pos<0 || pos>=size()) return;  
    85.         Node *& pn = getptr(pos);  
    86.         Node * p = pn;  
    87.         pn = pn->next;  
    88.         delete p;  
    89.         --len;  
    90.     }  
    91.       
    92.     //根据元素查找位置  
    93.     int find(const T& d)const{  
    94.         int pos = 0;  
    95.         Node* p = head;  
    96.         while(p){  
    97.             if(p->data==d) return pos;  
    98.             p = p->next;  
    99.             ++pos;  
    100.         }  
    101.         return -1;  
    102.     }  
    103.   
    104.     //根据元素删除  
    105.     void remove(const T& d){  
    106.         int pos;  
    107.         while((pos = find(d)) != -1)  
    108.             erase(pos);  
    109.     }  
    110.   
    111. };  
    112.   
    113. int main(){  
    114.     List l;  
    115.     l.push_front(5);  
    116.     l.push_front(8);  
    117.     l.push_front(20);  
    118.     //在第2个位置插入9  
    119.     l.insert(9, 2);  
    120.     l.travel();  
    121.   
    122.     return 0;  
    123. }  

     

    通过上图可以看出来,如果我们要插入一个节点,就需要找到指向该位置的指针(或者前一个结点),比如上图的p->next指针就是我们需要找到的。删除一个节点也一样,需要找到指向该节点的指针。

  • 相关阅读:
    Python-Image 基本的图像处理操作
    剪枝
    poj1182(食物链)续
    HLG2035广搜
    HLG2040二叉树遍历已知前中,求后
    先序,中序,后序,已知两者求第三者
    C++中new的解说
    僵尸进程
    HLG2062(make,heap问题)
    make_head,,,pop_head,,,push_head,,,sort_head..
  • 原文地址:https://www.cnblogs.com/wohenben/p/5407841.html
Copyright © 2011-2022 走看看