#include
#include<malloc.h>
#include <stdio.h>
#include <stdlib.h>
#include<process.h>
typedef int ElemType;
typedef int Status;
#define LISTSIZE 100
#define OK 1
#define OVERFLOW -2
#define ERROR 0
//创建结构体
typedef struct{
ElemType *elem;
int length;
int listsize;
}SqList;
//构造线性表
Status InitList(SqList &L)
{
L.elem=(ElemType )malloc(LISTSIZEsizeof(ElemType));
if(!L.elem)
exit(OVERFLOW);//储存分配失败
L.length=0;
L.listsize=LISTSIZE;
return OK;
}
//销毁线性表
void DestoryList(SqList &L)
{
if(L.elem)
free(L.elem);
}
//清空线性表
void ClearList(SqList &L)
{
L.length=0;
}
//求线性表的长度
int GetlengthList(SqList &L)
{
return(L.length);
}
//获取线性表中的元素
int GetElem(SqList &L,int i,ElemType &e)
{
if(i<1||i>L.length)
{
return ERROR;
}
e=L.elem[i-1];
}
//在线性表中插入元素
Status ListInsert(SqList &L,int i,ElemType e)
{
if(i<1||i>L.length+1)
{
return ERROR;
}
for(int j=L.length-1;j>=i-1;j--)
{
L.elem[j+1]=L.elem[j];//元素后移
}
L.elem[i-1]=e;//插入元素
L.length++;
return OK;
}
//删除线性表中的元素
Status ListDelete(SqList &L,int i,ElemType &e)
{
if(i<1||i>L.length)
{
return ERROR;
}
e=L.elem[i-1];
for(int j=i-1;j<=L.length-1;j++)
{
L.elem[j]=L.elem[j+1];//元素前移
}
L.length--;
return OK;
}
//定位顺序表中的元素
int LocateList(SqList L,ElemType e,Status(*compare)(ElemType,ElemType))
{
int i=1,*p;
p=L.elem;
while(i<=L.length&&!(*compare)(*p++,e))
{
i++;
}
if(i<=L.length)
{
return i;
}
else
{
return 0;
}
}
int equal(int e1,int e2)
{
return(e1==e2)?true:false;
}
//将所有在线性表b中但不在a中的元素插入到a中
void UnionList(SqList &La,SqList Lb)
{
int eb,j;
La.length=GetlengthList(La);
Lb.length=GetlengthList(Lb);//求出a,b的长度
for(j=1;j<=Lb.length;j++)
{
GetElem(Lb,j,eb);//获取b中元素
if(!LocateList(La,eb,equal))//如果la不存在e,合并
{
ListInsert(La,++La.length,eb);
}
}
}
//将a表和b表合并为c表
void MergeList(SqList La,SqList Lb,SqList &Lc)
{
int i=0,j=1,k=0;
int ea,eb;
La.length=GetlengthList(La);
Lb.length=GetlengthList(Lb);//获取a,b长度
while( i<=La.length&&j<=Lb.length)
{
GetElem(La,i,ea);//获取a中元素
GetElem(Lb,j,eb);//获取b中元素
if(ea>eb)
{
ListInsert(Lc,k,eb);//将元素插入到c中
k++;
j++;
}
else
{
ListInsert(Lc,k,ea);//将元素插入到c中
k++;
i++;
}
}
while(i<=La.length)
{
GetElem(La,i,ea);//获取a中元素
ListInsert(Lc,k,ea);//将元素插入到c中
k++;
i++;
}
while(j<=Lb.length)
{
GetElem(Lb,j,eb);//获取b中元素
ListInsert(Lc,k,eb);//将元素插入到c中
k++;
j++;
}
}
using namespace std;
int main()
{
int a[4]={3,5,8,11};
int b[7]={2,6,8,9,11,15,20};
SqList La,Lb,Lc;
int ea,eb;
InitList(La);//构建顺序表La
InitList(Lb);//构建顺序表Lb
InitList(Lc);//构建顺序表Lc
for(int i=1;i<=4;i++)
{
ListInsert(La,i,a[i-1]);//给表a赋值
}
for(int j=1;j<=7;j++)
{
ListInsert(Lb,j,b[j-1]);//给表b赋值
}
cout<<"La中的元素为:"<<endl;
for(int i=0;i<La.length;i++)
{
GetElem(La,i+1,ea);
cout<<ea<<endl;
}
cout<<"Lb中的元素为:"<<endl;
for(int j=0;j<Lb.length;j++)
{
GetElem(Lb,j+1,eb);
cout<<eb<<endl;
}
cout<<"表La的长度为:"<<endl;
cout<<GetlengthList(La)<<endl;
cout<<"表Lb的长度为:"<<endl;
cout<<GetlengthList(Lb)<<endl;
//MergeList(La,Lb,Lc);
//cout<<"合并后的表Lc为:"<<endl;
// for(int i=0;i<Lc.length;i++)
// {
// GetElem(Lc,i+1,e);
// cout<<e<<endl;
// }
//cout<<"表Lc的长度为:"<<endl;
//cout<<GetlengthList(Lc)<<endl;
cout<<"合并后La中的元素为:"<<endl;
UnionList(La,Lb);
for(int i=0;i<La.length;i++)
{
GetElem(La,i+1,ea);
cout<<ea<<endl;
}
return 0;
}
合并后La的理想输出应该是LA=(3,5,8,11,2,6,9,15,20)
为什么会插入0和一些乱码呢?
该如何解决?