zoukankan      html  css  js  c++  java
  • 链表的有序合并

    
    
    #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
    ");
            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
    ");
           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
    ");
            return ;
        }
        Node* cur = list->pFrist;
        while(cur!=NULL)
        {
            printf("%d ",cur->iData);
                    cur= cur->next;
    
    
        }
    
    
        printf("
    ");
    
    
    }
    //创建链表
    List *getlist()
    {
        List *list = mklist();
        int iData = 0;
        while(1)
        {
            printf("please input data:
    ");
            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*******
    ");
        List *l1 = getlist();
    
    
    
    
        printf("******list2******
    ");
        List *l2 = getlist();
    
    
        merge(l1,l2);
        printf("
    
    ******list 1******
    ");
        display(l1);
        printf("
    
    ******lsit********
    ");
        display(l2);
    
    
        return 0;
    }
    
    
    
    
    
    
    
    
    
    

  • 相关阅读:
    Flink 1.9 实战:使用 SQL 读取 Kafka 并写入 MySQL
    Spring 社区的唯一一个国产开源项目
    使用DynamoShake从dynamodb迁移到mongodb
    1754-I Hate It 线段树(单点替换,区间最值)
    1166-敌兵布阵 线段树(单点增减,区间求和)
    P1078 文化之旅
    P1107 最大整数
    521. [NOIP2010] 引水入城 cogs
    P1137 旅行计划
    P2678 跳石头(二分答案)
  • 原文地址:https://www.cnblogs.com/countryboy666/p/10932471.html
Copyright © 2011-2022 走看看