hhhhhhaa 发表于 2021-3-9 17:04:48

如何将链表中的元素相加

使用线性表结构实现多项式求和运算
要求:
1、请分别使用顺序表和链表实现两个多项式的加法运算,其中多项式的项按指数从高到低排列。例如,输入形式为32 (为次数) -1120。
测试样例:
样例1:
输入样例:
342312(第一个多项式)
-23425160(第二个多项式)
输出样例:
34525160(求和结果)

巴巴鲁 发表于 2021-3-9 17:39:59

不太理解样例中的求和结果

baige 发表于 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;
}

baige 发表于 2021-3-9 22:45:48

本帖最后由 baige 于 2021-3-9 22:48 编辑

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


hhhhhhaa 发表于 2021-3-9 23:13:00

是将不同多项式如5X(三次方)+3X+2,与另一多项式3X(四次方)+2X(三次方)想加

hhhhhhaa 发表于 2021-3-9 23:14:15

我是C++初学者,对于一些库函数啥的不是太了解,还请大佬指导
页: [1]
查看完整版本: 如何将链表中的元素相加