一元多项式(具有非负次幂)的链表实现

/* list_poly.h */

#ifndef _LIST_POLY_H
#define _LIST_POLY_H

struct node;

typedef struct node *ptr_to_node;
typedef struct node *position;
typedef struct node *list;

list create_list();
void insert(int coef, int exp, list l, position p);
void insert_to_head(int coef, int exp, list l);
void insert_to_tail(int coef, int exp, list l);
position find_previous(int exp, list l);
int is_last(position p, list l);
void delete(int exp, list l);
void printl(list l);
void add_poly(const list poly1, const list poly2, list polysum);      /* 多项式加法 */
void multi_poly(const list poly1, const list poly2, list polyprod);   /* 多项式乘法 */
void merger_same_exp(list poly);    /* 合并同类项 */

#endif
/* list_poly.c */

#include "list_poly.h"
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>

struct node {
    int coef;
    int exp;
    ptr_to_node next;
};

list 
create_list()
{
    list l;
    l = malloc(sizeof(struct node));
    if(l == NULL)
    {
        perror("malloc error");
        exit(1);
    }
    l->next = NULL;
    
    return(l);
}

void 
insert(int coef, int exp, list l, position p)
{
    position newnode;
    newnode = malloc(sizeof(struct node));

    if(newnode == NULL)
    {
        perror("malloc error");
        exit(1);
    }
    newnode->coef = coef;
    newnode->exp = exp;
    newnode->next = p->next;
    p->next = newnode;
}
void
insert_to_head(int coef, int exp, list l)
{
    insert(coef, exp, l, l);
}
void
insert_to_tail(int coef, int exp, list l)
{
    ptr_to_node nptr;

    nptr = l;
    while(nptr->next != NULL)
        nptr = nptr->next;
    insert(coef, exp, l, nptr);
}
position
find_previous(int exp, list l)
{
    position p;
    
    p = l;
    while(p->next != NULL && p->next->exp != exp)
        p = p->next;

    return(p);
}
int
is_last(position p, list l)
{
    return(p->next == NULL);
}
void
delete(int exp, list l)
{
    position p, tmpp;

    p = find_previous(exp, l);

    if( !is_last(p, l))
    {
        tmpp = p->next; 
        p->next = tmpp->next;
        free(tmpp);
    }
}
void printl(list l)
{
    position p;
    
    p = l->next;    /* because we have a list_head */
    while(p != NULL)
    {
        if(!is_last(p, l))
        {
            if(p->coef != 1)
                printf("%dx^%d + ", p->coef, p->exp);
            else
                printf(" x^%d + ", p->exp);
        }
        else
        {
            if(p->coef != 1)
                printf("%dx^%d\n", p->coef, p->exp);
            else
                printf("x^%d\n", p->exp);
        }
        p = p->next;    
    }
}

void 
add_poly(const list poly1, const list poly2, list polysum)
{
    ptr_to_node p1, p2;
    p1 = poly1->next;    /* because we have a list_head */
    p2 = poly2->next;
    
    while(p1 != NULL || p2 != NULL)
    {
        if(p1 != NULL && p2 != NULL)
        {
            if(p1->exp > p2->exp)
            {        
                insert_to_tail(p1->coef, p1->exp, polysum);
                p1 = p1->next;
            }
            else if(p1->exp < p2->exp)
            {
                insert_to_tail(p2->coef, p2->exp, polysum);
                p2 = p2->next;
            }
            else
            {
                insert_to_tail(p1->coef + p2->coef, p1->exp, polysum);
                p1 = p1->next;
                p2 = p2->next;
            }
        }
        else if(p1 != NULL && p2 == NULL)
        {
            for(; p1 != NULL; p1 = p1->next)
            {
                insert_to_tail(p1->coef, p1->exp, polysum);
            }
        }
        else if(p1 == NULL && p2 != NULL)
        {
            for(; p2 != NULL; p2 = p2->next)
            {
                insert_to_tail(p2->coef, p2->exp, polysum);
            }
        }
        
    }
}

void 
multi_poly(const list poly1, const list poly2, list polyprod)
{
    ptr_to_node p1, p2;

    for(p1 = poly1->next; p1 != NULL; p1 = p1->next)
        for(p2= poly2->next; p2 != NULL; p2 = p2->next)
        {
            insert_to_tail(p1->coef * p2->coef, p1->exp + p2->exp, polyprod);
        }
    
    merger_same_exp(polyprod);
}

void
merger_same_exp(list poly)
{
    list p, q, qpre, tmp;

    for(p = poly->next; p != NULL; p = p->next)
        for(qpre = p, q = p->next; q != NULL; qpre = q, q = q->next)
        {
            if(q->exp == p->exp)
            {
                p->coef += q->coef;
                tmp = q;
                qpre->next = q->next;
                free(tmp);    
            }
        }
}
/* list_poly_test.c */

#include "list_poly.h"
#include <stdio.h>

int
main(void)
{    
    list poly1, poly2, polysum, polyprod;
    poly1 = create_list();
    poly2 = create_list();
    polysum = create_list();    
    polyprod = create_list();

    insert_to_tail(3, 2, poly1);
    insert_to_tail(2, 1, poly1);
    insert_to_tail(1, 0, poly1);

    insert_to_tail(4, 2, poly2);
    insert_to_tail(3, 1, poly2);
    insert_to_tail(1, 0, poly2);
    
    printf("poly1         = ");
    printl(poly1);
    printf("poly2         = ");
    printl(poly2);
    
    add_poly(poly1, poly2, polysum);
    printf("poly1 + poly2 = ");
    printl(polysum);
    
    multi_poly(poly1, poly2, polyprod);
    printf("poly1 * poly2 = ");
    printl(polyprod);
}

 

编译及运行结果如下:

image

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值