链表的有序合并

时间:2022-04-17 14:37:13

 

 
#include <stdio.h>
#include <stdarg.h>
#include <assert.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
 
typedef struct node
{
    int iData;
    struct node* next;
 
}Node;
 
typedef struct list
{
    int ilen;
    Node* pFrist;
 
}List;
 
//创建节点
 
Node* mknode()
{
 
    Node* node =(Node*)malloc(sizeof(Node));
    if(NULL == node)
    {
        fprintf(stdout,"malloc error\n");
        exit(EXIT_FAILURE);
    }
    memset(node,0,sizeof(Node));
    return node;
}
//创建头节点
List* mklist()
{
   List* list = (List*)malloc(sizeof(List));
   if(NULL == list)
   {
       fprintf(stdout,"malloc list error\n");
       exit(EXIT_FAILURE);
 
   }
   memset(list,0,sizeof(List));
   return list;
 
}
 
 
//插入数据
void insert(List *list,int data)
{
    if(NULL == list)
    {
 
        return;
    }
    Node* node = mknode();
    node->iData = data;
    if(NULL == list->pFrist)
    {
        list->pFrist = node;
    }
    else
    {
        Node *pre = list->pFrist;
        Node *cur = pre;
        while(cur!=NULL)
        {
           if(data < cur->iData)
           {
               if(cur == list->pFrist)
               {
                   node->next=cur;
                   list->pFrist=node;


               }else
               {
                   pre->next=node;
                   node->next = cur;
 
               }
               break;
           }
           pre = cur;
           cur = cur->next;
 
        }
    if(cur == NULL)
    {
        pre->next=node;
 
    }
 
    }


}
//打印数据
void display(const List *list)
{
    if(NULL == list->pFrist)
    {
        printf("list is empty\n");
        return ;
    }
    Node* cur = list->pFrist;
    while(cur!=NULL)
    {
        printf("%d ",cur->iData);
                cur= cur->next;
 
    }
 
    printf("\n");
 
}
//创建链表
List *getlist()
{
    List *list = mklist();
    int iData = 0;
    while(1)
    {
        printf("please input data:\n");
        scanf("%d",&iData);
        if(0 == iData)
        {
            break;
 
        }
        insert(list,iData);
        display(list);
 
 
    }


    return list;
}
//链表合并
void merge(List* dest,List* src)
{
    if(NULL == dest|| NULL ==src)
    {
        return ;
 
    }
    Node* dCurNode =dest->pFrist;
    Node* dPreNode = dCurNode;
    Node* sNode = src->pFrist;
 
    while(NULL !=sNode )  //判断资源是否为空
    {
        while(NULL!= dCurNode)  //判断目标是否为空
        {
            if(sNode->iData < dCurNode->iData)
            {
                src->pFrist=sNode->next;//取出第一个节点
                if(dCurNode==dest->pFrist) //判断是不是第一个节点
                {
                    dest->pFrist=sNode;
                    sNode->next=dCurNode;
 
                }else                                     //中间插入
                {
                    dPreNode->next = sNode;
                    sNode->next = dCurNode;
                }
                dPreNode = sNode; //改变指针指向
                break;                           //跳出循环获得下一个 要查入的节点
            }
            else
            {
 
                dPreNode=dCurNode;                 //如果移动指针查找第一个比sNode 大的节点
                dCurNode = dCurNode->next;
            }
 
        }                                           //跳出循环
        if(NULL == dCurNode && NULL != sNode)
        {
 
            if(NULL == dest->pFrist)   //待查入的list为空
            {
                dest->pFrist = sNode;
                src->pFrist = NULL;
 
            }else   //目标list的当前指针为空 把目标list直接插入尾部
            {
                dPreNode->next = sNode;
                src->pFrist = NULL;
            }
            break;
        }
        sNode =src->pFrist; //改变目标list指针
    }
}
 
 
int main()
{
    printf("******list1*******\n");
    List *l1 = getlist();
 
 
    printf("******list2******\n");
    List *l2 = getlist();
 
    merge(l1,l2);
    printf("\n\n******list 1******\n");
    display(l1);
    printf("\n\n******lsit********\n");
    display(l2);
 
    return 0;
}