1.
#include "common.h"
#include "seqlist.h"
void merge(SeqList *LA, SeqList *LB, SeqList *LC)
{
int i,j,k;
i=0;
j=0;
k=0;
while(i<=LA->last&&j<=LB->last)
if(LA->elem[i]<=LB->elem[j])
{
LC->elem[k]= LA->elem[i];
i++;
k++;
}
else
{
LC->elem[k]=LB->elem[j];
j++;
k++;
}
while(i<=LA->last)
{
LC->elem[k]=LA->elem[i];
i++;
k++;
}
while(j<=LB->last)
{
LC->elem[k]=LB->elem[j];
j++;
k++;
}
LC->last=LA->last+LB->last+1;
}
int main()
{
SeqList la,lb,lc;
int r;
int i;
printf("put in the length of A:");
scanf("%d",&r);
la.last=r-1;
printf("put in the number of A:\n");
for(i=0;i<=la.last;i++)
{
scanf("%d",&la.elem[i]);
}
printf("put in the length of B:");
scanf("%d",&r);
lb.last=r-1;
printf("put in the number of B:\n");
for(i=0;i<=lb.last;i++)
{
scanf("%d",&lb.elem[i]);
}
merge(&la,&lb,&lc);
printf("the final C:\n");
for(i=0;i<=lc.last;i++)
{
printf("%d ",lc.elem[i]);
}
return 1;
}
头文件:
1.1.
common.h
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
1.2.
seqlist.h
#define ElemType int
#define MAXSIZE 100
typedef struct
{
ElemType elem[MAXSIZE];
int last;
}SeqList;
2.
#include "common.h"
#include "seqlist.h"
void merge(SeqList *LA, SeqList *LB, SeqList *LC)
{
int i,j,k;
i=0;
j=0;
k=0;
while(i<=LA->last&&j<=LB->last)
if(LA->elem[i]<=LB->elem[j])
{
LC->elem[k]= LA->elem[i];
i++;
k++;
}
else
{
LC->elem[k]=LB->elem[j];
j++;
k++;
}
while(i<=LA->last)
{
LC->elem[k]=LA->elem[i];
i++;
k++;
}
while(j<=LB->last)
{
LC->elem[k]=LB->elem[j];
j++;
k++;
}
LC->last=LA->last+LB->last+1;
}
int main()
{
SeqList *la,*lb,*lc;
int r;
int i;
la=(SeqList*)malloc(sizeof(SeqList));
printf("put in the length of A:");
scanf("%d",&r);
la->last=r-1;
printf("put in the number of A:\n");
for(i=0;i<=la->last;i++)
{
scanf("%d",&la->elem[i]);
}
lb=(SeqList*)malloc(sizeof(SeqList));
printf("put in the length of B:");
scanf("%d",&r);
lb->last=r-1;
printf("put in the number of B:\n");
for(i=0;i<=lb->last;i++)
{
scanf("%d",&lb->elem[i]);
}
lc=(SeqList*)malloc(sizeof(SeqList));
merge(la,lb,lc);
printf("the final C:\n");
for(i=0;i<=lc->last;i++)
{
printf("%d",lc->elem[i]);
}
return 1;
}
头文件同1.