表的实现(顺序)一

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

SeqList.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-27 20:03 
  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. const int Dafultsize = 10;  
  19. enum Error_code{success, overflow, underflow, Range_error};  
  20. template<class List_entry>  
  21.   
  22. class List{  
  23.   public:  
  24.       List(int Size = Dafultsize):Max_list(Size), num(0){  
  25.         Entry = new List_entry[Max_list];  
  26.       }  
  27.       int Size_list()const;  
  28.       bool Full_list()const;  
  29.       bool Empty_list()const;  
  30.       void Clear();  
  31.       void Print_list()const;  
  32.       void Tra_list(void (*visit)(List_entry &));  
  33.       Error_code retrieve(int postion,  List_entry &item)const;  
  34.       Error_code Replace(int postion, const List_entry &item);  
  35.       Error_code Remove(int postion,  List_entry &item);  
  36.       Error_code Insert(int postion, const List_entry &item);  
  37.       ~List(){  
  38.          delete []Entry;  
  39.       }  
  40.   protected:  
  41.       int num;  
  42.       List_entry  *Entry;  
  43.       int Max_list;  
  44. };  
  45. template<class List_entry>  
  46. int List<List_entry>::Size_list()const{  
  47.   return num ;  
  48. }  
  49. template<class List_entry>  
  50. bool List<List_entry>::Full_list()const{  
  51.    return num == Max_list - 1;  
  52. }  
  53. template<class List_entry>  
  54. bool List<List_entry>::Empty_list()const{  
  55.    return num == 0;  
  56. }  
  57.   
  58. template<class List_entry>  
  59. void List<List_entry>::Clear(){  
  60.    num = 0;  
  61. }  
  62.   
  63. template<class List_entry>  
  64. Error_code List<List_entry>::Insert(int position, const List_entry &item){  
  65.     if(Full_list()){  
  66.         return overflow;  
  67.     }  
  68.     if(position < 0 || position > num){  
  69.         return Range_error;  
  70.     }  
  71.     for(int i = num - 1; i >= position; i--){  
  72.         Entry[i + 1] = Entry[i];  
  73.     }  
  74.     Entry[position] = item;  
  75.     num++;  
  76.     return success;  
  77. }  
  78.   
  79. template<class List_entry>  
  80. void List<List_entry>::Tra_list(void(*visit)(List_entry &)){  
  81.    for(int i = 0; i < num; i++){  
  82.         (*visit)(Entry[i]);  
  83.    }  
  84. }  
  85.   
  86. template<class List_entry>  
  87. Error_code List<List_entry>::retrieve(int position,  List_entry &item)const{  
  88.     if(Full_list()){  
  89.         return underflow;  
  90.     }  
  91.     if(position < 0 || position > num){  
  92.         return Range_error;  
  93.     }  
  94.     item = Entry[position];  
  95.     return success;  
  96. }  
  97.   
  98. template<class List_entry>  
  99. Error_code List<List_entry>::Replace(int position, const List_entry &item){  
  100.     if(position > num || position < 0){  
  101.         return Range_error;  
  102.     }  
  103.    Entry[position] = item;  
  104.    return success;  
  105. }  
  106. template<class List_entry>  
  107. Error_code List<List_entry>::Remove(int position,  List_entry &item){  
  108.    if(Empty_list()){  
  109.     return underflow;  
  110.    }  
  111.    if(position < 0 || position > num){  
  112.         return Range_error;  
  113.     }  
  114.     item = Entry[position];  
  115.    for(int i = position;i < num; i++){  
  116.        Entry[i] = Entry[i + 1];  
  117.    }  
  118.    num--;  
  119.    return success;  
  120. }  
  121. template<class List_entry>  
  122. void List<List_entry>::Print_list()const{  
  123.     cout<<"| ";  
  124.    for(int i = 0; i < num; i++){  
  125.     cout<<Entry[i];  
  126.     if(i != num - 1){  
  127.         cout<<" -- ";  
  128.     }  
  129.    }  
  130.    cout<<" |"<<endl;  
  131. }  

Test.cpp:

[cpp] view plain copy print?
  1. #include<iostream>  
  2. #include"a.h"  
  3. using namespace std;  
  4. int main()  
  5. {  
  6.     List<int>list_1(9);  
  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.Replace(2, 100);  
  18.     list_1.Print_list();  
  19.   
  20.     list_1.Remove(3, x);  
  21.     list_1.Print_list();  
  22.   
  23.     list_1.Clear();  
  24.     list_1.Print_list();  
  25.     return 0;  

0 0