下面是《数据结构与算法分析C语言描述》中的40页练习
polynomialList.h
#ifndef POLYNOMIAL_LIST_H
#define POLYNOMIAL_LIST_H
typedef struct node
{
int coefficient;
int exponent;
node * next;
} * Node;
typedef Node polynomial;
void error(const char []);
polynomial generatePoly(int highPower);
void displayPolynomial(const polynomial poly);
void insert(polynomial poly, Node X);
void insertX(polynomial poly, Node X);
void addPolynomial(const polynomial poly1, const polynomial poly2, polynomial polySum);
void multiPolynomial(const polynomial poly1, const polynomial poly2, polynomial polyMulti);
#endif
#endif
polynomialList.c
#include "polynomialList.h"
#include<stdio.h>
#include<stdlib.h>
void insert(polynomial head, Node X)
{
if(head->next == NULL)
{
head->next = X;
}
else
{
Node node = head->next;
head->next = X;
X->next = node;
}
}
void error(const char printedChar[])
{
printf(printedChar);
printf("\n");
}
polynomial generatePoly(int highPower)
{
polynomial polyHead = (polynomial)malloc(sizeof(struct node));
polyHead->coefficient = 0;
polyHead->exponent = 0;
polyHead->next = NULL;
for(int i = 0; i <= highPower; i++)
{
Node X = (polynomial)malloc(sizeof(struct node));
X->coefficient = random() % 10;
X->exponent = i;
X->next = NULL;
insert(polyHead, X);
}
return polyHead;
}
void displayPolynomial(polynomial polyHead)
{
Node node = polyHead->next;
while (node->next != NULL)
{
printf("%dX^%d + ", node->coefficient, node->exponent);
node = node->next;
}
printf("%d \n\n", node->coefficient);
}
// 在比X小的幂数前,插入X
void insertX(polynomial poly, Node X)
{
polynomial P;
Node Pprev;
Node new_node = (Node)malloc(sizeof(struct node));
new_node->coefficient = X->coefficient;
new_node->exponent = X->exponent;
new_node->next = X->next;
// printf(">>>>>>>>>>>>插入节点%dX^%d \n", X->coefficient, X->exponent);
if(poly->next != NULL)
{
P = poly->next;
while (P != NULL && P->exponent > X->exponent)
{
Pprev = P;
// printf("%dX^%d ", P->coefficient, P->exponent);
P = P->next;
}
// printf("\n");
if(P == NULL)
{
Pprev->next = new_node;
new_node->next = NULL;
return;
}
// 这时候,P->exponent可能小于等于X->exponent
if (P->exponent == X->exponent)
{
P->coefficient = P->coefficient + X->coefficient;
}
else
{
// printf("#####插入节点%dX^%d \n", X->coefficient, X->exponent);
Node newNode = (Node)malloc(sizeof(struct node));
newNode->next = P;
Pprev->next = newNode;
}
}
else
{
poly->next = new_node;
new_node->next = NULL;
}
}
void addPolynomial(const polynomial poly1, const polynomial poly2, polynomial polySum)
{
Node node1, node2;
node1 = poly1->next;
node2 = poly2->next;
// 如果其中一个多项式为空
if(node1 == NULL)
{
polySum = poly2;
return;
}
else if (node2 == NULL)
{
polySum = poly1;
return;
}
// 如果两个多项式都不空
while (node1 != NULL || node2 != NULL)
{
if (node1 != NULL && node2 != NULL)
{
if (node1->exponent == node2->exponent)
{
// printf("%d == %d \n", node1->exponent, node2->exponent);
Node new_node = (Node)malloc(sizeof(struct node));
new_node->coefficient = node1->coefficient + node2->coefficient;
new_node->exponent = node1->exponent;
new_node->next = NULL;
insertX(polySum, new_node);
}
else if(node1->exponent > node2->exponent)
{
// printf("%d > %d \n", node1->exponent, node2->exponent);
insertX(polySum, node1);
insertX(polySum, node2);
}
else
{
// printf("%d < %d \n", node1->exponent, node2->exponent);
insertX(polySum, node2);
insertX(polySum, node1);
}
node1 = node1->next;
node2 = node2->next;
}
else if (node1 == NULL && node2 != NULL)
{
// printf("node1 == NULL n");
insertX(polySum, node2);
node2 = node2->next;
}
else if (node2 == NULL && node1 != NULL)
{
// printf("node2 == NULL \n");
insertX(polySum, node1);
node1 = node1->next;
}
}
}
void multiPolynomial(const polynomial poly1, const polynomial poly2, polynomial polyMulti)
{
Node node1;
Node node2;
node1 = poly1->next;
node2 = poly2->next;
if (poly1->next == NULL || poly2->next == NULL)
{
return;
}
else
{
while (node1 != NULL)
{
node2 = poly2->next;
while (node2 != NULL)
{
Node new_node = (Node)malloc(sizeof(struct node));
new_node->next = NULL;
new_node->coefficient = node1->coefficient * node2->coefficient;
new_node->exponent = node1->exponent + node2->exponent;
insertX(polyMulti, new_node);
node2 = node2->next;
}
node1 = node1->next;
}
}
}
polynomialListTest.c
#include "polynomialList.c"
int main()
{
polynomial poly1 = generatePoly(3);
displayPolynomial(poly1);
polynomial poly2 = generatePoly(2);
displayPolynomial(poly2);
polynomial poly3 = (polynomial)malloc(sizeof(struct node));
poly3->next = NULL;
poly3->coefficient = 0;
poly3->exponent = 0;
addPolynomial(poly1, poly2, poly3);
printf("加:");
displayPolynomial(poly3);
polynomial poly4 = (polynomial)malloc(sizeof(struct node));
multiPolynomial(poly1, poly2, poly4);
printf("乘:");
displayPolynomial(poly4);
return 0;
}
}