7-1 两个有序链表序列的合并
发布日期:2021-06-23 09:17:42 浏览次数:0 分类:技术文章
#include <bits/stdc++.h>using namespace std;typedef struct Link_list{
       int data;    struct Link_list *next;}List;void print(List *L) {
   	if(L==NULL) cout<<"NULL";	else{
   		cout<<L->data;		L = L -> next;		while(L) {
   			cout<<" "<<L->data; 			L = L -> next;		}	}}List *create(List *L) {
       L = (List *)malloc(sizeof(List));    L->next = NULL;    List *r = L;    int b;    cin>>b;    while(b!=-1){
           List *p = (List *)malloc(sizeof(List));        p->data = b;        r->next = p;        p->next = NULL;        r = p;        cin>>b;    }    return L;}List *merge_list(List *L1, List *L2) {
       List p;    p.data = 0;    p.next = NULL;    List *result = &p;    while (L1 && L2) {
           if (L1->data < L2->data) {
               result->next = L1;            L1 = L1->next;        } else {
               result->next = L2;            L2 = L2 ->next;        }         result = result -> next;    }    if (L1 == NULL) {
           result ->next = L2;    }         if (L2 == NULL) {
           result ->next = L1;    }    return p.next;}int main(){
       List *L1,*L2;     L1 = create(L1);     L2 = create(L2);    List *result = merge_list(L1->next,L2->next);    print(result);    return 0;}
上一篇:Markdown学习
下一篇:制造业 - Splashtop 助力雅马哈远程支持物联网设备

关于作者

    白红宇是个全栈工程师,前端vue,小程序,app开发到后端框架设计,数据库设计,环境部署上线运维。

最新文章