使用 动态数组 解决 非递减数组 归并问题

本文介绍了如何使用C语言的动态数组来解决非递减数组的归并问题,通过代码示例展示了具体实现过程,同时探讨了内存优化的策略。

文章目录

题目

已知线性表La和Lb中的数据元素按值非递减(非递减就是递增)有序排列,
现要求将La和Lb归并为一个新的线性表Lc,且Lc中的数据元素仍按值非
递减有序排列。
示例输入:La = {3, 5, 8, 11};
		 Lb = {2, 6, 8, 9, 11, 15, 20};
示例输出: Lc = {2, 3, 5, 6, 8, 8, 9, 11, 11, 15, 20};

代码

/**
 * @author: Simon_z
    已知线性表La和Lb中的数据元素按值非递减(非递减就是递增)有序排列,现要求将La和Lb归并为
    一个新的线性表Lc,且Lc中的数据元素仍按值非递减有序排列。
    示例输入:La = {3, 5, 8, 11};
              Lb = {2, 6, 8, 9, 11, 15, 20};
    示例输出: Lc = {2, 3, 5, 6, 8, 8, 9, 11, 11, 15, 20};
 */
//  使用动态数组分配La,Lb内存,需输入La, Lb的length

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;

int *CreateDynamicArray(int *p, int arrayLength);
int Compare(int La_array[], int Lb_array[], int La_value, int Lb_value);

int main(int argv,char *argc[]){
    int *La, *Lb;
    int La_Length, Lb_Length, Lc_Length;
    bool Perform;
    //构造数组La,Lb, Lc
    printf("Please input La_Length: ");
    scanf("%d", &La_Length);
    printf("Please input La_array: ");
    La = CreateDynamicArray(La, La_Length);
    printf("Please input Lb_Length: ");
    scanf("%d", &Lb_Length);
    printf("Please input Lb_array: ");
    Lb = CreateDynamicArray(Lb, Lb_Length);
    //构造数组Lc不需要手动输入数据,所以直接在main方法里构造即可,不需要调用
    //*CreateDynamicArray方法
    Lc_Length = La_Length + Lb_Length; //由于不需要去重所以Lc的长度即是La_Length + Lb_Length
    int Lc[Lc_Length] = {0};
    printf("初始化Lc\n");
    for(int i = 0; i < Lc_Length; i++){
        printf("%d ", Lc[i]);
    }
    if(!Lc){
        printf("Array creation failed!\n");
    }
    //输出插入前的La,Lb
    printf("Brfore Merge: \n");
    printf("La: ");
    for (int i = 0; i < La_Length; i++){
        printf("%d ", La[i]);
    }
    printf("\n");
    printf("Lb: ");
    for (int i = 0; i < Lb_Length; i++){
        printf("%d ", Lb[i]);
    }
    //执行归并算法
    printf("\n");
    printf("After Merge: \n");

    int La_Location = 0, Lb_Location = 0, Lc_Location = 0;  //给La, Lb, Lc添加位置指针
    while((La_Location <= La_Length) && (Lb_Location <= Lb_Length)){
        printf("La_Location:%d La_Length:%d Lb_Location:%d Lb_Length:%d\n", La_Location, La_Length, Lb_Location, Lb_Length);
        //判断Compare方法的返回值,返回true插入Lb的元素,返回false插入La的元素
        Perform = Compare(La, Lb, La_Location, Lb_Location);

        for(int i = 0; i < Lc_Length; i++){
            printf("%d ", Lc[i]);
        }
        printf("\n");
        if (Perform == true){
            Lc[Lc_Location] = Lb[Lb_Location];
            /*指针下移*/
            Lc_Location += 1;
            Lb_Location += 1;
        }else if (Perform == false){
            Lc[Lc_Location] = La[La_Location];
            /*指针下移*/
            Lc_Location += 1;
            La_Location += 1;
        }
    }
    /*将剩余不用比较的数据插入Lc*/
    printf("La_Location:%d La_Length:%d", La_Location, La_Length);
    if (La_Location > La_Length){
        int i,j;
        /*由于while里主动将Lc指针下移, 所以Lc_Location - 1*/
        for (i = Lb_Location, j = Lc_Location - 1; j < Lc_Length; i++, j++){
            Lc[j] = Lb[i];
        }

    }else{
        int i,j;
        /*由于while里主动将Lc指针下移, 所以Lc_Location - 1*/
        for (i = La_Location, j = Lc_Location - 1; j < Lc_Length; i++, j++){
            Lc[j] = La[i];
        }
    }

    //输出插入后的Lc
    printf("\nLc: ");
    for (int i = 0; i < Lc_Length; i++){
        printf("%d ", Lc[i]);
    }

    /*释放内存*/
    free(La);
    free(Lb);
    free(Lc);
    return 0;
}

//返回头指针即可
int *CreateDynamicArray(int *p, int arrayLength){
    int c;
    p = (int *)malloc(arrayLength*sizeof(int)); //在堆中开辟数组内存
    for(int i = 0;i < arrayLength; i++)
    {
        scanf("%d", &c);
        p[i] = c;
    }
    return p;
}

//比较La和Lb的对应元素决定插入哪个数组的元素
int Compare(int La_array[], int Lb_array[], int La_value, int Lb_value){
    bool Judge = true;
    if (La_array[La_value] > Lb_array[Lb_value]){
        Judge = true;
    }else{
        Judge = false;
    }
    return Judge;
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值