将一个升序链表和一个降序链表合并成为一个有序链表

#include <stdio.h>
typedef int Elem;
typedef struct LinkNode
{
    Elem data;
    struct LinkNode *next;
}LinkNode,*LinkList;
void createLinkList(LinkList L,int arr[],int length){
    //L=(LinkNode *)malloc(sizeof(LinkNode));
   LinkNode *pTail=L;
    int index=0;
    while(index<length){
        LinkNode *pNode=(LinkNode *)malloc (sizeof(LinkNode));
        pNode->data=arr[index];
        pNode->next=NULL;
        pTail->next=pNode;
        pTail=pTail->next;
        index++;
    }

}
void reverseLink(LinkList L)
{
    LinkNode *pNode=L->next;
    L->next=NULL;
    while(pNode!=NULL)
    {
        //printf("%d\n",pNode->data);
        LinkNode *temp=pNode;
        pNode=pNode->next;
        temp->next=L->next;
        L->next=temp;
        //pNode=pNode->next;
    }
}
void combineAB(LinkList La,LinkList Lb)
{
    //int cnt=0;
    if(NULL==La||Lb==NULL)
    {
        return ;
    }
    LinkNode *pLa=La->next;
    LinkNode *pLb=Lb->next;
    free(Lb);
    La->next=NULL;
    LinkNode *pTail=La;
    while(pLa!=NULL&&pLb!=NULL)
    {
        if(pLa->data < pLb->data)
        {
            LinkNode *temp=pLa;
            pLa=pLa->next;
            temp->next=NULL;
            pTail->next=temp;
            //printf("%d\n",pLa->data);
        }
        else
        {
            LinkNode *temp=pLb;
            pLb=pLb->next;
            temp->next=NULL;
            pTail->next=temp;
           // printf("%d\n",pLb->data);
        }
        pTail=pTail->next;
    }
    if(pLa!=NULL)
    {

        pTail->next=pLa;
    }

    if(pLb!=NULL)
    {
        pTail->next=pLb;
    }
     LinkNode *p=La->next;
    while(p!=NULL){
        printf("%d ",p->data);
        p=p->next;
    }

}
void print(LinkList L){
    LinkNode *pNode=L->next;
    while(pNode!=NULL){
        printf("%d ",pNode->data);
        pNode=pNode->next;
    }
}
int main(){
    LinkList La,Lb;
    La=(LinkNode *)malloc(sizeof(LinkNode));
    La->next=NULL;
    Lb=(LinkNode *)malloc(sizeof(LinkNode));
    Lb->next=NULL;
    int arrA[100]={0};
    int arrB[100]={0};
    int a_length=0,b_length=0;
    int enterData=9999;
    while(scanf("%d",&enterData)!=EOF){
        if(enterData==9999){
            break;
        }
        else{
            arrA[a_length]=enterData;
        a_length++;
        }
    }
    while(scanf("%d",&enterData)!=EOF){
        if(enterData==9999){
            break;
        }
        else{
            arrB[b_length]=enterData;
          b_length++;
        }
    }
    createLinkList(La,arrA,a_length);
    createLinkList(Lb,arrB,b_length);
    //print(La);
   // printf("\n");
    //print(Lb);
   // printf("\n");
    reverseLink(Lb);
   // print(Lb);
    combineAB(La,Lb);
    //print(Lab);
}

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值