安县哪些地方在招工:我的这个顺序表程序怎么实现不了交并运算呢???急!!!

来源:百度文库 编辑:查人人中国名人网 时间:2024/05/09 04:40:53
#include <iostream>
using namespace std;
const DefaultSize=20;
template< class Type> class SeqList
{
public:
SeqList(int MaxSize=DefaultSize);
~SeqList() {delete []data; }
int Length() const {return last+1;}
int Find(Type& x) const;

int Insert(Type x ,int i);
int Remove(Type x);

int IsEmpty() {return last==-1;}
int IsFull() {return last==MaxSize-1;}
Type Get(int i) {return i<0||i>last?NULL:data[i];}
void Union(SeqList<Type>& LA,SeqList<Type>& LB);
void Intersection(SeqList<Type>& LA,SeqList<Type>& LB);
void PrintList();
void InputList();
private:
Type* data;
int MaxSize;
int last;
};

template <class Type> SeqList<Type>::SeqList(int sz)
{

if(sz>0)
{
MaxSize=sz;
last=sz-1;
data=new Type[MaxSize];
}
}
template <class Type> int SeqList<Type>::Find(Type& x) const
{

int i=0;
while(i<=last&&data[i]!=x)
i++;
if(i>last)
return -1;
else
return i;
}

template <class Type> int SeqList<Type>::Insert(Type x,int i)
{

if(i<0||i>last+1||last==MaxSize-1) return 0;
else
{
last++;
for(int j=last;j>i;j--)
data[j]=data[j-1];
data[i]=x;
return 1;
}
}
template <class Type> int SeqList<Type>::Remove(Type x)
{
int i=Find(x);
if(i>=0)
{
last--;
for(int j=i;j<=last;j++)
data[j]=data[j+1];
return 1;
}
return 0;
}

template <class Type> void SeqList<Type>::Union(SeqList<Type>& LA,SeqList<Type>& LB)
{

int n=LA.Length();
int m=LB.Length();
for(int i=0;i<m;i++)
{
Type x=LB.Get(i);
int k=LA.Find(x);
if(k==-1)
{
LA.Insert(n+1,x);
n++;
}
}
}
template <class Type> void SeqList<Type>::Intersection(SeqList<Type> &LA,SeqList<Type>& LB)
{

int n=LA.Length();
int m=LB.Length();
int i=0;
while(i<n)
{
Type x=LA.Get(i);
int k=LB.Find(x);
if(-1==k)
{
LA.Remove(x);
n--;
}
else
i++;
}
}
template <class Type> void SeqList<Type>::PrintList()
{

int l=Length()-1;
for(int i=0;i<l;i++)
cout<<"\tData["<<i<<"]:"<<data[i]<<endl;
}
template <class Type> void SeqList<Type>::InputList()
{

int l=Length()-1;
for(int i=0;i<l;i++)
{
cout<<"\tPlease enter data["<<i<<"]:";
cin>>data[i];
//cout<<endl;
}
}

void main()
{
SeqList<int> Test1(5);
SeqList<int> Test2(6);
SeqList<int> Test3(11);
SeqList<int> Test4(11);

cout<<"Test1.length:"<<Test1.Length()<<endl;
cout<<"Test2.length:"<<Test2.Length()<<endl;

cout<<"Please input the data of Test1:"<<endl;
Test1.InputList();
cout<<"The data in Test1 is:"<<endl;
Test1.PrintList();
cout<<"Please input the data of Test2:"<<endl;
Test2.InputList();
cout<<"The data in Test2 is:"<<endl;
Test2.PrintList();

Test3.Union(Test1,Test2);
Test3.PrintList();
Test4.Intersection(Test1,Test2);
Test4.PrintList();

}