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

/* 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

posted @ 2014-03-08 08:59  ITtecman  阅读(340)  评论(0编辑  收藏  举报