1. 程式人生 > >7-3 兩個有序連結串列序列的合併 (20 分)

7-3 兩個有序連結串列序列的合併 (20 分)

7-3 兩個有序連結串列序列的合併 (20 分)

已知兩個非降序連結串列序列S1與S2,設計函式構造出S1與S2合併後的新的非降序連結串列S3。

輸入格式:

輸入分兩行,分別在每行給出由若干個正整數構成的非降序序列,用−1表示序列的結尾(−1不屬於這個序列)。數字用空格間隔。

輸出格式:

在一行中輸出合併後新的非降序連結串列,數字間用空格分開,結尾不能有多餘空格;若新連結串列為空,輸出NULL

輸入樣例:

1 3 5 -1
2 4 6 8 10 -1

輸出樣例:

1 2 3 4 5 6 8 10

 

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
#define maxn 10000008
#define INF 0x3f3f3f3f
int a[maxn];
int main()
{
	int flag = 0;
	int i = 0,x;
	memset(a,INF,sizeof(a));
	while(~scanf("%d",&x))
	{
		
		if(x==-1)
			flag++;
		else
			a[i++] = x;
		if(flag==2)
			break;
	}
	
	sort(a,a+i);
	int index = 0;
	for(int k = 0; k < i; k ++)
	{
		if(index)
			printf(" %d",a[k]);
		else
		{
			printf("%d",a[k]);
			index = 1;
		}
	}
	if(!index)
		printf("NULL");
		
}