顺序表,数组就是典型的顺序表,顺序表便于查询元素,单移动、删除时间开销比较大,好了废话少说,直接上代码,对照源码看吧
- #ifndef SEQLIST_H
- #define SEQLIST_H
- #include <iostream>
- #include <stdlib.h>
- using std::cin;
- using std::cout;
- using std::cerr;
- using std::endl;
- using std::ostream;
- using std::istream;
- const int defaultSize = 100;
- const int initLen = 20;
- template <class T>
- class SeqList
- {
- public:
- SeqList(const int maxSize = defaultSize);//带默认参数的构造函数
- SeqList(const SeqList<T> &seqList);
- ~SeqList()
- {
- delete []data;//销毁数组
- }
- int length() const//返回顺序表内已有数据的长度
- {
- return last + 1;
- }
- int maxSize() const//返回顺序表的最大容量
- {
- return this->size;
- }
- bool isEmpty()//返回顺序表是否为空
- {
- return (last == -1);
- }
- bool isFull()//返回顺序表是否已满
- {
- return (last == size-1);
- }
- int search(T &x) const;//查找x是否在顺序表中存在,存在返回其位置,不存在返回0
- T& getData(int i) const//返回第i个编号位置的元素
- {
- if(i>0 && i<=last+1)
- {
- return data[i-1];
- }
- }
- void setData(int i, T &x)//对第i个编号位置的元素赋值
- {
- if (i>0 && i<=last+1){
- data[i-1] = x;
- }
- }
- bool insert(int i, T &x);//在第i个编号位置插入元素
- bool remove(int i, T &x);//把第i个编号位置的元素移走
- void reSize(int newSize);//调整顺序表的大小,关键是要把原来的数据复制过来
- SeqList<T>& operator= (const SeqList<T> &rhs);
- void init();//这个方法无实际意义,纯粹是为了测试用单独引入的1个方法
- friend ostream& operator << (ostream& os,const SeqList<T>& seqList)
- {
- for(int i = 0; i <= seqList.last; i++)
- {
- os << "#" << i+1 << ":" << seqList.data[i] << endl;
- }
- return os;
- }
- private:
- T *data;//存放的数组
- int size;//最大可存放的项
- int last;//当前尾巴元素的小标
- };
- template <class T>
- SeqList<T>::SeqList(int maxSize)
- {
- if(maxSize > 0)
- {
- this->size = maxSize;//创建数组
- this->last = -1;
- data = new T[maxSize];
- if(data == NULL)
- {
- cerr << "malloc memory failed!" << endl;
- return;
- }
- }
- }
- template <class T>
- SeqList<T>::SeqList(const SeqList<T>& seqList)
- {
- size = seqList.maxSize();
- last = seqList.length() - 1;
- data = new T[size];
- if (data == NULL){
- cerr << "Memory allocating error!" << endl;
- return;
- }
- for (int i = 1; i <= last+1; i++){
- data[i-1] = seqList.getData(i);
- }
- }
- template <class T>
- int SeqList<T>::search(T &x) const
- {
- for(int i = 0; i <= last; i++)
- {
- if(this->data[i] == x)//找到后返回下标
- {
- return i+1;
- }
- }
- return 0;
- }
- template <class T>
- bool SeqList<T>::insert(int i, T &x)
- {
- if(isFull())
- {
- return false;
- }
- if( i < 0 || i > last + 1)//注意我们讲的是顺序表
- {
- return false;
- }
- for(int j = last; j >= i-1; j--)
- {
- this->data[j+1] = this->data[j];
- }
- this->data[i-1] = x;//插入
- last++;//现有尾巴下标后移
- return true;
- }
- template <class T>
- bool SeqList<T>::remove(int i, T &x)
- {
- if(isEmpty())
- {
- return false;
- }
- if(i < 1 || i >= last+1)
- {
- return false;
- }
- x = this->data[i-1];
- for(int j = i; j <= last; j++)//后面的元素向前移
- {
- this->data[j-1] = this->data[j];
- }
- last--;
- return true;
- }
- template <class T>
- void SeqList<T>::reSize(int newSize)
- {
- if(newSize <= 0)
- {
- cerr << "invalid newSize!" << endl;
- return;
- }
- if(newSize != size)
- {
- int end = -1;//要拷贝数据的终止下标
- if(size < newSize)//新的容量比原来要大
- {
- end = last;
- }
- else //新的容量比原来小
- {
- end = newSize - 1;
- }
- T* newArray = new T[newSize];
- if( newArray == NULL)
- {
- cerr << "malloc memory failed!" << endl;
- return;
- }
- T* srcPtr = data;
- T* descPtr = newArray;
- for(int i = 0; i <= end; i++)
- {
- *descPtr++ = *srcPtr++;
- }
- delete []data;//删除原来的数组
- data = newArray;//data指向新的数组
- last = end;//重新记录尾巴元素的下标
- size = newSize;//重新新的容量
- }
- }
- template <class T>
- SeqList<T>& SeqList<T>::operator= (const SeqList<T> &rhs)
- {
- size = seqList.maxSize();
- last = seqList.length() - 1;
- if(data != NULL)
- {
- delete []data;
- }
- data = new T[size];
- if (data == NULL){
- cerr << "Memory allocating error!" << endl;
- return;
- }
- for (int i = 1; i <= last+1; i++)
- {
- data[i-1] = seqList.getData(i);
- }
- return *this;
- }
- template <class T>
- void SeqList<T>::init()
- {
- for(int i = 0; i < initLen; i++)
- {
- last++;
- this->data[last] = (T)rand() / 100;
- }
- }
- #endif