当多项式的次数十分稀疏时,比如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
以下是运行效果: