zoukankan      html  css  js  c++  java
  • 有序列表的归并

    /*
    发现写长代码的时候写分成小部分去实现很有必要,容易差错
    */
    #include<stdio.h>
    #include<stdlib.h>
    struct node
    {
    int num;
    struct node* next;

    };
    struct node * input(int n)
    {
    int i;
    struct node *head,*tail,*p;
    head = tail = (struct node *)malloc(sizeof(struct node));
    head->next = NULL;

    for(i = 0; i < n; i++)
    {
    p = (struct node *)malloc(sizeof(struct node));
    p->next = NULL;
    scanf("%d", &p->num);
    tail->next = p;
    tail = tail->next;
    }

    return head;
    }

    int mix(struct node *head1,struct node *head2)
    {
    struct node *tail,*p1,*p2;
    p1 = head1->next;
    p2 = head2->next;
    free(head2);
    tail = head1;
    while(p1&&p2)
    {
    if(p1->num < p2->num)
    {
    tail->next = p1; //tail是游动指针
    tail = p1; //tail徐游动,就像建立新的链表一样
    p1 = p1->next;
    }
    else
    {
    tail->next = p2;
    tail = p2;
    p2 = p2->next;
    }
    }
    if(p1)
    {
    tail->next = p1;
    }
    else
    {
    tail->next = p2;
    }
    p1 = head1->next;
    while(p1->next != NULL)
    {
    printf("%d ",p1->num);
    p1 = p1->next;
    }
    printf("%d",p1->num);
    return 0;
    }

    int main()
    {
    struct node *head1,*tail,*head2,*p;
    int n1,n2;

    scanf("%d%d",&n1,&n2);

    head1 = input(n1);
    head2 = input(n2);
    p = head1->next;

    mix(head1,head2);
    }
  • 相关阅读:
    PHPStrom 转 VSCode 折腾记录
    vscode php 代码提示 自动完成
    Elasticsearch中文分词加拼音
    AutoMapper用法
    删除所有退出状态的容器
    Linux 安装Docker
    千里眼的修练方法--末法时代即将结束
    Visual NMP
    c#通过反射获取类上的自定义特性
    微信小程序学习笔记
  • 原文地址:https://www.cnblogs.com/0803yijia/p/2364051.html
Copyright © 2011-2022 走看看