多项式加法(单链表 c语言)

本文介绍了如何使用C语言实现多项式加法,通过单链表存储多项式,包括创建、显示、加法运算和冒泡排序等功能。程序中包含了输入多项式的系数和指数,对两个多项式进行相加,并对结果进行排序,最后显示加法结果并释放内存。
摘要由CSDN通过智能技术生成
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
typedef struct pnode
{
    float coef;
    int expn;
    struct pnode *next;
} pnode;

pnode * creat()
{
    float input_coef;
    int input_expn;
    pnode *head=NULL;
    pnode *prev, *current;
    while(scanf("%f%d",&input_coef,&input_expn)&&(input_expn>=0))
    {
        current=(pnode *)malloc(sizeof(pnode));
        if(head==NULL)
            head=current;
        else
            prev->next=current;
        current->next=NULL    ;
        current->coef=input_coef;
        current->expn=input_expn;
        prev=current;
        //    printf("input coef,expn:\n");
    }
    return head;
}

void display(pnode *head)
{
    pnode *current;
    current=head;
    while(current!=NULL)
    {
        printf("%.1f  %d\n",current->coef,current->expn);
        current=current->next;
    }
    printf("\n");
}

pnode * add(pnode *heada,pnode *headb)
{
    pnode *headc=NULL;
    pnode *p=heada,*q=headb;
    pnode *current,*prev;

    while(p!=NULL&&q!=NULL)
    {
        current=(pnode *)malloc(sizeof(pnode));
        if(headc==NULL)
            headc=current;
        else
            prev->next=current;
        current->next=NULL ;
        if(p->expn==q->expn )
        {
            //printf("%d\n",p->expn);
            if(p->coef+q->coef!=0)
            {
                current->coef=p->coef+q->coef;
                current->expn=p->expn;
                prev=current;
            }
            q=q->next;
            p=p->next;
        }
        else if(p->expn>q->expn)
        {
            //printf("%d  %d\n",p->expn,q->expn);
            current->coef=p->coef;
            current->expn=p->expn;
            prev=current;
            p=p->next;
        }
        else
        {
            //printf("%d  %d\n",p->expn,q->expn);
            current->coef=q->coef;
            current->expn=q->expn;
            prev=current;
            q=q->next;
        }
    }
    while(p!=NULL)
    {
        current=(pnode *)malloc(sizeof(pnode));
        if(headc==NULL)
            headc=current;
        else
            prev->next=current;
        current->next=NULL;
        current->coef=p->coef;
        current->expn=p->expn;
        prev=current;
        p=p->next;
    }
    while(q!=NULL)
    {
        current=(pnode *)malloc(sizeof(pnode));
        if(headc==NULL)
            headc=current;
        else
            prev->next=current;
        current->next=NULL;
        current->coef=q->coef;
        current->expn=q->expn;
        prev=current;
        q=q->next;
    }
    return headc;
}


void BubbleSort(pnode *head)
{
    pnode *i=NULL,*j=NULL;
    int temp_expn;
    float temp_coef;
    for(i = head; i!= NULL; i = i -> next)
        for(j=i->next; j!=NULL; j=j->next)
            if(i->expn<j->expn)
            {
                temp_expn=j->expn;
                temp_coef=j->coef;
                j->coef=i->coef;
                j->expn=i->expn;
                i->expn=temp_expn;
                i->coef=temp_coef;
            }
}


int Delete(pnode *head)
{
    pnode *temp;
    while(head!=NULL)
    {
        temp=head->next;
        free(head);
        head=temp;
    }
    if(head==NULL)
        return 1;
}


int main()

{
    freopen("input.txt","r",stdin);
    pnode * a,*b,*c;
    printf("input the first:\n");
    a=creat();
    BubbleSort(a);
    printf("input the second:\n");
    b=creat();
    BubbleSort(b);
    c=add(a,b);
    printf("the first:\n");
    display(a);
    printf("the second:\n");
    display(b);
    printf("sum is:\n");
    display(c);
    Delete(a);
    Delete(b);
    Delete(c);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值