Single list of applications

Problem Description:

Completion of the addition operation of two polynomials: There are two known polynomial P (x), Q (x), the design algorithm P (x) + Q (x) operation, but not adding to the re-open space. Storage Structure requirements implemented. E.g:
Alt

algorithm design

The programmed four functions:
➢ function Init () to initialize the eleven empty list;
➢ function CreateTail () is used to create a linked list, this is the end of the interpolation method to create a linked list;
➢ add () function add algorithm used to implement the two polynomials;
➢ function print () for outputting a polynomial.
The sum of two polynomials algorithm, the first two polynomials were stored as a linked list. Two pointers may be provided LA1 and LB1 respectively, the first mobile node polynomials Pm (x) and Qm (x) from the index comparisons LA1 and LB1 indicated node, the following three cases may be treated:
(1 ) If LA1-> exp <LB1-> exp, LA1 is referred to as a node of a polynomial, LA1 pointer is moved backward one position on the basis of the original _.
(2) If LA1-> exp = LB1-> exp, adding the coefficient corresponding item, then two cases: If the coefficient
term is zero, the node is released LA1 and LB1 is directed; if the coefficient the term is not zero and, LA1 modifying the
coefficient field node pointed to by the node LB1 release.
(3) If LA1-> exp> LB1-> exp, the node LB1 referred to as a polynomial, LB1 original pointer
moves based on a backward position.
As shown in FIG. Experimental:
Alt

Program realization

#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
typedef struct poly
{
    int exp;//指数幂
    int coef;//指数
    struct poly* next;//指针域
}PNode,*Plinklist;
int Init(Plinklist* head)//链表的初始化
{
    *head=(Plinklist)malloc(sizeof(PNode));
    if(*head)
    {
        (*head)->next=NULL;
        return 1;
    }
    else
        return 0;
}
int CreateTail(Plinklist *head)//尾插法创建链表
{
    Plinklist pTemp,pHead;
    int c;//存放系数
    int exp;//存放指数
    int i=1;//计数用户计算到第几项
    pHead=*head;
    scanf("%d,%d",&c,&exp);
    while(c!=0)
    {
        pTemp=(Plinklist)malloc(sizeof(PNode));
        if(pTemp)
        {
            pTemp->exp=exp;
            pTemp->coef=c;
            pTemp->next=NULL;
            pHead->next=pTemp;
            pHead=pTemp;
            scanf("%d,%d",&c,&exp);
        }
        else
            return 0;
    }
    return 1;
}
void add(Plinklist LA,Plinklist LB)//两个多项式相加,两个表都是按指数顺序增长
{
    PNode *LA1=LA->next;//LA的移动
    PNode *LB1=LB->next;//LB的移动
    PNode *temp;//保存要删除的节点
    int sum=0;//存放系数的和
    while(LA1&&LB1)
    {
        if(LA1->exp<LB1->exp)
        {
            LA->next=LA1;
            printf("%d %d\n",LA->coef,LA->exp);
            LA=LA->next;
            LA1=LA1->next;
        }
        else if(LA1->exp==LB1->exp)
        {
            sum=LA1->coef+LB1->coef;
            if(sum)
            {
                LA1->coef=sum;
                LA->next=LA1;
                LA=LA->next;
                LA1=LA1->next;
                temp=LB1;
                LB1=LB1->next;
                free(temp);
            }
            else
            {
                temp=LA1;
                LA1=LA1->next;
                free(temp);
                temp=LB1;
                LB1=LB1->next;
                free(temp);
            }
        }
        else
        {
            LA->next=LB1;
            LA=LA->next;
            LB1=LB1->next;
        }
    }
    if(LA1)//剩余节点插入链表
        LA->next=LA1;
    else
        LA->next=LB1;
}
void print(Plinklist head)//输出多项式
{
    head=head->next;
    while(head)
    {
        if(head->exp)
            printf("%dx^%d",head->coef,head->exp);
        else
            printf("%d",head->coef);
        if(head->next)
            printf("+");
        else
            break;
        head=head->next;
    }
}
int main()
{
    Plinklist LA,LB;
    Init(&LA);
    Init(&LB);
    printf("请输入第一个多项式的系数,指数(如2,3)输入0,0结束输入\n");
    CreateTail(&LA);
    printf("请输入第二个多项式的系数,指数(如2,3)输入0,0结束输入\n");
    CreateTail(&LB);
    print(LA);
    printf("\n");
    print(LB);
    printf("\n");
    add(LA,LB);
    printf("两个多项式相加的结果为:\n");
    print(LA);
    printf("\n");
    return 0;
}

The result:
Alt
the code finished, drawing on a book completed. But in the two polynomial function of the sum of the last to have a judge sentence:
Alt
this is still do not understand why should we increase these words, as if there is no effect removed it. There is no big brother to tell white look, I consider less the kind of situation?

Published 40 original articles · won praise 174 · views 20000 +

Guess you like

Origin blog.csdn.net/weixin_44895651/article/details/105124438