两个有序链表合并为一个新的有序链表

/*合并到新链表*/
#include<iostream>
#include<cmath> 
#include<algorithm>
using namespace std;
struct node {
    int v;
    struct node*pnext;
};
void input(struct node*phead);
void merge(struct node*phead1, struct node*phead2, struct node*phead3);
void output(struct node*phead);
int main() {
    struct node*phead1 = (struct node*)malloc(sizeof(struct node));
    phead1->pnext = NULL;
    struct node*phead2 = (struct node*)malloc(sizeof(struct node));
    phead2->pnext = NULL;
    struct node*phead3 = (struct node*)malloc(sizeof(struct node));
    phead3->pnext = NULL;
    input(phead1);
    input(phead2); 
    merge(phead1,phead2,phead3);
    output(phead3);
    return 0;
}
void input(struct node*phead) {
    struct node*last = phead;
    int n;
    cout << "节点数:" << endl;
    cin >> n;
    cout << "输入节点:" << endl;
    for (int i = 0; i < n; ++i) {
        struct node*p = (struct node*)malloc(sizeof(node));
        cin >> p->v;
        p->pnext = NULL;
        last->pnext = p;
        last = p;
    }
}
void merge(struct node*phead1, struct node*phead2, struct node*phead3) {
    struct node*p1 = phead1->pnext, *p2 = phead2->pnext, *p3 = phead3;
    while (p1&&p2) {
        struct node*p = (struct node*)malloc(sizeof(struct node));
        if (p1->v < p2->v) {
            p->v = p1->v;
            p->pnext = NULL;
            p1 = p1->pnext;
        }
        else {
            p->v = p2->v;
            p->pnext = NULL;
            p2 = p2->pnext;
        }
        p3->pnext = p;
        p3 = p;
    }
    while (p1) {
        struct node*p = (struct node*)malloc(sizeof(struct node));
        p->v = p1->v;
        p->pnext = NULL;
        p1 = p1->pnext;
        p3->pnext = p;
        p3 = p;
    }
    while (p2) {
        struct node*p = (struct node*)malloc(sizeof(struct node));
        p->v = p2->v;
        p->pnext = NULL;
        p2 = p2->pnext;
        p3->pnext = p;
        p3 = p;
    }
}
void output(struct node*phead) {
    struct node*p = phead->pnext;
    while (p) {
        cout << " " << p->v;
        p = p->pnext;
    }
}
/*在原链表上合并*/
#include<iostream>
#include<cmath> 
#include<algorithm>
using namespace std;
struct node {
    int v;
    struct node* pnext;
};
void input(struct node*head);
void merge(struct node*head1, struct node*head2);
void output(struct node*head);
int main() {
    struct node*phead1 = (struct node*)malloc(sizeof(struct node));
    phead1->pnext = NULL;
    struct node*phead2 = (struct node*)malloc(sizeof(struct node));
    phead2->pnext = NULL;
    input(phead1);
    input(phead2);
    merge(phead1, phead2);
    output(phead1);
    return 0;
}
void input(struct node*phead) {
    int n;
    struct node*last = phead;
    cout << "输入节点数:" << endl;
    cin >> n;
    cout << "输入节点:" << endl;
    for (int i = 0; i < n; ++i) {
        struct node*p= (struct node*)malloc(sizeof(struct node));
        cin >> p->v;
        p->pnext = NULL;
        last->pnext = p;
        last = p;
    }
}

void merge(struct node*phead1, struct node*phead2) {
    struct node*p1 = phead1, *p2 =phead2->pnext;
    while (p1->pnext&&p2) {
        if (p1->pnext->v > p2->v) {
            struct node* tmp = p2->pnext;
            p2->pnext=p1->pnext;
            p1->pnext=p2;
            p1 = p2;
            p2 = tmp;
        }
        else p1 = p1->pnext;
    }
    if (p2) p1->pnext=p2;

}
void output(struct node*phead) {
    cout << "合并结果:";
    struct node*p = phead;
    while (p->pnext) {
        cout << " " << p->pnext->v;
            p = p->pnext;
    }cout <<endl;

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值