线性表的合并

#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');
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值