基本数据结构:顺序表

顺序表,数组就是典型的顺序表,顺序表便于查询元素,单移动、删除时间开销比较大,好了废话少说,直接上代码,对照源码看吧

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #ifndef SEQLIST_H  
  2. #define SEQLIST_H  
  3.   
  4. #include <iostream>  
  5. #include <stdlib.h>  
  6. using std::cin;  
  7. using std::cout;  
  8. using std::cerr;  
  9. using std::endl;  
  10. using std::ostream;  
  11. using std::istream;  
  12.   
  13. const int defaultSize = 100;  
  14. const int initLen = 20;  
  15.   
  16. template <class T>  
  17. class SeqList  
  18. {  
  19.   
  20. public:  
  21.     SeqList(const int maxSize = defaultSize);//带默认参数的构造函数  
  22.     SeqList(const SeqList<T> &seqList);  
  23.     ~SeqList()  
  24.     {  
  25.         delete []data;//销毁数组  
  26.     }  
  27.   
  28.     int length() const//返回顺序表内已有数据的长度  
  29.     {  
  30.         return last + 1;  
  31.     }  
  32.   
  33.     int maxSize() const//返回顺序表的最大容量  
  34.     {  
  35.         return this->size;  
  36.     }  
  37.   
  38.     bool isEmpty()//返回顺序表是否为空  
  39.     {  
  40.         return (last == -1);  
  41.     }  
  42.   
  43.     bool isFull()//返回顺序表是否已满  
  44.     {  
  45.         return (last == size-1);  
  46.     }  
  47.   
  48.     int search(T &x) const;//查找x是否在顺序表中存在,存在返回其位置,不存在返回0  
  49.   
  50.     T& getData(int i) const//返回第i个编号位置的元素  
  51.     {  
  52.         if(i>0 && i<=last+1)  
  53.         {  
  54.             return data[i-1];  
  55.         }  
  56.           
  57.     }  
  58.   
  59.     void setData(int i, T &x)//对第i个编号位置的元素赋值  
  60.     {  
  61.         if (i>0 && i<=last+1){  
  62.             data[i-1] = x;  
  63.         }  
  64.           
  65.     }  
  66.   
  67.     bool insert(int i, T &x);//在第i个编号位置插入元素  
  68.     bool remove(int i, T &x);//把第i个编号位置的元素移走  
  69.     void reSize(int newSize);//调整顺序表的大小,关键是要把原来的数据复制过来  
  70.     SeqList<T>& operator= (const SeqList<T> &rhs);  
  71.     void init();//这个方法无实际意义,纯粹是为了测试用单独引入的1个方法  
  72.   
  73.     friend ostream& operator << (ostream& os,const SeqList<T>& seqList)  
  74.     {  
  75.         for(int i = 0; i <= seqList.last; i++)  
  76.         {  
  77.             os << "#" << i+1 << ":" << seqList.data[i] << endl;  
  78.         }  
  79.   
  80.         return os;  
  81.     }  
  82.   
  83. private:  
  84.     T *data;//存放的数组  
  85.     int size;//最大可存放的项  
  86.     int last;//当前尾巴元素的小标  
  87. };  
  88.   
  89.   
  90. template <class T>  
  91. SeqList<T>::SeqList(int maxSize)  
  92. {  
  93.     if(maxSize > 0)  
  94.     {  
  95.         this->size = maxSize;//创建数组  
  96.         this->last = -1;  
  97.         data = new T[maxSize];  
  98.         if(data == NULL)  
  99.         {  
  100.             cerr << "malloc memory failed!" << endl;  
  101.             return;  
  102.         }  
  103.     }  
  104. }  
  105.   
  106. template <class T>  
  107. SeqList<T>::SeqList(const SeqList<T>& seqList)  
  108. {  
  109.     size = seqList.maxSize();  
  110.     last = seqList.length() - 1;  
  111.     data = new T[size];  
  112.   
  113.     if (data == NULL){  
  114.         cerr << "Memory allocating error!" << endl;  
  115.         return;  
  116.     }  
  117.   
  118.     for (int i = 1; i <= last+1; i++){  
  119.         data[i-1] = seqList.getData(i);  
  120.     }  
  121. }  
  122.   
  123. template <class T>  
  124. int SeqList<T>::search(T &x) const  
  125. {  
  126.     for(int i = 0; i <= last; i++)  
  127.     {  
  128.         if(this->data[i] == x)//找到后返回下标  
  129.         {  
  130.             return i+1;  
  131.         }  
  132.     }  
  133.   
  134.     return 0;  
  135. }  
  136.   
  137. template <class T>  
  138. bool SeqList<T>::insert(int i, T &x)  
  139. {  
  140.     if(isFull())  
  141.     {  
  142.         return false;  
  143.     }  
  144.   
  145.     if( i < 0 || i > last + 1)//注意我们讲的是顺序表  
  146.     {  
  147.         return false;  
  148.     }  
  149.   
  150.     for(int j = last; j >= i-1; j--)  
  151.     {  
  152.         this->data[j+1] = this->data[j];  
  153.     }  
  154.     this->data[i-1] = x;//插入  
  155.     last++;//现有尾巴下标后移  
  156.   
  157.     return true;  
  158. }  
  159.   
  160. template <class T>  
  161. bool SeqList<T>::remove(int i, T &x)  
  162. {  
  163.     if(isEmpty())  
  164.     {  
  165.         return false;  
  166.     }  
  167.     if(i < 1 || i >= last+1)  
  168.     {  
  169.         return false;  
  170.     }  
  171.       
  172.     x = this->data[i-1];  
  173.     for(int j = i; j <= last; j++)//后面的元素向前移  
  174.     {  
  175.         this->data[j-1] = this->data[j];  
  176.     }  
  177.   
  178.     last--;  
  179.     return true;  
  180. }  
  181.   
  182. template <class T>  
  183. void SeqList<T>::reSize(int newSize)  
  184. {  
  185.     if(newSize <= 0)  
  186.     {  
  187.         cerr << "invalid newSize!" << endl;  
  188.         return;  
  189.     }  
  190.   
  191.   
  192.     if(newSize != size)  
  193.     {  
  194.         int end = -1;//要拷贝数据的终止下标  
  195.         if(size < newSize)//新的容量比原来要大  
  196.         {  
  197.             end = last;  
  198.         }  
  199.         else //新的容量比原来小  
  200.         {  
  201.             end = newSize - 1;  
  202.         }  
  203.   
  204.         T* newArray = new T[newSize];  
  205.         if( newArray == NULL)  
  206.         {  
  207.             cerr << "malloc memory failed!" << endl;  
  208.             return;  
  209.         }  
  210.   
  211.         T* srcPtr = data;  
  212.         T* descPtr = newArray;  
  213.         for(int i = 0; i <= end; i++)  
  214.         {  
  215.             *descPtr++ = *srcPtr++;  
  216.         }  
  217.   
  218.         delete []data;//删除原来的数组  
  219.         data = newArray;//data指向新的数组  
  220.         last = end;//重新记录尾巴元素的下标  
  221.         size = newSize;//重新新的容量  
  222.     }  
  223. }  
  224.   
  225. template <class T>  
  226. SeqList<T>& SeqList<T>::operator= (const SeqList<T> &rhs)  
  227. {  
  228.     size = seqList.maxSize();  
  229.     last = seqList.length() - 1;  
  230.     if(data != NULL)  
  231.     {  
  232.         delete []data;  
  233.     }  
  234.     data = new T[size];  
  235.   
  236.     if (data == NULL){  
  237.         cerr << "Memory allocating error!" << endl;  
  238.         return;  
  239.     }  
  240.   
  241.     for (int i = 1; i <= last+1; i++)  
  242.     {  
  243.         data[i-1] = seqList.getData(i);  
  244.     }  
  245.   
  246.     return *this;  
  247. }  
  248.   
  249. template <class T>  
  250. void SeqList<T>::init()  
  251. {  
  252.     for(int i = 0; i < initLen; i++)  
  253.     {  
  254.         last++;  
  255.         this->data[last] = (T)rand() / 100;  
  256.     }  
  257. }  
  258. #endif  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值