6-6 Merge of two ordered linked list sequences (15 points)

This question requires the implementation of a function that combines the increasing integer sequences represented by two linked lists into a non-decreasing integer sequence.

Function interface definition:

List Merge( List L1, List L2 );

The List structure is defined as follows:

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

L1 and L2 are given singly linked lists of leading nodes, and the data stored in their nodes are in increasing order; the function Merge merges L1 and L2 into a non-decreasing integer sequence. The nodes in the original sequence should be used directly and the linked list head pointer of the merged head node should be returned.

Sample Referee Test Procedure:

#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;
}

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

Input example:

3
1 3 5
5
2 4 6 8 10

Output sample:

1 2 3 4 5 6 8 10 
NULL
NULL
#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;
}

///读取链表
List Read()
{
    
    
    int n;
    scanf("%d",&n);
    List L = (List)malloc(sizeof(PtrToNode));
    L->Next = NULL;
    if(n){
    
    
        List l = L;
        int i;
        for(i=0; i<n; i++){
    
    
            List p = (List)malloc(sizeof(struct Node));
            scanf("%d",&p->Data);
            l->Next = p;
            l = p;
        }//for
        l->Next = NULL;
    }//if
    return L;
}

///输出链表内容
void Print( List L )
{
    
    
    List p = L->Next;
    if(p){
    
    
        List l;
        l = L;
        while(l->Next){
    
    
            l = l->Next;
            printf("%d ",l->Data);
        }//while
    }else{
    
    
        printf("NULL");
    }
    printf("\n");
}

///合并两个链表
List Merge( List L1, List L2 )
{
    
    
    List head = (List)malloc(sizeof(List));
    List p = head, a = L1->Next, b = L2->Next;
    while(a && b){
    
    
        if(a->Data < b->Data){
    
    
            p->Next = a;
            a = a->Next;
        }else{
    
    
            p->Next = b;
            b = b->Next;
        }//if...else...
        p = p->Next;
    }//while
    p->Next = a ? a : b;
    L1->Next = NULL;
    L2->Next = NULL;
    return head;
}

Guess you like

Origin blog.csdn.net/YSA_SFPSDPGY/article/details/114697532