@李长宁的笔记
实现线性表顺序表的并集
#include
using namespace std;
const int MaxSize=100;
template
class SeqList
{
public :
SeqList(){length=0;}
SeqList(DataType a[],int n);
~SeqList(){};
int Length(){return length;}
void Length_(){cout<<length;}
DataType Get(int i);
int Locate(DataType x);
void Insert(int i,DataType x);
DataType Delete(int i);
void PrintList();
private :
DataType data[MaxSize];
int length;
};
int main()
{
//以下是测试成员函数
int ab[]={1,2,3,4,5,6,7,8};
SeqList sxb1;
SeqList sxb2(ab,8);
sxb2.PrintList();
cout<<endl;
sxb2.Length_();
cout<<endl;
cout <<sxb2.Get(3)<<endl;
cout <<sxb2.Locate(8)<<endl;
sxb2.Insert(3,2);
sxb2.PrintList();
cout <<endl;
cout<<sxb2.Delete(3)<<endl;
sxb2.PrintList();
//正式开始写顺序表的并
cout<<endl;
cout<<endl;
cout<<endl;
int b3[8]={1,2,3,4,5,6,7,9,},b4[5]={4,5,6,7,8};
SeqList <int> sxb3(b3,8);
SeqList <int> sxb4(b4,5);
int i,x;
for(i=1;i<=8;i++)
{
x=sxb3.Get(i);
if(sxb4.Locate(x)==0)
{
sxb4.Insert(1,x);
//sxb4.PrintList();
//cout<<endl;
}
else
{
continue;
}
}
sxb4.PrintList();
}
template
SeqList::SeqList(DataType a[],int n)
{
int i;
if(n>MaxSize)throw “参数非法”;
for(i=0;i<n;i++)
data[i]=a[i];
length=n;
}
template
DataType SeqList::Get(int i)
{
if(i<1&&i>length)throw “查找位置非法”;
else return data[i-1];
}
template
int SeqList::Locate(DataType x)
{
int i;
for(i=0;i<length;i++)
if(data[i]==x)return i+1;
return 0;
}
template
void SeqList::Insert(int i,DataType x)
{
int j;
if(length>=MaxSize)throw"上溢";
if(i<1||i>length+1)throw"位置";
for(j=length;j>=i;j–)
data[j]=data[j-1];
data[i-1]=x;
length++;
}
template
DataType SeqList::Delete(int i)
{
DataType x;
int j;
if(length==0)throw “上溢”;
if(i<1||i>length)throw “位置”;
x=data[i-1];
for(j=i;j<length;j++)
data[j-1]=data[j];
length–;
return x;
}
template
void SeqList::PrintList()
{
int i;
for(i=0;i<length;i++)
cout<<data[i];
}