头文件xb.h中
const int MaxListSize = 20;
template
class SeqList
{
public:
SeqList(T a[], int n = 0); //构造函数
~SeqList(); //析构函数
int Length(); //求表的长度
SeqList &Insert(int i, T x); //插入
SeqList &Delete(int i); //删除
T GetNode(int i); //得到第I个数
int LocateNode(T x); //找到X的位置
void ShowList(); //输出表
bool IsEmpty(); //判读为空
bool IsFull(); //判断为满
private:
T data[MaxListSize];
int length;
};
源文件xb.cpp中
#include"xb.h"
template
SeqList::SeqList(T a[], int n = 0) //顺序表构造函数
{
length = n;
for (int i = 0; i < length; i++)
{
data[i] = a[i];
}
};
template
SeqList::~SeqList() //析构函数
{
};
template
int SeqList::Length()
{
return length;
}
template
SeqList & SeqList::Insert(int i, T x)
{
if (i<1 || i>length + 1)
{
cout << “非法位置,终止运行!” << endl;
exit(1);
}
if (IsFull())
{
cout << “表满溢出,终止运行!” << endl;
exit(1);
}
for (int j = length - 1; j >= i - 1; j–) //从后面开始,依次向后移动一个位置
{
data[j + 1] = data[j];
}
data[i - 1] = x;
length++;
return *this;
};
template
SeqList & SeqList::Delete(int i)
{
if (i<1 || i>length)
{
cout << “非法位置,终止运行!” << endl;
exit(1);
}
if (IsEmpty())
{
cout << “空表,不能删除!” << endl;
exit(1);
}
for (int j = i - 1; j <= length - 1; j++) //依次向前移动一个位置
{
data[j] = data[j+1];
}
length–;
return *this;
}
template
T SeqList::GetNode(int i)
{
if (i<1 || i>length)
{
cout << “非法位置,终止运行!” << endl;
exit(1);
}
else
{
return data[i - 1];
}
}
template
int SeqList::LocateNode(T x)
{
for (int i = 0; i <= length - 1; i++)
{
if (data[i] = x)
return i + 1;
}
return 0;
}
template
void SeqList::ShowList()
{
for (int i = 0; i <= length - 1; i++)
{
cout << data[i] << " ";
}
cout << endl;
}
template
bool SeqList::IsEmpty()
{
return (length == 0);
}
template
bool SeqList::IsFull()
{
return (length == MaxListSize);
}