#include
using namespace std;
template
class CSequenceList
{
public:
CSequenceList(int size);
~CSequenceList(void);
//判断线性表是否为空
bool IsEmpty()const;
//返回线性表的长度
int Length()const;
//得到第i元素的值
bool Get(int i, T& x)const;
//找到跟x相等的下标,失败返回-1
int Find(const T x)const;
//在第i个数据之后插入x,若i=-1,那么在表头之前插入
bool Insert(int i, T x);
//删除第i个数据
bool Delete(int i);
//更新数据
bool Update(int i,T x);
//输出表
void Output(ostream& out)const;
//排序,从小到大排序
void sort();
private:
int m_number;
int maxLength;
T* mElements;
};
template
CSequenceList
{
maxLength=size;
mElements=new T[maxLength];
m_number=0;
}
template
CSequenceList
{
delete [] mElements;
}
template
bool CSequenceList
{
return m_number==0;
}
template
int CSequenceList
{
return m_number;
}
template
bool CSequenceList
{
if (i <0 || i>m_number-1)
{
cout<<"Get() is error!"<
}
}
template
int CSequenceList
{
for (int i=0; i
if(x==mElements[i])return i;
}
return -1;
}
template
bool CSequenceList
{
if (i<-1 || i>m_number-1 || m_number == maxLength)
{
cout<<"Insert() is Error!";
return false;
}
for (int j=m_number-1;j>i;j--)
{
mElements[j+1]=mElements[j];
}
mElements[i+1]=x;
m_number++;
return true;
}
template
bool CSequenceList
{
if (i<0 || i>m_number-1 || m_number==0)
{
cout<<"Delete() is Error!";
return false;
}
for (int j=i; j
mElements[j]=mElements[j+1];
}
m_number--;
return true;
}
template
bool CSequenceList
{
if (i<0 || i>m_number-1)
{
cout<<"Update() is Error!";
return false;
}
mElements[i]=x;
return true;
}
template
void CSequenceList
{
cout<
out<
out<
template
void CSequenceList
{
if (m_number == 0)
{
return;
}
for (int i=0;i
for (int j=i+1;j
if (mElements[i] > mElements[j])
{
T temp=mElements[j];
mElements[j]=mElements[i];
mElements[i]=temp;
}
}
}
}
int main()
{
CSequenceList
for (int i=0;i<10;i++)
{
SL.Insert(i-1,i);
}
//初始化数据
cout<<"-----初始化数据-------"<
cout<
cout<<"-----在第3个后面添加一个数据99-------"<
SL.Output(cout);
cout<
cout<<"-----删除第8个数据-------"<
SL.Output(cout);
cout<
cout<<"-----改掉第5个数据-------"<
SL.Output(cout);
cout<
SL.Output(cout);
cout<
}
就用模版写了一下第一问,希望你能够用的上,看的懂。
我有“线性表”的源码而已(自己写的)。