链式表的实现

来源:互联网 发布:m1216nfh 网络扫描 编辑:程序博客网 时间:2024/06/06 03:46

最头两天开始实现数据结构到现在,这个代码敲了三个多小时,悲剧啊,都怪当初c++很多东西都没有扎实(本来就没怎么学);

List类实现了基本的增删改查,拷贝构造函数,重载运算符。在实现重载运算符使其支持对象赋值的过程中,一定要将数据成员完全复制!否则num数据成员就不准确了。

另外实现拷贝构造函数时,也要完全复制,另外当时突然想起来以前看 primer plus时 ,书上说过,内存中临时创建的对象并不会默认调用构造函数,所以可能导致num,等

数据成员为随机值。 

另外我实现set_position_2()的时候利用保留当前位置的方法定位,一定要注意当我们在添加第一个结点的时候,也要把current指针指向添加的第一个元素,否则以后就没有办法来拿current来遍历定位了。然后当我们删除中间结点时,我们肯定要定位他的前驱结点,那么此时的current指针 和current_position是一个确切值然而当我删除第一个结点时,Remove()实现中并没有定位操作,所以删除的结点在current_position位置之前,那么删除之后,current指针 和current_position就不是一个确切值了,我们应该重置,使其再次确切。set_position_1()函数在实现中并没有遇到问题。


把两个定位操作函数放在protected中是因为,两个方法都要访问数据成员,并返回一个指向Node的指针,为了数据的安全性,我们把这两个定位方法声明为不可见的,并保证他

只能作为构造List其他方法时的可用工具;


const修饰能加的地方还是尽量要加上,确实在编译的时候能爆出很多可能潜在的错误。

以下是实现代码:


List.h:

[cpp] view plain copy print?
  1. /*============================================================================= 
  2. # 
  3. #      Author: liangshu - cbam  
  4. # 
  5. #      QQ : 756029571  
  6. # 
  7. #      School : 哈尔滨理工大学  
  8. # 
  9. #      Last modified: 2015-10-28 14:50 
  10. # 
  11. #     Filename: 链式表的实现 - 副本.cpp 
  12. # 
  13. #     Description:  
  14. #        The people who are crazy enough to think they can change the world, are the ones who do !  
  15. =============================================================================*/  
  16. #  
  17. using namespace std;  
  18. enum Error_code {success, overflow, underflow, Range_error};  
  19.   
  20. template<class Node_entry>  
  21. struct Node{  
  22.     Node_entry Entry;  
  23.     Node<Node_entry> * next;  
  24.     Node();  
  25.     Node(const Node_entry &item, Node<Node_entry> *link = NULL):Entry(item), next(link){}  
  26. };  
  27. template<class Node_entry>  
  28. Node<Node_entry>::Node(){  
  29.   next = NULL;  
  30. }  
  31. template<class List_entry>  
  32. class List{  
  33. public:  
  34.     List();  
  35.     List(const List<List_entry> &Copy);  
  36.     int Size_list()const{  
  37.     return num;  
  38.     }  
  39.     bool Empty_list()const{  
  40.        return num == 0;  
  41.     }  
  42.     void Clear();  
  43.     Error_code retrieve(int position, List_entry &item)const;  
  44.     Error_code Remove(int position, List_entry &item);  
  45.     Error_code Replace(int position, const List_entry &item);  
  46.     Error_code Insert(int position, const List_entry &item);  
  47.     void operator = (const List<List_entry> &Copy);  
  48.     void Print_list()const;  
  49.   
  50. protected:  
  51.     mutable int current_position;  
  52.     mutable Node<List_entry> *current;  
  53.     Node<List_entry>*head;  
  54.     Node<List_entry>*set_position_1(int postion)const;  
  55.     Node<List_entry>*set_position_2(int postion)const;  
  56.     int num;  
  57. };  
  58.   
  59. template<class List_entry>  
  60. List<List_entry>::List(){  
  61.   num = current_position = 0;  
  62.   head = NULL;  
  63.   current = head;  
  64. }  
  65.   
  66. template<class List_entry>  
  67. void List<List_entry>::Clear(){  
  68.     while(!Empty_list()){  
  69.         Node<List_entry> *old_head = head;  
  70.         head = head -> next;  
  71.         num--;  
  72.         delete old_head;  
  73.     }  
  74.     current = NULL;  
  75.     current_position = 0;  
  76. }  
  77.   
  78. template<class List_entry>  
  79. List<List_entry>::List(const List<List_entry> &Copy){  
  80.     Node<List_entry> *new_copy, *old_node = Copy.head;  
  81.     if(old_node == NULL){  
  82.         head = NULL;  
  83.     }  
  84.     else{  
  85.         head = new_copy = new Node<List_entry>(old_node -> Entry,old_node -> next);  
  86.         while(old_node -> next != NULL){  
  87.             old_node = old_node -> next;  
  88.             new_copy -> next = new Node<List_entry>(old_node -> Entry,old_node -> next);  
  89.             new_copy = new_copy -> next;  
  90.         }  
  91.     }  
  92.     num = Copy.num;  
  93.     current = Copy.current;  
  94.     current_position = Copy.current_position;  
  95. }  
  96. template<class List_entry>  
  97. Error_code List<List_entry>::retrieve(int position, List_entry &item)const{  
  98.     if(position < 0 || position > num){  
  99.         return Range_error;  
  100.     }  
  101.    Node<List_entry> *new_node = set_position_2(position);  
  102.    item = new_node -> Entry;  
  103.    return success;  
  104. }  
  105.   
  106. template<class List_entry>  
  107. Error_code List<List_entry>::Remove(int position, List_entry &item){  
  108.      if(position < 0 || position > num){  
  109.         return Range_error;  
  110.      }  
  111.      Node<List_entry>*previous, *old_node;  
  112.      if(position > 0){  
  113.         old_node = previous = set_position_1(position - 1);  
  114.         old_node = old_node -> next;  
  115.         item = old_node -> Entry;  
  116.         previous -> next = previous-> next -> next;  
  117.         delete old_node ;  
  118.      }  
  119.      else{  
  120.         old_node = head;  
  121.         head = old_node -> next;  
  122.         item = old_node -> Entry;  
  123.         delete old_node;  
  124.         current = head;  
  125.         current_position = 0;  
  126.      }  
  127.      num--;  
  128.      return success;  
  129. }  
  130.   
  131. template<class List_entry>  
  132. Error_code List<List_entry>::Replace(int position, const List_entry &item){  
  133.     if(position < 0 || position > num){  
  134.         return Range_error;  
  135.      }  
  136.      List_entry x;  
  137.      Remove(position, x);  
  138.      Insert(position, item);  
  139.      return success;  
  140. }  
  141.   
  142. template<class List_entry>  
  143. void List<List_entry>::operator = (const List<List_entry> &Copy){  
  144.     Node<List_entry> *new_head, *new_copy, *old_node = Copy.head;  
  145.     if(old_node == NULL){  
  146.         head = NULL;  
  147.     }  
  148.     else{  
  149.         new_head = new_copy = new Node<List_entry>(old_node -> Entry,old_node -> next);  
  150.         while(old_node -> next != NULL){  
  151.             old_node = old_node -> next;  
  152.             new_copy -> next = new Node<List_entry>(old_node -> Entry,old_node -> next);  
  153.             new_copy = new_copy -> next;  
  154.         }  
  155.     }  
  156.     while(!Empty_list()){  
  157.   
  158.         Node<List_entry> *old_head = head;  
  159.         head = head -> next;num--;  
  160.         delete old_head;  
  161.     }  
  162.     current = Copy.current;  
  163.     current_position = Copy.current_position;  
  164.     num = Copy.num;  
  165.     head = new_head;  
  166. }  
  167.   
  168. template<class List_entry>  
  169. Node<List_entry>* List<List_entry>::set_position_1(int position)const{  
  170.    Node<List_entry> *q = head;  
  171.    for(int i = 0; i < position; i++){  
  172.     q = q -> next;  
  173.    }  
  174.    return q;  
  175. }  
  176.   
  177. template<class List_entry>  
  178. Node<List_entry>* List<List_entry>::set_position_2(int position)const{  
  179.    if(position < current_position){  
  180.     current_position = 0;  
  181.     current = head;  
  182.    }  
  183.    for(;current_position != position; current_position++){  
  184.     current = current -> next;  
  185.    }  
  186.    return current;  
  187. }  
  188.   
  189. template<class List_entry>  
  190. void List<List_entry>:: Print_list()const{  
  191.    cout<<"| -- ";  
  192.    Node<List_entry> *old_head = head;  
  193.    while(old_head != NULL){  
  194.     cout<<old_head -> Entry<<" -- ";  
  195.        old_head = old_head -> next;  
  196.    }  
  197.    cout<<" |"<<endl;  
  198. }  
  199. template<class List_entry>  
  200. Error_code List<List_entry>::Insert(int position, const List_entry &item){  
  201.    if(position < 0 || position > num){  
  202.     return Range_error;  
  203.    }  
  204.    Node<List_entry> * new_node , *previous, *following;  
  205.    if(position > 0){  
  206.    previous = set_position_1(position - 1);  
  207.    // previous = current;  
  208.     following = previous -> next;  
  209.    }  
  210.    else{  
  211.     following = head;  
  212.    }  
  213.    new_node = new Node<List_entry>(item, following);  
  214.    if(new_node == NULL){  
  215.     return overflow;  
  216.    }  
  217.    if(position == 0){  
  218.     head = new_node;  
  219.     current = head;  
  220.    }  
  221.    else{  
  222.      previous -> next = new_node;  
  223.    }  
  224.    num ++;  
  225.    return success;  
  226. }  

Test.cpp:
[cpp] view plain copy print?
  1. #include<iostream>  
  2. #include"List.h"  
  3. using namespace std;  
  4. int main()  
  5. {  
  6.     List<int>list_1, list_2, list_3;  
  7.     for(int i = 1; i <= 5; i++){  
  8.        list_1.Insert(i - 1, i);  
  9.     }  
  10.     int x;  
  11.     list_1.Print_list();  
  12.     cout<<list_1.Size_list()<<endl;  
  13.   
  14.     list_1.retrieve(1, x);  
  15.     cout<<x<<endl;  
  16.   
  17.     list_1.Remove(2, x);  
  18.     cout<<"Remove ="<<x<<endl;  
  19.     list_1.Print_list();  
  20.     cout<<list_1.Size_list()<<endl;  
  21.     list_1.Replace(2, 100);  
  22.     list_1.Print_list();  
  23.   
  24.     list_1.Remove(0, x);  
  25.     cout<<"Remove2 = "<<x<<endl;  
  26.     list_1.Print_list();  
  27.     cout<<list_1.Size_list()<<endl;  
  28.   
  29.     list_1.Insert(3,123456);  
  30.     list_1.Print_list();  
  31.     cout<<list_1.Size_list()<<endl;  
  32.   
  33.     list_1.Clear();  
  34.     list_1.Print_list();  
  35.     cout<<list_1.Size_list()<<endl;  
  36.   
  37.     for(int i = 1; i <= 9; i++){  
  38.        list_1.Insert(i - 1, i);  
  39.     }  
  40.     list_1.Print_list();  
  41.     list_2 = list_1;  
  42.     list_2.Print_list();  
  43.     cout<<"ci = "<<endl;  
  44.     list_3 = List<int>(list_2);  
  45.     list_3.Print_list();  
  46.   
  47.     list_3.Remove(3,x);  
  48.     list_3.Print_list();  
  49.     list_3.Insert(0,19999);  
  50.     list_3.Insert(7, 1000009);  
  51.     list_3.retrieve(9, x);  
  52.     cout<<"x ="<<x<<endl;  
  53.     list_3.Print_list();  
  54.     return 0;  

0 0
原创粉丝点击