#include<stdio.h>
#include <conio.h>
#include<stdlib.h>
#include<iostream.h>
#define MAXSIZE 100
typedef int ElemType;
typedef struct
{
ElemType *elem;
int length;
}SqList;
SqList LA,LB;
void InitList(SqList &LA,SqList &LB)
{
LA.elem = new ElemType[MAXSIZE];
LB.elem = new ElemType[MAXSIZE];
if((!LA.elem)||(!LB.elem))
{
cerr<<"存储分配错误!"<<endl;
exit(1);
}
LA.length=0;
LB.length=0;
}
int Length(SqList &L)
{
return L.length;
}
int Search(SqList &L,ElemType x)
{
for (int i=1;i<=L.length;i++)
if(L.elem[i-1]==x) return i;
return 0;
}
bool Insert(SqList &L,int i,ElemType &x)
{
if(L.length==MAXSIZE) return false;
if(i<0||i>L.length+1) return false;
for(int j=L.length;j>=i;j--)
L.elem[j]=L.elem[j-1];
L.elem[i-1]=x;
L.length++;
return true;
}
int Exchang(SqList &L)
{
int i,j,t=0;
for(i=0;i<L.length;i++)
{
for(j=i;j>=1;j--)
{
if(L.elem[j-1]>L.elem[j])
{
t=L.elem[j-1];
L.elem[j-1]=L.elem[j];
L.elem[j]=t;
}
}
}
return 0;
}
/*void Merge(SqList &LA,SqList &LB)
{合并线性表LA与LB,结果存于LA,重复元素只留一个
int n = Length(LA),m = Length(LB);
int i,k,x;
for (i=1;i<=m;i++)
{
x=LB.elem[i-1];
k=Search(LA,x);
if(k==0)
{
Insert(LA,n+1,x);
n++;
}
}
}*/
void Merge(SqList &LA,SqList &LB)
{
int n = Length(LA),m = Length(LB);
int i,x;
for (i=1;i<=m;i++)
{
x=LB.elem[i-1];
Insert(LA,n+1,x);
n++;
}
}
void main()
{
char ch;
int a,b;
SqList LA,LB;
InitList(LA,LB);
do{
cout<<"请输入LA的长度:";
cin>>a;
cout<<"请输入LA的元素:";
for(int j=0;j<a;j++)
cin>>LA.elem[j];
cout<<"请输入LB的长度:";
cin>>b;
cout<<"请输入LB的元素:";
for(int k=0;k<b;k++)
cin>>LB.elem[k];
LA.length=a;
LB.length=b;
Merge(LA,LB);
Exchang(LA);
int n=Length(LA);
for(int i=0;i<n;i++)
cout<<LA.elem[i]<<endl;
printf("是否继续?[y/n][y]");
while((ch=getchar())!='\n');
}while((ch=getchar())=='y'||ch=='\n');
}
线性表的合并
最新推荐文章于 2022-06-06 23:45:00 发布