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");
}
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);
}
}
4.添加元素
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;
}
5.加起来
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) { //情况一:指数p<q
printf("case 1\r\n");
r = p;
printNode(r, 'r');
p = p->next;
printNode(p, 'p');
} else if ((p->exponent > q->exponent)) {//情况二:指数p>q
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) {//情况三:p.q指数相等,系数相加为零
printf("case 3.1\r\n");
s = p;
p = p->next;
printNode(p, 'p');
// free(s);
} else {//情况四:p.q指数相等,系数相加不为零
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("End of while.\r\n");
if (p == NULL) {
r->next = q;
} else {
r->next = p;
}
printf("Addition ends.\r\n");
}
多项式的加法运算一般用单链表实现
算法思路大概是:
两个指针P1和P2分别指向这两个多项式的第一个节点,不断循环
P1->expon==P2->expon:系数相加,若结果不为0,则作为结果多项式对应项的系数。
同时,P1和P2都分别指向下一项
P1->expon>P2->expon:将P1的当前项存入结果多项式,并使P1指向下一项
P1->expon<P2->expon:将P2的当前项存入结果多项式,并使P2指向下一项
6.测试
void additionTest(){
LinkList tempList1 = initLinkList();
appendElement(tempList1, 7, 0);
appendElement(tempList1, 3, 1);
appendElement(tempList1, 9, 8);
appendElement(tempList1, 5, 17);
printList(tempList1);
LinkList tempList2 = initLinkList();
appendElement(tempList2, 8, 1);
appendElement(tempList2, 22, 7);
appendElement(tempList2, -9, 8);
printList(tempList2);
add(tempList1, tempList2);
printList(tempList1);
}
7.完整代码
#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;
}
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) { //情况一:指数p<q
printf("case 1\r\n");
r = p;
printNode(r, 'r');
p = p->next;
printNode(p, 'p');
} else if ((p->exponent > q->exponent)) {//情况二:指数p>q
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) {//情况三:p.q指数相等,系数相加为零
printf("case 3.1\r\n");
s = p;
p = p->next;
printNode(p, 'p');
// free(s);
} else {//情况四:p.q指数相等,系数相加不为零
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("End of while.\r\n");
if (p == NULL) {
r->next = q;
} else {
r->next = p;
}
printf("Addition ends.\r\n");
}
void additionTest(){
LinkList tempList1 = initLinkList();
appendElement(tempList1, 7, 0);
appendElement(tempList1, 3, 1);
appendElement(tempList1, 9, 8);
appendElement(tempList1, 5, 17);
printList(tempList1);
LinkList tempList2 = initLinkList();
appendElement(tempList2, 8, 1);
appendElement(tempList2, 22, 7);
appendElement(tempList2, -9, 8);
printList(tempList2);
add(tempList1, tempList2);
printList(tempList1);
}
void main(){
additionTest();
}
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 = 8066096, q = 8066224
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 = 8066128, q = 8066256
case 2
The element of r is (22 * 10^7)
The element of q is (-9 * 10^8)
p = 8066128, q = 8066288
case 3
The coefficient is: 0.
case 3.1
The element of p is (5 * 10^17)
p = 8066160, q = 0
End of while.
Addition ends.
7 * 10^0 + 11 * 10^1 + 22 * 10^7 + 5 * 10^17 +
请按任意键继续...