顺序表

  为了加深大家对C++的理解,我们还是继续通过实践的方式,今天,我们一起写一个顺序表,具体代码如下。

Seqlist.h具体内容如下:

[cpp]  view plain copy
  1. const int DefaultSize = 100;  
  2.   
  3. template <typename Type> class SeqList{  
  4. public:  
  5.     SeqList(int sz = DefaultSize)  
  6.         :m_nmaxsize(sz), m_ncurrentsize(-1){  
  7.         if (sz > 0){  
  8.             m_elements = new Type[m_nmaxsize];  
  9.         }  
  10.     }  
  11.     ~SeqList(){  
  12.         delete[] m_elements;  
  13.     }  
  14.     int Length() const{                 //get the length  
  15.         return m_ncurrentsize + 1;  
  16.     }  
  17.     int Find(Type x) const;             //find the position of x  
  18.     int IsElement(Type x) const;        //is it in the list  
  19.     int Insert(Type x, int i);          //insert data  
  20.     int Remove(Type x);                 //delete data  
  21.     int IsEmpty(){  
  22.         return m_ncurrentsize == -1;  
  23.     }  
  24.     int IsFull(){  
  25.         return m_ncurrentsize == m_nmaxsize - 1;  
  26.     }  
  27.     Type Get(int i){                    //get the ith data  
  28.         return i<0 || i>m_ncurrentsize ? (cout << "can't find the element" << endl, 0) : m_elements[i];  
  29.     }  
  30.     void Print();  
  31.   
  32. private:  
  33.     Type *m_elements;  
  34.     const int m_nmaxsize;  
  35.     int m_ncurrentsize;  
  36. };  
  37.   
  38. template <typename Type> int SeqList<Type>::Find(Type x) const{  
  39.     for (int i = 0; i < m_ncurrentsize; i++)  
  40.         if (m_elements[i] == x)  
  41.             return i;  
  42.     cout << "can't find the element you want to find" << endl;  
  43.     return -1;  
  44. }  
  45.   
  46. template <typename Type> int SeqList<Type>::IsElement(Type x) const{  
  47.     if (Find(x) == -1)  
  48.         return 0;  
  49.     return 1;  
  50. }  
  51.   
  52. template <typename Type> int SeqList<Type>::Insert(Type x, int i){  
  53.     if (i<0 || i>m_ncurrentsize + 1 || m_ncurrentsize == m_nmaxsize - 1){  
  54.         cout << "the operate is illegal" << endl;  
  55.         return 0;  
  56.     }  
  57.     m_ncurrentsize++;  
  58.     for (int j = m_ncurrentsize; j > i; j--){  
  59.         m_elements[j] = m_elements[j - 1];  
  60.     }  
  61.     m_elements[i] = x;  
  62.     return 1;  
  63. }  
  64.   
  65. template <typename Type> int SeqList<Type>::Remove(Type x){  
  66.     int size = m_ncurrentsize;  
  67.     for (int i = 0; i < m_ncurrentsize;){  
  68.         if (m_elements[i] == x){  
  69.             for (int j = i; j < m_ncurrentsize; j++){  
  70.                 m_elements[j] = m_elements[j + 1];  
  71.             }  
  72.             m_ncurrentsize--;  
  73.             continue;  
  74.         }  
  75.         i++;  
  76.     }  
  77.     if (size == m_ncurrentsize){  
  78.         cout << "can't find the element you want to remove" << endl;  
  79.         return 0;  
  80.     }  
  81.     return 1;  
  82. }  
  83.   
  84. template <typename Type> void SeqList<Type>::Print(){  
  85.     for (int i = 0; i <= m_ncurrentsize; i++)  
  86.         cout << i + 1 << ":\t" << m_elements[i] << endl;  
  87.     cout << endl << endl;  
  88. }  
main.cpp内容如下:

[cpp]  view plain copy
  1. #include <iostream>  
  2. #include "SeqList.h"  
  3. using namespace std;  
  4. int main()  
  5. {  
  6.     SeqList<int> test(15);  
  7.     int array[15] = { 2, 5, 8, 1, 9, 9, 7, 6, 4, 3, 2, 9, 7, 7, 9 };  
  8.     for (int i = 0; i < 15; i++){  
  9.         test.Insert(array[i], 0);  
  10.     }  
  11.     test.Insert(1, 0);  
  12.     cout << (test.Find(0) ? "can't be found " : "Be found ") << 0 << endl << endl;  
  13.     test.Remove(7);  
  14.     test.Print();  
  15.     test.Remove(9);  
  16.     test.Print();  
  17.     test.Remove(0);  
  18.     test.Print();  
  19.     cin.get();  
  20.     return 0;  
  21. }  
运行效果如图1所示:

图1 运行效果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值