實驗項目 2-11. 兩個有序鏈表序列的合併(15)

2-11. 兩個有序鏈表序列的合併(15)

時間限制
500 ms
內存限制
80000 kB
代碼長度限制
8000 B
判題程序
Standard

已知兩個非降序鏈表序列S1與S2,設計函數構造出S1與S2的並集新非降序鏈表S3。

輸入格式說明:

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

輸出格式說明:

在一行中輸出合併後新的非降序鏈表,數字間用空格分開,結尾不能有多餘空格;若新鏈表爲空,輸出“NULL”。

樣例輸入與輸出:

序號 輸入 輸出
1
1 3 5 -1
2 4 6 8 10 -1
1 2 3 4 5 6 8 10
2
1 2 3 4 5 -1
1 2 3 4 5 -1
1 1 2 2 3 3 4 4 5 5
3
-1
-1
NULL

鏈表標準代碼:

#include <stdio.h>
#include <stdlib.h>
#define LEN (struct node*)malloc(sizeof(struct node))

struct node
{
	int num;
	struct node *next;
};
node *creat()
{
	node *t,*p,*head;
	int n,i=0;
	p=LEN;
	t=p;
	scanf("%d",&p->num);
	head=NULL;
	p->next=NULL;
	while(p->num!=-1){
		if(!i){
			head=p;
			i=1;
		}
		else
			t->next=p;
		t=p;
		p=LEN;
		scanf("%d",&p->num);
		p->next=NULL;
	}
	return head;
}
node *he(node *head1,node *head2)
{
	node *head,*t,*p1,*p2,*p,*w;
	int flag=0;
	head=NULL;
	t=head;
	p1=head1;
	p2=head2;
	while(p1&&p2){
		p=LEN;
		if(p1->num > p2->num)
        {
            p->num=p2->num;
            p->next=NULL;
            p2=p2->next;
            if(!flag){
                head=p;
                flag=1;
            }
            else
                t->next=p;
            t=p;
        }
		else if(p1->num < p2->num)
        {
            p->num=p1->num;
            p->next=NULL;
            p1=p1->next;
            if(!flag){
                head=p;
                flag=1;
            }
            else
                t->next=p;
            t=p;
        }
		else{
			w=LEN;
			p->num=w->num=p1->num;
			p->next=w->next=NULL;
			if(!flag){
				head=p;
				flag=1;
			}
			else
				t->next=p;
			t=p;
			t->next=w;
			t=w;
			p1=p1->next;
			p2=p2->next;
		}
	}
	if(p1)
    {
        while(p1)
        {
            p=LEN;
            p->num=p1->num;
            p->next=NULL;
            p1=p1->next;
            if(!flag){
                head=p;
                flag=1;
            }
            else
                t->next=p;
            t=p;
        }
    }
    else if(p2)
    {
        while(p2)
        {
			
			p=LEN;
			p->num=p2->num;
            p->next=NULL;
            p2=p2->next;
            if(!flag){
                head=p;
                flag=1;
            }
            else
                t->next=p;
            t=p;
        }
    }
	return head;
}

void print(node *head)
{
	struct node *p;
	p=head;
	int i=0;
	while(p!=NULL){
		if(i)
			printf(" ");
		i=1;
		printf("%d",p->num);
		p=p->next;
	}
	if(!i)
		printf("NULL");
	printf("\n");
}

int main()
{
	node *head1,*head2;
	head1=creat();
	head2=creat();
	head1=he(head1,head2);
	print(head1);
	return 0;
}

vector解法:

#include <stdio.h>
#include <algorithm>
#include <vector>
using namespace std;
vector < int > v;
int main()
{
	int i,j=0,b;
	for(i=0;i<2;i++){
		for(;;){
			scanf("%d",&b);
			if(b==-1)
				break;
			v.push_back(b);
		}
	}
	sort(v.begin(),v.end());
	j=v.size();
	if(!j)
		printf("NULL\n");
	else{
		printf("%d",v[0]);
		for(i=1;i<j;i++)
			printf(" %d",v[i]);
		printf("\n");
	}
	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章