YaYa很开心 2022-06-10 01:32 采纳率: 81.3%
浏览 73
已结题

C语言合并一个降序和升序数列

我的输出特别奇怪,麻烦大家帮我看看
题目:已知链表 LA 按值非递增有序排列,LB 按值非递减有序排列,现要求将 LA和 LB 归并为一个新的链表 LC,且 LC 中的数据元素按值非递减有序排列

#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
    int date;
    struct node* next;
}node,*list;
list create();
void nizhi(list A);
void combine(list A,list B);
int main()
{
    list A=create();
    list B=create();
    void nizhi(A);
    combine(A,B);
    return 0;
}
list create()
{
    int x;
    scanf("%d",&x);
    list head=(list)malloc(sizeof(node));
    head->next=NULL;
    head->date=0;
    list q=head;
    do{
        list p=(list)malloc(sizeof(node));
        p->date=x;
        q->next=p;
        p->next=NULL;
        q=p;
    }while(getchar()!='\n');
    return head;
}
void nizhi(list A)
{
    list p=A->next;
    A->next=NULL;
    while(p){
        list r=p->next;
        p->next=A->next;
        A->next=p;
        p=r;
    }
}
void combine(list A,list B)
{
    list C=(list)malloc(sizeof(node));
    C->date=0;
    C->next=NULL;
    list t=C;
    list p=A->next;
    list q=B->next;
    while(p&&q){
        list s=(list)malloc(sizeof(node));
        if(p->date<q->date){
            s->date=p->date;
            t->next=s;
            t=s;
            p=p->next;
        }
        else if(p->date>q->date){
            s->date=q->date;
            t->next=s;
            t=s;
            q=q->next;
        }
    }
    if(p){
            t->next=p;
        }
        else if(q){
            t->next=q;
        }
    list f=C->next; 
    while(f){
        printf("%d ",f->date);
        f=f->next;
    }
}
  • 写回答

1条回答 默认 最新

  • qzjhjxj 2022-06-10 13:26
    关注

    修改处见注释,供参考。另:将 LA和 LB 归并为一个新的链表 LC,是否需要申请新的空间,值得商榷,代码里是生成一部分,后面又截取部分原来的。

    #include <stdio.h>
    #include <stdlib.h>
    typedef struct node
    {
        int date;
        struct node* next;
    }node, * list;
    list create();
    void nizhi(list A);
    void combine(list A, list B);
    int main()
    {
        list A = create();
        list B = create();
        nizhi(A);    //void nizhi(A); 函数调用  修改
        combine(A, B);
        return 0;
    }
    list create()
    {
        int x;
              //scanf("%d", &x);  修改
        list head = (list)malloc(sizeof(node));
        head->next = NULL;
        head->date = 0;
        list q = head;
        do {
            scanf("%d", &x); //输入 x 应放在 do{}while;循环体里 修改
            list p = (list)malloc(sizeof(node));
            p->date = x;
            q->next = p;
            p->next = NULL;
            q = p;
        } while ((getchar()) != '\n');
        return head;
    }
    void nizhi(list A)
    {
        list p = A->next;
        A->next = NULL;
        while (p) {
            list r = p->next;
            p->next = A->next;
            A->next = p;
            p = r;
        }
    }
    void combine(list A, list B)
    {
        list C = (list)malloc(sizeof(node));
        C->date = 0;
        C->next = NULL;
        list t = C;
        list p = A->next;
        list q = B->next;
        while (p && q) {
            list s = (list)malloc(sizeof(node));
            s->next = NULL;      //修改
            if (p->date < q->date) {
                s->date = p->date;
                t->next = s;
                t = s;
                p = p->next;
            }
            else {       //if (p->date > q->date) { 修改
                s->date = q->date;
                t->next = s;
                t = s;
                q = q->next;
            }
        }
        if (p) {
            t->next = p;
        }
        if (q) {  //else if (q) 修改
            t->next = q;
        }
        list f = C->next;
        while (f) {
            printf("%d ", f->date);
            f = f->next;
        }
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 6月18日
  • 已采纳回答 6月10日
  • 创建了问题 6月10日

悬赏问题

  • ¥15 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真