• PAT中国大学MOOC-陈越、何钦铭-数据结构-2020夏 函数题 :02-线性结构1 两个有序链表序列的合并


    题目:https://pintia.cn/problem-sets/1268384564738605056/problems/1271415149946912768

    题解:https://blog.csdn.net/wanmeiwushang/article/details/52585327

    题解代码:

    List Merge(List L1, List L2) {        
        List L = (List)malloc(sizeof(struct Node));
        L->Next = NULL;
        List r = L;
        List p = L1->Next;
        List q = L2->Next;
        while (p!=NULL && q!=NULL) {
            if (p->Data < q->Data) {
                r->Next = p;
                p = p->Next;
                r = r->Next;
            }
            else {
                r->Next = q;
                q = q->Next;
                r = r->Next;
            }
        }
        if (p != NULL) r->Next = p;
        if (q != NULL) r->Next = q;
        L1->Next = NULL;
        L2->Next = NULL;
    
        return L;
    }

     完整代码:

    #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 Merge(List L1, List L2) {        
        List L = (List)malloc(sizeof(struct Node));
        L->Next = NULL;
        List r = L;
        List p = L1->Next;
        List q = L2->Next;
        while (p!=NULL && q!=NULL) {
            if (p->Data < q->Data) {
                r->Next = p;
                p = p->Next;
                r = r->Next;
            }
            else {
                r->Next = q;
                q = q->Next;
                r = r->Next;
            }
        }
        if (p != NULL) r->Next = p;
        if (q != NULL) r->Next = q;
        L1->Next = NULL;
        L2->Next = NULL;
    
        return L;
    }
    List Read()
    {
        int len = 0;
        int num = 0;
        PtrToNode h = NULL;
        PtrToNode last = NULL;
    
        scanf("%d", &len);
        if (len == 0)
            return NULL;
    
        h = (PtrToNode)malloc(sizeof(struct Node));//建立头结点
        h->Next = NULL;
        last = h;
        while (len) {
            scanf("%d", &num);
            PtrToNode node = (PtrToNode)malloc(sizeof(struct Node));
            node->Data = num;
            node->Next = NULL;
            last->Next = node;
            last = node;
            len--;
        }
        return h;
    }
    void Print(List L)
    {
        if (L->Next == NULL) {
            printf("NULL
    ");
            return;
        }
        L = L->Next;
        while (L != NULL) {
            printf("%d ", L->Data);
            L = L->Next;
        }
        putchar('
    ');
    }
  • 相关阅读:
    spring mvc ajax请求
    spring mvc 返回页面数据
    spring mvc 参数传递的三种方式
    spring mvc 注解示例
    spring mvc handler的三种方式
    MyBatis 一级、二级缓存
    MyBatis 调用存储过程
    MyBatis动态SQL与模糊查询
    spring mvc 注解入门示例
    MyBatis 一对多关联查询
  • 原文地址:https://www.cnblogs.com/simon-chou/p/13587360.html
Copyright © 2020-2023  润新知