m0_61800796 2021-10-19 12:36 采纳率: 93.9%
浏览 80
已结题

这为啥运行结果是空的啊,编译也没问题

img


 
#include <stdio.h>
#include <stdlib.h>
struct space{
        int xishu;
        int zhishu;
        struct space *next;
};
struct space* create();
void print(struct space *p);
struct space* mul(struct space *p1,struct space *p2);
struct space* add(struct space *p1,struct space *p2);
void attach(int c,int e,struct space **L2);
int main(){
    struct space *p1;
    struct space *p2;
    struct space *p3;
    struct space *p4;
    p1 = create();
    p2 = create();
    p3 = mul(p1,p2);
    p4 = add(p1,p2);
    print(p3);
    print(p4);
    return 0;
}
struct space* create(){ //创建链表 
    int n,i;
    scanf("%d",&n);
    struct space *head;
    struct space *rom;
    rom=(struct space*)malloc(sizeof(struct space));
    head=rom;
    while(n--){
    rom=rom->next;
    rom=(struct space*)malloc(sizeof(struct space));
    scanf("%d %d",&rom->xishu,&rom->zhishu);
    }
    rom->next=NULL;
    return head;
}
struct space* mul(struct space *p1,struct space *p2){ //两个多项式相乘 
    struct space *L,*front1,*middle1,*middle2;
    L=(struct space*)malloc(sizeof(struct space));
    L->next=NULL;
    front1=L;
    p1=p1->next;
    p2=p2->next;
    middle1=p1;
    L=L->next;
    for(;middle1;middle1=middle1->next){
        middle2=p2;
        for(;middle2;middle2=middle2->next){
    L->xishu=(middle1->xishu)+(middle2->xishu);
    L->zhishu=(middle1->zhishu)*(middle2->zhishu);
    L->next = NULL;
    L=L->next;
    L=(struct space*)malloc(sizeof(struct space));
    L->next = NULL;
        }
    }
    return front1;
}
struct space* add(struct space *p1,struct space *p2){ //两个多项式相加 
    struct space *L2,*front2;
    L2=(struct space*)malloc(sizeof(struct space));
    L2->next=NULL;
    front2=L2;
    p1=p1->next;
    p2=p2->next;
    while(p1&&p2){
    if(p1->zhishu>p2->zhishu){
        attach(p1->xishu,p1->zhishu,&L2);
        p1=p1->next;
    }else if(p2->zhishu>p2->zhishu){
        attach(p2->xishu,p2->zhishu,&L2);
        p2=p2->next;
    }else{
        if(p2->xishu+p1->xishu){
            attach(p1->xishu+p2->xishu,p1->zhishu,&L2);
            p2=p2->next;
            p1=p1->next;
        }else{
            ;
        }
    }
}
        L2->next=p1?p1:p2;
        return front2;
}
void attach(int c,int e,struct space **L2){ //将系数、指数插到新链表里 
    struct space *p;
    p=(struct space*)malloc(sizeof(struct space));
    p->ishu=c;
    p->zhishu=e;
    (*L2)->next=p;
    (*L2)=p;
}
void print(struct space *p){ //打印 
    p=p->next;
    while(p){
        printf("%d %d",p->xishu,p->zhishu);
        if(p->next){
        printf(" ");
        }
        p=p->next;
    }
}
 
  • 写回答

4条回答 默认 最新

  • orange4reg 2021-10-19 16:16
    关注
    
    
    ```c
    
    //代码太多了,眼会花,先改这个创建的函数,改完还有错就继续来
    struct space* create(){ //创建链表 
        int n,i;
        scanf("%d",&n);
        struct space *head;
        struct space *rom;
        head=(struct space*)malloc(sizeof(struct space));
        rom=head;
        while(n--){
        rom->next=(struct space*)malloc(sizeof(struct space));
        rom=rom->next;
        scanf("%d %d",&rom->xishu,&rom->zhishu);
        }
        rom->next=NULL;
        return head;
    }
    
    

    ```

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

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

悬赏问题

  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误