顺序链表合并

1.

#include "common.h"
#include "seqlist.h"

void merge(SeqList *LA, SeqList *LB, SeqList *LC)
{
    int i,j,k;
    i=0;
    j=0;
    k=0;
    while(i<=LA->last&&j<=LB->last)
        if(LA->elem[i]<=LB->elem[j])
        {
            LC->elem[k]= LA->elem[i];
            i++;
            k++;
        }
        else
        {
            LC->elem[k]=LB->elem[j];
            j++;
            k++;
        }
        while(i<=LA->last)
        {
            LC->elem[k]=LA->elem[i];
            i++;
            k++;
        }
        while(j<=LB->last)
        {
            LC->elem[k]=LB->elem[j];
            j++;
            k++;
        }
        LC->last=LA->last+LB->last+1;
}

int main()
{
    SeqList la,lb,lc;
    int r;
    int i;

    printf("put in the length of A:");
    scanf("%d",&r);
   
    la.last=r-1;
    printf("put in the number of A:\n");
   
    for(i=0;i<=la.last;i++)
    {
        scanf("%d",&la.elem[i]);
    }
    
    printf("put in the length of B:");
    scanf("%d",&r);
   
    lb.last=r-1;
    printf("put in the number of B:\n");
    
    for(i=0;i<=lb.last;i++)
    {
        scanf("%d",&lb.elem[i]);
    }
    
    
    merge(&la,&lb,&lc);
    printf("the final C:\n");
   
    for(i=0;i<=lc.last;i++)
    {
        printf("%d  ",lc.elem[i]);
    }
    return 1;

}

头文件:

1.1.

common.h

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

#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0


1.2.

seqlist.h

#define ElemType int
#define MAXSIZE 100
typedef struct
{
    ElemType elem[MAXSIZE];
    int last;
}SeqList;

2.

#include "common.h"
#include "seqlist.h"

void merge(SeqList *LA, SeqList *LB, SeqList *LC)
{
    int i,j,k;
    i=0;
    j=0;
    k=0;
    while(i<=LA->last&&j<=LB->last)
        if(LA->elem[i]<=LB->elem[j])
        {
            LC->elem[k]= LA->elem[i];
            i++;
            k++;
        }
        else
        {
            LC->elem[k]=LB->elem[j];
            j++;
            k++;
        }
        while(i<=LA->last)
        {
            LC->elem[k]=LA->elem[i];
            i++;
            k++;
        }
        while(j<=LB->last)
        {
            LC->elem[k]=LB->elem[j];
            j++;
            k++;
        }
        LC->last=LA->last+LB->last+1;
}

int main()
{
    SeqList *la,*lb,*lc;
    int r;
    int i;
    la=(SeqList*)malloc(sizeof(SeqList));
    printf("put in the length of A:");
    scanf("%d",&r);
    la->last=r-1;

    printf("put in the number of A:\n");
    for(i=0;i<=la->last;i++)
    {
        scanf("%d",&la->elem[i]);
    }
    lb=(SeqList*)malloc(sizeof(SeqList));
    printf("put in the length of B:");
    scanf("%d",&r);
    lb->last=r-1;
    printf("put in the number of B:\n");
    for(i=0;i<=lb->last;i++)
    {
        scanf("%d",&lb->elem[i]);
    }
    lc=(SeqList*)malloc(sizeof(SeqList));

    merge(la,lb,lc);
    printf("the final C:\n");
    for(i=0;i<=lc->last;i++)
    {
        printf("%d",lc->elem[i]);
    }
    return 1;
}

头文件同1.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值