合并有序单链表并排序(遍历一次)

本文介绍了一种链表合并算法的实现方法,该算法能够将两个已排序的链表合并成一个新的有序链表。通过定义结构体来表示链表节点,并使用指针进行操作,实现了链表的初始化、元素插入及合并等功能。此外,还提供了一个测试函数用于验证合并算法的正确性。
LinkList MergeLinkList(LinkList L1, LinkList L2)
{
    LinkList PnewHead = NULL;
    LinkList PL1 = L1;
    LinkList PL2 = L2;
    LinkList PtailNode = NULL;
    if (L1 == NULL)
        return L2;
    if (L2 == NULL)
        return L1;
    if (PL1->data<= PL2->data)
    {
        PnewHead = PL1;
        PtailNode = PL1;
        PL1 = PL1->next;
    }
    else
    {
        PnewHead = PL2;
        PtailNode = PL2;
        PL2 = PL2->next;
    }
    while (PL1&&PL2){

        if (PL1->data < PL2->data)
        {
            PtailNode->next = PL1;
            PL1 = PL1->next;
        }
        else
        {
            PtailNode->next= PL2;
            PL2 = PL2->next;
        }
        PtailNode = PtailNode->next;
    }
    if (PL1 == NULL)
        PtailNode->next = PL2;

    if (PL2 == NULL)
        PtailNode->next = PL1;
    return PnewHead;
}

//测试函数
test,c
#define _CRT_SECURE_NO_WARNINGS 1
typedef int DataType;
#define NULL 0
#include<stdio.h>
#include<windows.h>
#include<assert.h>
typedef struct Node
{
    DataType data;
    struct Node*next;
}Node, *LinkList;
void InitList(LinkList *L)
{
    *L = (Node*)malloc(sizeof(Node));
    if (*L == NULL){
        printf("申请内存空间失败");
    }
    (*L)->next = NULL;
    (*L)->data = NULL;
}
LinkList BuyNode(DataType data)
{
    LinkList NewNode = NULL;
    NewNode = (LinkList)malloc(sizeof(Node));
    if (NewNode == NULL){
        printf("为节点创建空间失败");
    }
    NewNode->data = data;
    NewNode->next = NULL;
    return NewNode;
}
void PrintList(LinkList L)
{
    LinkList Cur = L;
    if (L == NULL){
        printf("NULL");
    }
    while (Cur)
    {
        printf("%d--->", Cur->data);
        Cur = Cur->next;
    }
    printf("NULL\n");
}
void PushBack(LinkList* L, DataType data)
{
    assert(L);
    LinkList Cur = *L;
    if (Cur == NULL){
        Cur = BuyNode(data);
    }
    while (Cur->next)
    {
        Cur = Cur->next;
    }
    Cur->next = BuyNode(data);
}

LinkList MergeLinkList(LinkList L1, LinkList L2)
{
    LinkList PnewHead = NULL;
    LinkList PL1 = L1;
    LinkList PL2 = L2;
    LinkList PtailNode = NULL;
    if (L1 == NULL)
        return L2;
    if (L2 == NULL)
        return L1;
    if (PL1->data<= PL2->data)
    {
        PnewHead = PL1;
        PtailNode = PL1;
        PL1 = PL1->next;
    }
    else
    {
        PnewHead = PL2;
        PtailNode = PL2;
        PL2 = PL2->next;
    }
    while (PL1&&PL2){

        if (PL1->data < PL2->data)
        {
            PtailNode->next = PL1;
            PL1 = PL1->next;
        }
        else
        {
            PtailNode->next= PL2;
            PL2 = PL2->next;
        }
        PtailNode = PtailNode->next;
    }
    if (PL1 == NULL)
        PtailNode->next = PL2;

    if (PL2 == NULL)
        PtailNode->next = PL1;
    return PnewHead;
}
void test2()
{
    LinkList M = NULL;
    LinkList L = NULL;
    LinkList S = NULL;
    InitList(&M);
    InitList(&L);
    PushBack(&M, 1);
    PushBack(&M, 3);
    PushBack(&M, 5);
    PushBack(&L, 2);
    PushBack(&L, 4);
    PushBack(&L, 6);
    ///*PopBack(&M);
    //PopFront(&M);
    //Erase(&M, Find(&M,3));
    //Insert(Find(&M,0), 3);
    //printf("%d\n",Size(M));
    //BubbleSortLinkList(M);
    S = MergeLinkList(L, M);
    PrintList(S);
}
int main()
{
    test2();
    system("pause");
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值