使用链表的多项式加法

问题描述 投票:0回答:1

这是使用c中的链表进行多项式加法的程序。在执行程序时,给定的多项式正确显示,但是在多项式相加之后,仅显示相加多项式的第一项,其余项不显示。请帮助我为什么会这样。

#include<stdio.h>
#include<stdlib.h>
struct polynl
{
int coef,expo;
struct polynl *next;
};
void main()
{
int n,i,coefficient,exponent;
struct polynl  *head=NULL,*head1=NULL,*head2=NULL,*temp,*temp1,*temp3,*p,*k,*c,*a,*b;
printf("Enter the no of terms of polynomial 1:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
    printf("Enter the coef of polynomial 1:");
    scanf("%d",&coefficient);
    printf("Enter the exponent of polynomial 1:");
    scanf("%d",&exponent);
    temp=(struct polynl*)malloc(sizeof( struct polynl));
    temp->coef=coefficient;
    temp->expo=exponent;
    temp->next=NULL;
    if(head1==NULL)
        head1=temp;
    else
    {
        p=head1;
    while(p->next!=NULL)
        p=p->next;
    p->next=temp;
    }

 }


printf("Enter the no of terms of polynomial 2:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
    printf("Enter the coef of polynomial 2:");
    scanf("%d",&coefficient);
    printf("Enter the exponent of polynomial 2:");
    scanf("%d",&exponent);
    temp1=(struct polynl*)malloc(sizeof(struct polynl));
    temp1->coef=coefficient;
    temp1->expo=exponent;
    temp1->next=NULL;
    if(head2==NULL)
        head2=temp1;
    else
    {
        k=head2;
    while(k->next!=NULL)
        k=k->next;
    k->next=temp1;
    }
}
printf("The entered polynomials are\n");
for(p=head1;p!=NULL;p=p->next)
{
printf("%dX^%d",p->coef,p->expo);
if(p->next!=NULL)
{
    printf("+");
}
}
printf("\n");

for(k=head2;k!=NULL;k=k->next)
{
printf("%dX^%d",k->coef,k->expo);
if(k->next!=NULL)
{
    printf("+");
}
}
printf("\n");
a=head1;
b=head2;
while(a->next!=NULL&&b->next!=NULL)
{
temp3=(struct polynl *)malloc(sizeof(struct polynl));

    temp3->next=NULL;
    if(head==NULL)
    {
        head=temp3;
        c=temp3;
    }
    else
    {
        while(c->next!=NULL)
        c=c->next;
        c->next=temp3;
    }

    if(a->expo==b->expo)
    {

        temp3->coef=a->coef+b->coef;
        temp3->expo=a->expo;
        a=a->next;
        b=b->next;
    }
    else if(a->expo>b->expo)
    {
        temp3->coef=a->coef;
        temp3->expo=a->expo;
        a=a->next;
    }
    else if(b->expo>a->expo)
    {
        temp3->coef=b->coef;
        temp3->expo=b->expo;
        b=b->next;
    }

}

    for(;a->next!=NULL;a=a->next)
    {
        temp3=(struct polynl*)malloc(sizeof(struct polynl));
        temp3->next=NULL;
        temp3->expo=a->expo;
        temp3->coef=a->coef;
        c->next=temp3;
        c=c->next;
    }
    for(;b->next!=NULL;b=b->next)
    {
        temp3=(struct polynl*)malloc(sizeof(struct polynl));
        temp3->next=NULL;
        temp3->expo=b->expo;
        temp3->coef=b->coef;
        c->next=temp;
        c=c->next;
    }
printf("Polynomial addition\n");
for(k=head;k!=NULL;k=k->next)
{
printf("%dX^%d",c->coef,c->expo);
if(k->next!=NULL)
{
    printf("+");
}
}
c singly-linked-list polynomials
1个回答
0
投票

您的代码有几个问题:

  • 这里的主要问题是,您尝试使用一个令人惊讶的功能来完成所有操作,尤其是因为您使用了比实际需要更多的临时变量。该代码具有很多重复(或近似重复,请参见下文),并且会受益于编写函数来添加多项式项,打印多项式以及可能还要添加两个多项式。

  • 同时循环ab时,您的条件是

    while(a->next != NULL && b->next != NULL)
    

    这意味着您错过了每个多项式的最后一项。应该是:

    while(a != NULL && b != NULL)
    

    (仅在必须到达最后一项时才需要next,但在这里必须处理所有项。)之后,必须对循环进行相同的更正,在其中添加其余项。

    ] >
  • c是新列表的结尾,但是您使用两种不同的策略来维护它。在ab的第一个循环中,通过在插入之前将其向前移动来确保c为尾部。其他两个循环,请在插入后重新分配c。第二种方法看起来更漂亮;也要在第一个循环中使用它,否则当您添加其余术语时,c将不正确。

  • 当其余术语来自b时,您分配c->next = temp,但是您应该分配c->next=temp3。 (这是在功能范围内使用一袋临时文件的缺点:编译器无法警告您。)

  • 打印结果时,您在k上循环,但在打印c的内容。

  • 修复这些错误,然后使用小的,整洁的功能重写程序。 :)

© www.soinside.com 2019 - 2024. All rights reserved.