题目要求
依值递增有序排列的线性表a,b另辟c,使元素c递增有序排列
代码
#include "pch.h"
#include <iostream>
using namespace std;
#define datatype int
#define maxsize 100
typedef struct {
datatype data[maxsize];
int j=0;
}Seqlist;
void insert(Seqlist *a) {
int t;
scanf("%d", &t);
for ( a->j = 0; a->j < maxsize&&t!=0; a->j++)
{
a->data[a->j] = t;
scanf("%d", &t);
}
a->data[a->j] = '\0';
}
void merge(Seqlist *a,Seqlist *b,Seqlist *c) {
int i=0,t=0;
while (a->data[t]&&b->data[i])
{
if (a->data[t]>b->data[i])
{
c->data[c->j++] = b->data[i++];
}
else
{
c->data[c->j++] = a->data[t++];
}
}
while (b->data[i])
{
c->data[c->j++] = b->data[i++];
}
while (a->data[t])
{
c->data[c->j++] = a->data[t++];
}
c->data[c->j] = '\0';
}
int main() {
Seqlist a, b,c;
insert(&a);
insert(&b);
merge(&a, &b, &c);
printf("%d", c.j);
for (int i = 0; i < c.j; i++)
{
printf("%d", c.data[i]);
}
return 0;
}