顺序表的类模板定义
//---------------------------------------ClassArrList.h--------------------------------------------------
template <class T>
class arrList
{ // 顺序表,向量
private: // 线性表的取值类型和取值空间
T *aList ; // 私有变量,存储顺序表的实例
int maxSize; // 私有变量,顺序表实例的最大长度
int curLen; // 私有变量,顺序表实例的当前长度
int position; // 私有变量,当前处理位置
public: // 顺序表的运算集
arrList(const int size)
{ // 创建一个新的顺序表,参数为表实例的最大长度
maxSize = size; aList = new T[maxSize]; curLen = position = 0;
}
~arrList()
{ // 析构函数,用于消除该表实例
delete [] aList;
cout<<"析构函数"<<endl;
}
void clear()
{ // 将顺序表存储的内容清除,成为空表
delete [] aList; curLen = position = 0;
aList = new T[maxSize];
}
int length(); // 返回此顺序表的当前实际长度
bool append(const T value); // 在表尾添加一个元素value,表的长度增1
bool insert(const int p, const T value); // 在位置p上插入一个元素value,表的长度增1
bool delete1(const int p); // 删除位置p上的元素,表的长度减 1
bool setValue(const int p, const T value); // 用value修改位置p的元素值
bool getValue(const int p, T& value); // 把位置p的元素值返回到变量value中
bool getPos(int & p, const T value); // 查找值为value的元素,并返回第1次出现的位置
void output();
};
template <class T>
int arrList<T>::length()
{
return curLen;
}
template <class T>
bool arrList<T>::append(const T value)
{
aList[curLen++]=value;
return true;
}
template <class T>
bool arrList<T>::insert(const int p, const T value) // 在位置p上插入一个元素value,表的长度增1
{
if(curLen>=maxSize)
cout<<"The list is overflow!"<<endl;
if(p<0||p>=maxSize)
cout<<"The position is illegal!"<<endl;
for(int i=curLen-1;i>=p;i--)
aList[i+1]=aList[i];
curLen++;
aList[p]=value;
return true;
}
template <class T>
bool arrList<T>::delete1(const int p) // 删除位置p上的元素,表的长度减 1
{
if(curLen>=maxSize)
cout<<"The list is overflow!"<<endl;
if(p<0||p>=maxSize)
cout<<"The position is illegal!"<<endl;
for(int i=p;i<curLen-1;i++)
aList[i]=aList[i+1];
curLen--;
return true;
}
template <class T> bool arrList<T>::setValue(const int p, const T value) // 用value修改位置p的元素值
{
if(curLen>=maxSize)
cout<<"The list is overflow!"<<endl;
if(p<0||p>maxSize)
cout<<"The position is illegal!"<<endl;
aList[p]=value;
return true;
}
template <class T>
bool arrList<T>::getValue(const int p, T& value) // 把位置p的元素值返回到变量value中
{
if(curLen>=maxSize)
cout<<"The list is overflow!"<<endl;
if(p<0||p>maxSize)
cout<<"The position is illegal!"<<endl;
value=aList[p];
return true;
}
template <class T>
bool arrList<T>::getPos(int & p,const T value) // 查找值为value的元素,并返回第1次出现的位置
{
if(curLen>=maxSize)
cout<<"The list is overflow!"<<endl;
for(int i=0;i<curLen;i++)
{
if(aList[i]==value)
{
p=i;
break;
}
}
return true;
}
template <class T>
void arrList<T>::output() //输出顺序表函数
{
for(int i=0;i<length();i++)
{
T value;
getValue(i,value);
cout<<value<<" ";
}
cout<<endl;
}
//------------------------------------main fun.cpp-------------------------------------
#include "iostream"
#include "ClassArrList.h"
using namespace std;
int main()
{
cout<<"----------------初始化顺序表-----------------"<<endl;
arrList <int> list(13);
int input;
int i;
cout<<"请输入6个数字:";
for(i=0;i<6;i++)
{
cin>>input;
list.append(input);
}
list.output();
cout<<"--------------初始的顺序表长度---------------"<<endl;
cout<<"length of arrList:"<<list.length()<<endl;
cout<<"-----------在第三位上插入一个数据------------"<<endl;
list.insert(3,7);
list.output();
cout<<"-------------删除第三位上的数据--------------"<<endl;
list.delete1(3);
list.output();
cout<<"-------------修改第三为上的数据--------------"<<endl;
list.setValue(3,7);
list.output();
cout<<"------------返回第三为上的数据值-------------"<<endl;
int value;
list.getValue(3,value);
cout<<"The third value of the list is "<<value<<endl;
cout<<"-----输出执行了查找第一次位置操作的结果------"<<endl;
int p;
list.getPos(p,7);
cout<<"7 is first appeared in list is the position "<<p<<endl;
cout<<"----------------调用清空函数-----------------"<<endl;
list.clear();
cout<<"the longth of list is "<<list.length()<<endl;
cout<<"----------------调用析构函数-----------------"<<endl;
return 0;
}
template <class T>
class arrList
{ // 顺序表,向量
private: // 线性表的取值类型和取值空间
T *aList ; // 私有变量,存储顺序表的实例
int maxSize; // 私有变量,顺序表实例的最大长度
int curLen; // 私有变量,顺序表实例的当前长度
int position; // 私有变量,当前处理位置
public: // 顺序表的运算集
arrList(const int size)
{ // 创建一个新的顺序表,参数为表实例的最大长度
maxSize = size; aList = new T[maxSize]; curLen = position = 0;
}
~arrList()
{ // 析构函数,用于消除该表实例
delete [] aList;
cout<<"析构函数"<<endl;
}
void clear()
{ // 将顺序表存储的内容清除,成为空表
delete [] aList; curLen = position = 0;
aList = new T[maxSize];
}
int length(); // 返回此顺序表的当前实际长度
bool append(const T value); // 在表尾添加一个元素value,表的长度增1
bool insert(const int p, const T value); // 在位置p上插入一个元素value,表的长度增1
bool delete1(const int p); // 删除位置p上的元素,表的长度减 1
bool setValue(const int p, const T value); // 用value修改位置p的元素值
bool getValue(const int p, T& value); // 把位置p的元素值返回到变量value中
bool getPos(int & p, const T value); // 查找值为value的元素,并返回第1次出现的位置
void output();
};
template <class T>
int arrList<T>::length()
{
return curLen;
}
template <class T>
bool arrList<T>::append(const T value)
{
aList[curLen++]=value;
return true;
}
template <class T>
bool arrList<T>::insert(const int p, const T value) // 在位置p上插入一个元素value,表的长度增1
{
if(curLen>=maxSize)
cout<<"The list is overflow!"<<endl;
if(p<0||p>=maxSize)
cout<<"The position is illegal!"<<endl;
for(int i=curLen-1;i>=p;i--)
aList[i+1]=aList[i];
curLen++;
aList[p]=value;
return true;
}
template <class T>
bool arrList<T>::delete1(const int p) // 删除位置p上的元素,表的长度减 1
{
if(curLen>=maxSize)
cout<<"The list is overflow!"<<endl;
if(p<0||p>=maxSize)
cout<<"The position is illegal!"<<endl;
for(int i=p;i<curLen-1;i++)
aList[i]=aList[i+1];
curLen--;
return true;
}
template <class T> bool arrList<T>::setValue(const int p, const T value) // 用value修改位置p的元素值
{
if(curLen>=maxSize)
cout<<"The list is overflow!"<<endl;
if(p<0||p>maxSize)
cout<<"The position is illegal!"<<endl;
aList[p]=value;
return true;
}
template <class T>
bool arrList<T>::getValue(const int p, T& value) // 把位置p的元素值返回到变量value中
{
if(curLen>=maxSize)
cout<<"The list is overflow!"<<endl;
if(p<0||p>maxSize)
cout<<"The position is illegal!"<<endl;
value=aList[p];
return true;
}
template <class T>
bool arrList<T>::getPos(int & p,const T value) // 查找值为value的元素,并返回第1次出现的位置
{
if(curLen>=maxSize)
cout<<"The list is overflow!"<<endl;
for(int i=0;i<curLen;i++)
{
if(aList[i]==value)
{
p=i;
break;
}
}
return true;
}
template <class T>
void arrList<T>::output() //输出顺序表函数
{
for(int i=0;i<length();i++)
{
T value;
getValue(i,value);
cout<<value<<" ";
}
cout<<endl;
}
//------------------------------------main fun.cpp-------------------------------------
#include "iostream"
#include "ClassArrList.h"
using namespace std;
int main()
{
cout<<"----------------初始化顺序表-----------------"<<endl;
arrList <int> list(13);
int input;
int i;
cout<<"请输入6个数字:";
for(i=0;i<6;i++)
{
cin>>input;
list.append(input);
}
list.output();
cout<<"--------------初始的顺序表长度---------------"<<endl;
cout<<"length of arrList:"<<list.length()<<endl;
cout<<"-----------在第三位上插入一个数据------------"<<endl;
list.insert(3,7);
list.output();
cout<<"-------------删除第三位上的数据--------------"<<endl;
list.delete1(3);
list.output();
cout<<"-------------修改第三为上的数据--------------"<<endl;
list.setValue(3,7);
list.output();
cout<<"------------返回第三为上的数据值-------------"<<endl;
int value;
list.getValue(3,value);
cout<<"The third value of the list is "<<value<<endl;
cout<<"-----输出执行了查找第一次位置操作的结果------"<<endl;
int p;
list.getPos(p,7);
cout<<"7 is first appeared in list is the position "<<p<<endl;
cout<<"----------------调用清空函数-----------------"<<endl;
list.clear();
cout<<"the longth of list is "<<list.length()<<endl;
cout<<"----------------调用析构函数-----------------"<<endl;
return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.