思路:根据多项式相加的运算规则:对两个多项式中所有指数相同的项,对应系数想加,若其和不为零,则作为“和多项式”中的一项插入到“和多项式”链表中去;对于两个多项式中指数不相同的项,则将指数较小的项插入到“和多项式”链表中去。 “多项式”链表中的节点无需生成,而应该从两个多项式的链表中摘取
1.定义结构体
typedef struct LinkNode {
int coefficient;//系数
int exponent;//指数
struct LinkNode* next;
}*LinkList,*NodePtr;
2.初始化
LinkList initLinkList()
{
LinkList tempHeader = (LinkList)malloc(sizeof(struct LinkNode));
tempHeader->coefficient = 0;
tempHeader->exponent = 0;
tempHeader->next = NULL;
return tempHeader;
}
3.打印链表
void printList(LinkList paraHeader) {
NodePtr p = paraHeader->next;
while (p != NULL)
{
printf("%d * 10^%d + ", p->coefficient, p->exponent);
p = p->next;
}
printf("\r\n");
}
4.打印结点
void printNode(NodePtr paraPtr, char paraChar)/*传入结点的指针与指针的名字*/ {
if (paraPtr == NULL) {
printf("NULL\r\n");
}
else {
printf("The element of %c is (%d * 10^%d)\r\n", paraChar, paraPtr->coefficient, paraPtr->exponent);
}
}
5.构造多项式
void appendElement(LinkList paraHeader, int paraCoefficient, int paraExponent)
{
NodePtr p, q;
//创建一个新结点
q = (NodePtr)malloc(sizeof(struct LinkNode));
q->coefficient = paraCoefficient;
q->exponent = paraExponent;
q->next = NULL;
p = paraHeader;
//寻找最后一个结点
while (p->next != NULL)
{
p=p->next;
}
//连接
p->next = q;
}
6.计算多项式相加
void add(NodePtr paraList1, NodePtr paraList2){
NodePtr p, q, r, s;
// Step 1. Search to the position.
p = paraList1->next;
printNode(p, 'p');
q = paraList2->next;
printNode(q, 'q');
r = paraList1; // Previous pointer for inserting.
printNode(r, 'r');
free(paraList2); // The second list is destroyed.
while ((p != NULL) && (q != NULL)) {
if (p->exponent < q->exponent) {
//Link the current node of the first list.
printf("case 1\r\n");
r->next = p;
r = p;
printNode(r, 'r');
p = p->next;
printNode(p, 'p');
} else if ((p->exponent > q->exponent)) {
//Link the current node of the second list.
printf("case 2\r\n");
r->next = q;
r = q;
printNode(r, 'r');
q = q->next;
printNode(q, 'q');
} else {
printf("case 3\r\n");
//Change the current node of the first list.
p->coefficient = p->coefficient + q->coefficient;
printf("The coefficient is: %d.\r\n", p->coefficient);
if (p->coefficient == 0) {
printf("case 3.1\r\n");
s = p;
p = p->next;
printNode(p, 'p');
// free(s);
} else {
printf("case 3.2\r\n");
r = p;
printNode(r, 'r');
p = p->next;
printNode(p, 'p');
}// Of if
s = q;
q = q->next;
//printf("q is pointing to (%d, %d)\r\n", q->coefficient, q->exponent);
free(s);
}// Of if
printf("p = %ld, q = %ld \r\n", p, q);
} // Of while
printf("End of while.\r\n");
if (p == NULL) {
r->next = q;
} else {
r->next = p;
} // Of if
printf("Addition ends.\r\n");
}// Of add
7.测试
void additionTest() {
// Step 1.构造第一个多项式
LinkList tempList1 = initLinkList();
appendElement(tempList1, 7, 0);
appendElement(tempList1, 3, 1);
appendElement(tempList1, 9, 8);
appendElement(tempList1, 5, 17);
printList(tempList1);
// Step 2. 都早第二个多项式
LinkList tempList2 = initLinkList();
appendElement(tempList2, 8, 1);
appendElement(tempList2, 22, 7);
appendElement(tempList2, -9, 8);
printList(tempList2);
// Step 3. 把第二个添加到第一个多项式里
add(tempList1, tempList2);
printList(tempList1);
}// Of additionTest
8.全部代码
#include<stdio.h>
#include<malloc.h>
/**
*整数的链表,关键是数据,关键按非降序排序
*/
typedef struct LinkNode {
int coefficient;//系数
int exponent;//指数
struct LinkNode* next;
}*LinkList,*NodePtr;
/**
*初始化链表
*返回链表的指针
*/
LinkList initLinkList()
{
LinkList tempHeader = (LinkList)malloc(sizeof(struct LinkNode));
tempHeader->coefficient = 0;
tempHeader->exponent = 0;
tempHeader->next = NULL;
return tempHeader;
}
//打印链表
void printList(LinkList paraHeader) {
NodePtr p = paraHeader->next;
while (p != NULL)
{
printf("%d * 10^%d + ", p->coefficient, p->exponent);
p = p->next;
}
printf("\r\n");
}
//打印结点
void printNode(NodePtr paraPtr, char paraChar)/*传入结点的指针与指针的名字*/ {
if (paraPtr == NULL) {
printf("NULL\r\n");
}
else {
printf("The element of %c is (%d * 10^%d)\r\n", paraChar, paraPtr->coefficient, paraPtr->exponent);
}
}
void appendElement(LinkList paraHeader, int paraCoefficient, int paraExponent)
{
NodePtr p, q;
//创建一个新结点
q = (NodePtr)malloc(sizeof(struct LinkNode));
q->coefficient = paraCoefficient;
q->exponent = paraExponent;
q->next = NULL;
p = paraHeader;
//寻找最后一个结点
while (p->next != NULL)
{
p=p->next;
}
//连接
p->next = q;
}
/*
计算连个多项式相加
paraList1是第一个多项式的头结点
paraList2是第二个多项式的头结点
*/
void add(NodePtr paraList1, NodePtr paraList2)
{
NodePtr p, q, r, s;
//寻找位置
p = paraList1->next;
printNode(p, 'p');
q = paraList2->next;
printNode(q, 'q');
r = paraList1;
printNode(r, 'r');
free(paraList2);
while ((p != NULL) && (q != NULL))
{
if (p->exponent < q->exponent)
{
printf("case 1\r\n");
r = p;
printNode(r, 'r');
p = p->next;
printNode(p, 'p');
}
else if (p->exponent > q->exponent)
{
printf("case 2\r\n");
r->next = q;
r = q;
printNode(r, 'r');
q = q->next;
printNode(q, 'q');
}
else {
printf("case 3\r\n");
p->coefficient = p->coefficient + q->coefficient;
printf("The coefficient is: %d.\r\n", p->coefficient);
if (p->coefficient == 0) {
printf("case 3.1\r\n");
s = p;
p = p->next;
printNode(p, 'p');
free(s);
}
else {
printf("case 3.2\r\n");
r = p;
printNode(r, 'r');
p = p->next;
printNode(p, 'p');
}
s = q;
q = q->next;
free(s);
}
printf("p = %ld, q = %ld \r\n", p, q);
}
printf("结束循环.\r\n");
if (p == NULL) {
r->next = q;
}
else {
r->next = p;
} // Of if
printf("结束地址\r\n");
}
//测试
void additionTest() {
// Step 1.构造第一个多项式
LinkList tempList1 = initLinkList();
appendElement(tempList1, 7, 0);
appendElement(tempList1, 3, 1);
appendElement(tempList1, 9, 8);
appendElement(tempList1, 5, 17);
printList(tempList1);
// Step 2. 都早第二个多项式
LinkList tempList2 = initLinkList();
appendElement(tempList2, 8, 1);
appendElement(tempList2, 22, 7);
appendElement(tempList2, -9, 8);
printList(tempList2);
// Step 3. 把第二个添加到第一个多项式里
add(tempList1, tempList2);
printList(tempList1);
}// Of additionTest
void main() {
additionTest();
printf("结束.\r\n");
}
9.运行结果
8 * 10^1 + 22 * 10^7 + -9 * 10^8 +
The element of p is (7 * 10^0)
The element of q is (8 * 10^1)
The element of r is (0 * 10^0)
case 1
The element of r is (7 * 10^0)
The element of p is (3 * 10^1)
p = -1508844160, q = -1508843280
case 3
The coefficient is: 11.
case 3.2
The element of r is (11 * 10^1)
The element of p is (9 * 10^8)
p = -1508843200, q = -1508843040
case 2
The element of r is (22 * 10^7)
The element of q is (-9 * 10^8)
p = -1508843200, q = -1508844240
case 3
The coefficient is: 0.
case 3.1
The element of p is (5 * 10^17)
p = -1508843920, q = 0
结束循环.
结束地址
7 * 10^0 + 11 * 10^1 + 22 * 10^7 + 5 * 10^17 +
结束.