软基作业——使用链表实现多项式的存储以及加法

20 篇文章 0 订阅

当多项式的次数十分稀疏时,比如1+x^500,存储使用数组无疑会浪费大量的空间。因此,使用链表便成为明智的选择。以下是具体的实现过程。

1、头文件的定义:

poly.h:

/*
 * poly.h
 * by Eric Brown.
 */

#ifndef POLY_H
#define POLY_H

typedef struct polynome_node
{
    int factor;
    int index;
    struct polynome_node * next;
}node;


typedef struct poly
{
    node *head;
    int length;
}polynome;


/*create_poly: to create a polynome by an array.*/
polynome * create_poly(int data[][2], int length);


/*poly_sum: to obtain the sum of two polynomes.*/
polynome * poly_sum(polynome * poly_a, polynome * poly_b);


/*poly_print: to print the polynome.*/
void poly_print(polynome * poly);


/*poly_clear: to delete the zero factor.*/
polynome * poly_clear(polynome * poly);


#endif

2、具体算法的实现:

poly.c:

/*
 * poly.c
 * to create ,print and plus the polynome.
 * by Eric Brown.
 */


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


void node_delete(polynome *poly, node ** pnode, node ** cnode);


polynome * create_poly(int data[][2], int length)
{
    int i;
    node *p, *n;
    polynome *poly;


    if ((poly = (polynome *)malloc(sizeof(polynome)))==NULL 
        || (p = (node *)malloc(sizeof(node)))==NULL)
    {
        printf("Memory Error!\n");
        exit(EXIT_FAILURE);
    }
    if (length <= 0)
        return NULL;
    
    poly->head = p;
    poly->length = 1;
    
    p->factor = data[0][0];
    p->index = data[0][1];
    
    for (i = 1; i < length; i++)
    {
        n = (node *)malloc(sizeof(node));
        n->factor = data[i][0];
        n->index = data[i][1];
        p->next = n;
        p = n;
        poly->length++;
    }


    p->next = NULL;
    return poly;
}


polynome * poly_sum(polynome * poly_a, polynome * poly_b)
{
    if (poly_a->length == 0)
    {
        poly_a = poly_b;
        return poly_a;
    }
    if (poly_b->length == 0)
        return poly_a;


    node *ca, *cb, *pa, *pb, *temp;
    /*p for previous, c for current.*/
    ca = pa = poly_a->head;
    cb = pb = poly_b->head;
    
    while (ca != NULL)
    {
        if (ca->index < cb->index)
        {
            pa = ca;
            ca = ca->next;
        }
        else if (ca->index > cb->index)
        {
            temp = (node *)malloc(sizeof(node));
            temp->factor = cb->factor;
            temp->index = cb->index;
            temp->next = ca;
            pa->next = temp;
            pa = temp;
            pb = cb;
            cb = cb->next;
            poly_a->length++;
        }
        else if(ca->index == cb->index)
        {
            ca->factor += cb->factor;
            pa = ca;
            ca = ca->next;
            pb = cb;
            cb = cb->next;
        }
        if (cb == NULL)
            break;
            
    }
    
    if (ca == NULL)
    {
        pa->next = cb;
        while (cb != NULL)
        {
            poly_a->length++;
            cb = cb->next;
        }
    }
    
    poly_clear(poly_a);
    return poly_a;
}


void poly_print(polynome * poly)
{
    int i;
    node *p = poly->head;
    
    if (poly->length == 0)
    {
        printf("This is an empty polynome!\n");
        return ;
    }
    
    printf("%dx^%d", p->factor, p->index);
    
    while((p = p->next) != NULL)
        printf(" + %dx^%d", p->factor, p->index);
    
    printf("\n");
}


polynome * poly_clear(polynome * poly)
{
    /* c for current and p for previous.*/
    node *cnode, *pnode;
    cnode = pnode = poly->head;
    while(cnode != NULL)
    {
        if (cnode->factor == 0)
            node_delete(poly, &pnode, &cnode);
        else {
            pnode = cnode;
            cnode = cnode->next;
        }
    }
    return poly;
}


void node_delete(polynome *poly, node ** pnode, node ** cnode)
{
    
    node *temp;
    if (*cnode == poly->head)
    {
        temp = *cnode;
        poly->head = temp->next;
        *pnode = *cnode = poly->head;
        free(temp);
    } else {
        temp = *cnode;
        (*pnode)->next = temp->next;
        (*cnode) = temp->next;
        free(temp);
    }
    poly->length--;
}

3、测试程序:

test.c:

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


int main(void)
{
    int A[][2] = {{1,0},{5,1},{7,3},{-9,7},{5,16}};
    int B[][2] = {{-1,0},{5,1},{-7,3},{9,7},{-5,16}};
    int C[][2] = {{2,0},{-10,20}};
    polynome *poly_a = create_poly(A,5);
    polynome *poly_b = create_poly(B,5);
    polynome *poly_c = create_poly(C,2);
    
    poly_clear(poly_b);
    printf("Poly A:\t");
    poly_print(poly_a);
    printf("Poly B:\t");
    poly_print(poly_b);
    printf("Poly C:\t");
    poly_print(poly_c);
    printf("\n");
    
    poly_sum(poly_a, poly_b);
    
    printf("Poly A = A+B = \t:");
    poly_print(poly_a);
    printf("Length of Poly A:\t%d\n", poly_a->length);
    
    printf("Poly C = C+A = :\t");
    poly_sum(poly_c,poly_a);
    poly_print(poly_c);
    printf("Length of Poly C:\t%d\n", poly_c->length);
    return 0;
}

最后,依旧是makefile:

test.exe: test.o poly.o
gcc -o test.exe test.o poly.o
test.o: test.c poly.h
gcc -c test.c
poly.o: poly.c poly.h
gcc -c poly.c


以下是运行效果:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值