C++顺序表的构建(用数组存储数据)
来源:互联网 发布:qq聊天记录数据恢复 编辑:程序博客网 时间:2024/05/22 15:48
这是最简单的顺序表,顺序表中的元素都存储在数组T *data中
const int defaultSize = 100;template <class T>class LinearList{protected: T *data; int maxSize; int last; void reSize(int newSize);//改变数组的大小public: LinearList(int sz = defaultSize); LinearList(LinearList<T> &L); ~LinearList() { delete[]data; } int Size()const { return maxSize; } int Length()const { return last + 1; } int Search(T &x)const; int Locate(int i)const; bool getData(int i, T &x)const { if(i > 0 && i <= last + 1) { x = data[i + 1]; return true; } else return false; } void setData(int i, T &x)const { if(i > 0 && i <= last + 1) data[i - 1] = x; } bool Insert(int i, T &x)const; bool Remove(int i, T &x)const; bool IsEmpty(){return (last==-1) ? true : false;} bool IsFull(){return (last+1==maxSize) ? true : false;} void input(); void output(); LinearList<T> operator=(LinearList<T> &L);};//构造函数template<class T>LinearList<T>::LinearList(int sz){ if(sz>0){ data=new T[sz]; if(data==NULL){cerr<<"内存分配失败!"; exit(1);} last=-1; maxSize=sz; }}//复制构造函数template<class T>LinearList<T>::LinearList(LinearList<T> &L){ maxSize=L.Size(); last=L.Length()-1; data=new T[maxSize]; if(data==NULL){cerr<<"内存分配失败!"; exit(1);} T value; for(int i=0; i<=last;i++){ L.getData(i+1, value);//从中获取值 data[i]=value; }}template<class T>void LinearList<T>::reSize(int newSize){ if(newSize<=last+1){ cerr<<"数组空间太小!"<<endl; return;} if(newSize!=maxSize){ T *arr=new T[newSize]; if(arr==NULL){ cerr<<"内存分配失败!"; exit(1);} maxSize=newSize; for(int i=0; i<=last;i++){ arr[i]=data[i]; } delete []data; data=arr; }}template<class T>int LinearList<T>::Search(T &x)const{ for(int i=0; i<=last;i++){ if(data[i]==x)return i+1; } return -1;//搜索失败}//确定这个位置是否有数template<class T>int LinearList<T>::Locate(int i)const{ if(i<=0||i>last+1)return -1; else return i;}template<class T>void LinearList<T>::input(){ int num; cout<<"开始建立顺序表,请输入表中元素个数:"; while(1){ cin>>num; if(num>0&&num<=maxSize)break; cout<<"您输入的数字太小或太大,要大于0并且小于等于"<<maxSize<<endl; } last=num-1; for(int i=0;i<=last;i++){ cout<<"输入第"<<i+1<<"个元素:"<<endl; cin>>data[i]; }}template<class T>void LinearList<T>::output(){ cout<<"顺序表当前元素最后的位置last为:"<<last<<endl; for(int i=0;i<=last;i++)cout<<"第"<<i+1<<"个元素为:"<<data[i]<<endl;}template<class T>bool LinearList<T>::Remove(int i, T &x)const { if(i > 0 && i <= last + 1) { x=data[i-1]; for(int j = i - 1; j<=last-1; j++) data[j] = data[j + 1]; return true; } else return false; }template<class T>bool LinearList<T>:: Insert(int i, T &x)const { if(i > 0 && i <= last + 1) { for(int j = last+1 ; j >=i; j--) { data[j] = data[j-1]; } data[i - 1] = x; return true; } else return false; }//其实和上面复制构造函数是一致的template<class T>LinearList<T> LinearList<T>::operator=(LinearList<T>& L){ maxSize=L.Size(); data=new T[maxSize]; if(data==NULL){cerr<<"内存分配失败"<<endl; exit(1);} last=L.Length()-1; T value; for(int i=0;i<=last;i++){ L.getData(i+1,value); data[i]=value; }}
阅读全文
1 0
- C++顺序表的构建(用数组存储数据)
- C语言实现顺序存储(数组)
- 数组的顺序存储
- 线性表的顺序存储结构--用数组实现
- 数组实现线性表的顺序存储
- 栈的顺序存储结构(数组)
- 堆栈的顺序存储(数组)
- 学习笔记------数据结构(C语言版)数组的顺序存储
- 《C和指针》——数组的存储顺序
- 将一个数组中的数据按相反的顺序存储(将数组中的数据逆序输出)
- c语言的线性表的顺序存储的实现,数组
- 动态构建顺序表,及顺序表的插入删除算法(C语言)
- 顺序栈,数组存储栈元素(C++)
- 数据结构之---C语言实现数组的顺序存储表示(可运行)
- 线性表(用数组存储数据)
- 数组的顺序存储表示
- 数组的顺序存储表示
- 用C语言实现线性表的顺序存储结构
- NYOJ635
- 如何全面的了解html5,为什么是当今主流的设计开发类语言?
- 医院信息科笔试题(一)
- hi3519v101 load3519v101后网络不通问题解决
- supervisor Python2安装
- C++顺序表的构建(用数组存储数据)
- 【hdu 5053】the Sum of Cube
- ServletContext、ActionContext与ServletActionContext的区别
- 思维导图的学习和感想
- 车牌识别EasyPR--开发详解
- Vmware12上安装和配置ubuntu系统总结
- Unity中的start()和构造函数的关系
- 生产者2
- Puthon 调试