|
楼主 |
发表于 2020-5-18 17:38:32
|
显示全部楼层
我已经写出了个大概,但好像还有一些细节问题有遗漏,一起来找找吧!
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100
#define OK 1
#define OVERFLOW -1
#define ERROR -2
typedef int ElemType;
typedef int Status;
typedef struct {
ElemType *elem; // 顺序表数组的基址
int length; // 顺序表当前元素个数
} SqList;
Status initList(SqList &L);
Status listInsert(SqList &L,int i ,ElemType e);
void listUnion(SqList &La,SqList Lb);
void sortList(SqList &L);
void showList(SqList L);
int main(void) {
SqList La,Lb;
int n,i;
ElemType t;
if(initList(La)!=OK) exit(0);
if(initList(Lb)!=OK) exit(0);
scanf("%d",&n);
for(i=1; i<=n; i++) {
scanf("%d",&t);
if(listInsert(La,i,t)!=OK) exit(0);
}
scanf("%d",&n);
for(i=1; i<=n; i++) {
scanf("%d",&t);
if(listInsert(Lb,i,t)!=OK) exit(0);
}
listUnion(La,Lb);
sortList(La);
showList(La);
return 0;
}
Status initList(SqList &L) {
L.elem=(ElemType*)malloc(MAXSIZE*sizeof(ElemType));
if(!L.elem) exit(OVERFLOW);
L.length=0;
return OK;
}
int getLength(SqList L) {
return L.length;
}
Status listInsert(SqList &L,int i ,ElemType e) {
if(L.length==MAXSIZE||i<1||i>L.length+1)
return ERROR;
if(i<=L.length)
{
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 getElem(SqList L,int i,ElemType &e)
{
if(L.length==0||i<1||i>L.length)
return ERROR;
e=L.elem[i-1];
return OK;
}
int locateElem(SqList L,ElemType e)
{
int i;
for(i=0;i<=L.length-1;i++)
{
if(e==L.elem[i])
return i;
}
if(i==L.length)
return 0;
}
void listUnion(SqList &La,SqList Lb){
int i;
ElemType e;
for(i=0;i<=Lb.length-1;i++)
{
if(getElem(Lb,i,e)==OK)
{
if(!locateElem(La,e))
{
listInsert(La,La.length+1,e);
}
}
}
}
void sortList(SqList &L){
int l,empty;
l=getLength(L);
for(int i=1;i<l;i++)
{
for(int j=0;j<l-i;j++)
{
if(L.elem[j]>L.elem[j+1])
{
empty=L.elem[j];
L.elem[j]=L.elem[j+1];
L.elem[j+1]=empty;
}
}
}
}
void showList(SqList L){
int l;
l=getLength(L);
for(int i=0;i<=l-1;i++)
{
printf("%d ",L.elem[i]);
}
} |
|