用C语言链表知识写一个多项式相乘
时间: 2023-03-12 21:35:57 浏览: 115
我可以给你提供一个简单的C语言程序来实现多项式的乘法运算: #include<stdio.h>
#include<stdlib.h> struct Polynomial
{
int coefficient;
int exponent;
struct Polynomial *next;
}; // 求两个多项式的乘积
struct Polynomial* multiplyPolynomial(struct Polynomial* poly1, struct Polynomial* poly2)
{
struct Polynomial *result = NULL;
struct Polynomial *temp, *tail;
while (poly1 != NULL)
{
poly2 = result;
tail = NULL;
while (poly2 != NULL)
{
if (poly1->exponent == poly2->exponent)
{
poly2->coefficient = poly2->coefficient + poly1->coefficient;
break;
}
tail = poly2;
poly2 = poly2->next;
}
if (poly2 == NULL)
{
temp = (struct Polynomial*)malloc(sizeof(struct Polynomial));
temp->coefficient = poly1->coefficient;
temp->exponent = poly1->exponent;
temp->next = NULL;
if (result == NULL)
result = temp;
else
tail->next = temp;
}
poly1 = poly1->next;
}
return result;
}
阅读全文