首页 > 解决方案 > 我正在编写一个代码来使用链表执行多项式加法和乘法。我有一个问题

问题描述

在我的代码中,添加部分正在工作。但是乘法部分不起作用..问题出在 create_node() 函数中......以及乘以 2 个多项式的循环。代码是:

#include <stdio.h>
#include <stdlib.h>

struct node
{
    int cof;
    int exp;
    struct node* next; 
}*phead ,*rt, *qhead , *rhead,*newh,*ph,*qh,*rh;

void create_node(struct node* header)
{
    int degree,c,i;
    header->next=NULL;
    struct node* ptr = (struct node*)malloc(sizeof(struct node*));
    printf("Enter the degree of the polynomial:");
    scanf("%d",&degree);
    for(i=degree; i>=0; i--)
    {
        printf("Enter the coficient of x^%d:",i);
        scanf("%d",&c);
        if(c!=0)
        {
            struct node* newnode = (struct node*)malloc(sizeof(struct node*));
            newnode->cof = c;
            newnode->exp = i;
            if(header->next==NULL)
            {
                header->next = newnode;
                ptr=newnode;
            }
            else
            {
                ptr->next=newnode;
                ptr=ptr->next;
            }
        }
    }
    ptr->next=NULL;

}

void display()
{
    struct node *pptr,*qptr,*rptr;
    pptr=phead->next;
    qptr=qhead->next;
    rptr=rhead->next;

    printf("\nThe entered polynomials are:");
    printf("\npolynomial 1:");
    while(pptr!=NULL)
    {
        printf("%dx^%d + ",pptr->cof,pptr->exp);
        pptr = pptr->next;
    }
    printf("\npolynomial 2:");
    while(qptr!=NULL)
    {
        printf("%dx^%d + ",qptr->cof,qptr->exp);
        qptr = qptr->next;
    }

    printf("\nThe resultant  polynomial is:");
    while(rptr!=NULL)
    {
        printf("%dx^%d + ",rptr->cof,rptr->exp);
        rptr = rptr->next;
    }
}

void displayp()
{
    struct node *p1,*q1,*r1;
    p1=ph->next;
    q1=qh->next;
    r1=rh;

    printf("\nThe entered polynomials are:");
    printf("\npolynomial 1:");
    while(p1!=NULL)
    {
        printf("%dx^%d + ",p1->cof,p1->exp);
        p1 = p1->next;
    }
    printf("\npolynomial 2:");
    while(q1!=NULL)
    {
        printf("%dx^%d + ",q1->cof,q1->exp);
        q1 = q1->next;
    }

    printf("\nThe resultant  polynomial is:");
    while(r1!=NULL)
    {
        printf("%dx^%d + ",r1->cof,r1->exp);
        r1 = r1->next;
    }
}
void main()
{
    struct node *pptr , *qptr , *rptr,*pn,*pn1,*x,*pt,* qt,*rt;
    phead = (struct node *)malloc(sizeof(struct node));
    qhead = (struct node *)malloc(sizeof(struct node));
    rhead = (struct node *)malloc(sizeof(struct node));
    pn = (struct node *)malloc(sizeof(struct node));
    pn1 = (struct node *)malloc(sizeof(struct node));

    create_node(phead);
    create_node(qhead);



    pptr = phead;
    qptr = qhead;
    rptr = rhead;
    x = rhead;
    while (pptr!=NULL && qptr!=NULL)
    {
        newh = (struct node *)malloc(sizeof(struct node));
        if (pptr->exp == qptr->exp)
        {
            newh->cof = pptr->cof + qptr->cof;
            newh->exp = pptr->exp; 
            pptr = pptr->next;
            qptr = qptr->next;
        }

        else if (pptr->exp>qptr->exp)
        {

            newh->cof = pptr->cof;
            newh->exp = pptr->exp;
            pptr = pptr->next;
        }

        else if (pptr->exp<qptr->exp)
        {
            newh->cof = qptr->cof;
            newh->exp = qptr->exp;
            qptr = qptr->next;
        }
        if (rhead == NULL)
        {
            rhead = newh;
            rptr = newh;
        }
        else
        {
            rptr->next = newh;
            rptr = newh;
        }
    }
    while (pptr!=NULL)
    {
        newh = (struct node *)malloc(sizeof(struct node));
        newh->cof = pptr->cof;
        newh->exp = pptr->exp;
        newh->next = NULL;
        rptr->next = newh;
        rptr = newh;
        pptr = pptr->next;
    }
    while (qptr!=NULL)
    {
        newh = (struct node *)malloc(sizeof(struct node));
        newh->cof = qptr->cof;
        newh->exp = qptr->exp;
        newh->next = NULL;
        rptr->next = newh;
        rptr = newh;
        qptr = qptr->next;
    }
    display();
               printf("---------------------------------------\n");
         printf("POLYNOMIAL MULTIPLICATION\n");
         ph = (struct node *)malloc(sizeof(struct node));
         qh = (struct node *)malloc(sizeof(struct node));
         rt = (struct node *)malloc(sizeof(struct node));
         create_node(ph);
         create_node(qh);
 //      displayp();
         printf("\n");
         pt = ph->next;
         qt = qh->next;
         rh = NULL;
         //rt = rh;
         while (pt!=NULL)
         {
                 while (qt!=NULL)
                 {
                         if (rh == NULL)
                         {
                                 rt->cof = pt->cof * qt->cof;
                                 rt->exp = pt->exp + qt->exp;
                                 rh = rt;
                                //pt = pt->next;
                                              //rt->next = NULL;
                                 qt = qt->next;
                         }
                         else
                         {
                                 struct node *nw = (struct node *)malloc(sizeof(struct node));
                                 nw->cof = pt->cof * qt->cof;
                                 nw->exp = pt->exp + qt->exp;
                                 //nw->next = NULL;
                                 rt->next = nw;
                                 rt = rt->next;
                                 qt = qt->next;
                         }
                 }
         //       displayp();
                 pt = pt->next;

         }

         displayp();
 }

好吧,我说重点了。在乘法循环中,第一个节点被乘以第二多项式中的所有其他节点。然后执行停止。pt = pt->next 没有被执行。

请帮忙

标签: c

解决方案


推荐阅读