數據結構——動態鏈表(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指針就是我們需要找到的。刪除一個節點也一樣,需要找到指向該節點的指針。



原文轉自:http://www.tuicool.com/articles/nA7VjmU

原作者爲 dawanganban. 請尊重原作者版權