数据结构-两个有序链表序列的合并

数据结构-两个有序链表序列的合并

本题要求实现一个函数,将两个链表表示的递增整数序列合并为一个非递减的整数序列。

函数接口定义:

List Merge( List L1, List L2 );

其中List结构定义如下:

typedef struct Node *PtrToNode;
struct Node {
    ElementType Data; /* 存储结点数据 */
    PtrToNode   Next; /* 指向下一个结点的指针 */
};
typedef PtrToNode List; /* 定义单链表类型 */

L1L2是给定的带头结点的单链表,其结点存储的数据是递增有序的;函数Merge要将L1L2合并为一个非递减的整数序列。应直接使用原序列中的结点,返回归并后的带头结点的链表头指针。

裁判测试程序样例:

#include <stdio.h>
#include <stdlib.h>

typedef int ElementType;
typedef struct Node *PtrToNode;
struct Node {
    ElementType Data;
    PtrToNode   Next;
};
typedef PtrToNode List;

List Read(); /* 细节在此不表 */
void Print( List L ); /* 细节在此不表;空链表将输出NULL */

List Merge( List L1, List L2 );

int main()
{
    List L1, L2, L;
    L1 = Read();
    L2 = Read();
    L = Merge(L1, L2);
    Print(L);
    Print(L1);
    Print(L2);
    return 0;
}

/* 你的代码将被嵌在这里 */

输入样例:

3
1 3 5
5
2 4 6 8 10

输出样例:

1 2 3 4 5 6 8 10 
NULL
NULL

结果:

Submit TimeStatusScoreProblemCompilerRun TimeUser
4/2/2019, 20:00:00

Accepted

1502-线性结构1C (gcc)1 msBrianLi
CaseHintResultRun TimeMemory
0同sample,交错归并Accepted1 ms256 KB
1两个完全一样的链表Accepted1 ms384 KB
2两个空链表Accepted1 ms256 KB
3L2完全贴在L1后面Accepted1 ms256 KB
4L1完全贴在L2后面Accepted1 ms356 KB
#include <stdio.h>
#include <stdlib.h>

typedef int ElementType;
typedef struct Node *PtrToNode;
struct Node {
    ElementType Data;
    PtrToNode   Next;
};
typedef PtrToNode List;

List Read(int seq[], int N); /* 细节在此不表 */
void Print( List L ); /* 细节在此不表;空链表将输出NULL */

List Merge( List L1, List L2 );

int main()
{
    List L1, L2, L;
    int L1_seq[3] = {1,3,5};
    int L2_seq[5] = {2,4,6,8,10};
    //创建链条
    L1 = Read(L1_seq, 3);
    L2 = Read(L2_seq, 5);
    //合并链条
    L = Merge(L1, L2);
    Print(L);
    Print(L1);
    Print(L2);
    return 0;
}

void Print( List L )
{
    List temp = L->Next;
    while(temp != NULL)
    {
        printf(" %d", temp->Data);
        temp = temp->Next;
    }
    printf("\n");
}

List Read(int seq[], int N)
{
    List L;
    List temp, tempL;

    //创建头结点
    L = (List)malloc(sizeof(struct Node));
    L->Next = NULL;
    tempL = L;

    for(int i=0; i<N; i++)
    {
        temp = (List)malloc(sizeof(struct Node));
        temp->Data = seq[i];
        tempL->Next = temp;
        tempL = tempL->Next;
    }

    return L;
}

List Merge(List L1,List L2)
{
    List L;
    List tempL, tempL1, tempL2;
    
    //创建一个新的链表头结点L
    L = (List)malloc(sizeof(struct Node));
    L->Next = NULL;
    
    //tempL指针用以指向当前操作链表结点
    tempL = L;
    tempL1 = L1->Next;
    tempL2 = L2->Next;

    while(tempL1 && tempL2)
    {
        if(tempL1->Data > tempL2->Data)
        {
            //链表指针向后指定
            tempL->Next = tempL2;
            //新的尾结点
            tempL = tempL2;
            tempL2 = tempL2->Next;
        }
        else
        {
            tempL->Next = tempL1;
            tempL = tempL1;
            tempL1 = tempL1->Next;
        }
    }
    
    //若L1的结点还没遍历完,则将剩余结点直接接到L链表的尾的结点的Next指针
    if(tempL1)
    {
        tempL->Next = tempL1;
    }
    if(tempL2)
    {
        tempL->Next = tempL2;
    }
    //无效化L1,L2链条(十分重要)相当于free(),释放链表
    L1->Next = NULL;
    L2 ->Next = NULL;
    tempL = NULL;

    return L;

}

 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值