鱼C论坛

 找回密码
 立即注册
查看: 1718|回复: 5

如何将链表中的元素相加

[复制链接]
发表于 2021-3-9 17:04:48 From FishC Mobile | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
使用线性表结构实现多项式求和运算
要求:
1、请分别使用顺序表和链表实现两个多项式的加法运算,其中多项式的项按指数从高到低排列。例如,输入形式为3  2 (为次数) -1  1  2  0。
测试样例:
样例1:
输入样例:
3  4  2  3  1  2(第一个多项式)
-2  3  4  2  5  1  6  0(第二个多项式)
输出样例:
3  4  5  2  5  1  6  0(求和结果)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-3-9 17:39:59 | 显示全部楼层
不太理解样例中的求和结果
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-9 22:41:33 | 显示全部楼层
#include <stdio.h>
#include <stdlib.h>

struct Node{
        int a;
        int x;
        struct Node *next;
};


int main(void){
        struct Node *head1 = (struct Node*)malloc(sizeof(struct Node)); // 第一个多项式头指针 
        struct Node *head2 = (struct Node*)malloc(sizeof(struct Node)); // 第二个多项式头指针 
        head1->next = NULL; head2->next = NULL;
        struct Node *tmp1 = head1, *tmp2 = head2;
        
        int len_head1, len_head2;
        puts("请输入多项式1的项数: ");
        scanf("%d", &len_head1);
        puts("请输入多项式1:");
        for(int i = 1; i <= len_head1; i++){
                struct Node *p = (struct Node*)malloc(sizeof(struct Node));
                scanf("%d%d",&p->a,&p->x);
                tmp1->next = p;
                tmp1 = p;
        }
        tmp1->next = NULL;
        
        puts("请输入多项式2的项数: ");
        scanf("%d", &len_head2);
        puts("请输入多项式2:");
        for(int i = 1; i <= len_head2; i++){
                struct Node *p = (struct Node*)malloc(sizeof(struct Node));
                scanf("%d%d",&p->a,&p->x);
                tmp2->next = p;
                tmp2 = p;
        }
        tmp2->next = NULL;
        
        tmp1 = head1->next, tmp2 = head2->next; 
        struct Node *ans = (struct Node*)malloc(sizeof(struct Node));
        ans->next = NULL;
        struct Node *ans_head = ans;
        while(tmp1 && tmp2){
                if(tmp1->x > tmp2->x) {
                        struct Node *tmp = (struct Node *) malloc(sizeof(struct Node));
                        tmp->x = tmp1->x;
                        tmp->a = tmp1->a;
                        ans_head->next = tmp;
                        ans_head = tmp;
                        tmp1 = tmp1->next;
                }
                else if(tmp2->x > tmp1->x){
                        struct Node *tmp = (struct Node *) malloc(sizeof(struct Node));
                        tmp->x = tmp2->x;
                        tmp->a = tmp2->a;
                        ans_head->next = tmp;
                        ans_head = tmp;
                        tmp2 = tmp2->next;
                }
                else {
                        struct Node *tmp = (struct Node *) malloc(sizeof(struct Node));
                        tmp->x = tmp1->x;
                        tmp->a = tmp2->a + tmp1->a;
                        ans_head->next = tmp;
                        ans_head = tmp;
                        tmp1 = tmp1->next;
                        tmp2 = tmp2->next;
                }
        }
        if(tmp1) ans_head->next = tmp1;
        if(tmp2) ans_head->next = tmp2;
        
        puts("答案为:");
        ans_head = ans->next;
        while(ans_head){
                if(ans_head->a) printf("%d %d ",ans_head->a, ans_head->x);
                ans_head = ans_head->next;
        }
        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-9 22:45:48 | 显示全部楼层
本帖最后由 baige 于 2021-3-9 22:48 编辑

这种写法感觉是比较容易想的,不用去太多考虑指针的指向;
随便写的,我也没有去释放内存空间,太久没有用C语言了
不清楚你的多项式要怎么结束输入,就加了个项数
屏幕截图 2021-03-09 224701.png

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-3-9 23:13:00 From FishC Mobile | 显示全部楼层
是将不同多项式如5X(三次方)+3X+2,与另一多项式3X(四次方)+2X(三次方)想加
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-3-9 23:14:15 From FishC Mobile | 显示全部楼层
我是C++初学者,对于一些库函数啥的不是太了解,还请大佬指导
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-22 09:45

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表