两个有序链表序列的合并

两个有序链表序列的合并

已知两个非降序链表序列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<map>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<vector>
#include<iomanip>
#include<cstring>
#include<iterator>
#include<iostream>
#include<algorithm>
#define R register
#define LL long long
#define pi 3.141
#define INF 1400000000
using namespace std;

struct Node{                                                       //定义结构体,结构体就是链表的结点
    int number;                                                    //链表中的数字
    Node* next;                                                    //链表中的指针,指向下一个节点
};

inline int read() {                                                //快读,加快输入速度,提升程序运行速度
	int number = 0;
	int f = 1;
	char ch = getchar();
	while (ch < '0' || ch > '9') {
		if (ch == '-') {
			f = -1;
		}
		ch = getchar();
	}
	while (ch >= '0' && ch <= '9') {
		number = number * 10 + ch - '0', ch = getchar();
	}
	return number * f;
}

int main(){
    Node* List1 = (Node*)malloc(sizeof(Node));                      //定义链表一,表示第一串序列
    List1->next = NULL;                                             //链表一的节点赋空
    Node* List2 = (Node*)malloc(sizeof(Node));                      //定义链表二,表示第二串序列
    List2->next = NULL;                                             //链表二的节点赋空
    Node* List3 = (Node*)malloc(sizeof(Node));                      //定义合并后的链表
    List3->next = NULL;                                             //链表三的节点赋空
    Node *point = (Node*)malloc(sizeof(Node));                      
    int number = read();                                            //读入的数据
    point = List1;                                                  //标记第一个链表,创建第一个链表
    while (number != -1) {                                          //读入数据为-1时,停止输入
        Node *temp = (Node*)malloc(sizeof(Node));                   //定义添加的新的节点
        temp->number = number;                                      //对转移节点进行初始化
        temp->next = NULL;
        point->next = temp;                                         //链表中新的节点指向新创建的节点
        //point = temp;                                             //使用memcpy的方式进行复制类似于对象间的拷贝构造函数,两种方式均可
        memcpy(&point, &temp, sizeof(temp));
        number = read();                                            //继续进行数据的读入
    }
    point = List2;                                                  //同时进行输入第二个链表,储存数据的方式和上面个类似
    number = read();
    while (number != -1) {
        Node *temp = (Node*)malloc(sizeof(Node));
        temp->number = number;
        temp->next = NULL;
        point->next = temp;
        point = temp;
        number = read();
    }
    Node *point1, *point2, *point3;                                 //重新定义三个指针,进行对链表合并的操作
    point3 = List3;
    point1 = List1->next, point2 = List2->next;
    while (point1 != NULL && point2 != NULL) {                      //两个链表均非空
        if (point1->number <= point2->number) {                     //取出两个链表当前元素中最小的元素
            point3->next = point1;
            point3 = point1;
            point1 = point1->next;
        }
        else {
            point3->next = point2;
            point3 = point2;
            point2 = point2->next;
        }
    }
    if (point1 != NULL) {                                           //当一个链表为空时,剩余数据直接填入
        point3->next = point1;
        point3 = point1;
        point1 = point1->next;
    }
    if (point2 != NULL) {
        point3->next = point2;
        point3 = point2;
        point2 = point2->next;
    }
    point = List3->next; 
    if (point == NULL) {                                            //链表为空,直接输出NULL
        printf("NULL");
    }
    else {                                                          //链表非空直接数据数据
        bool flag = false;
        while (point != NULL) {
            if (flag == false) {
                flag = !flag;
            }
            else {
                printf(" ");
            }
            printf("%d", point->number);
            point = point->next;
        }
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值