使用链表对多项式进行存储和相加,是链表压缩表示能力的一种体现,主要的难点在于对各个项大小的判断,主要的判断逻辑如下(以p、q、r为例,p为第一个多项式链表的第二个节点,q为第二个多项式链表的第二个节点,r为源第一个多项式内存空间新的头结点):
- 当p的指数小于q的指数时:将p链接在r的后面。
- 当p的指数大于q的指数时:将q链接在r的后面。
- 当p的指数等于q的指数时:a.相加系数和为0,则删去该节点。b.系数不为0,则相加成功。
优化:老师的代码在打印源多项式内容时末尾会出现多余的 “+” ,影响美观,在打印时进行判断可以解决此类情况。
代码设计如下:
//
// Created by Caka on 2023/4/9.
//
#include <iostream>
using namespace std;
/**
* Linked list of integers. The key is data. The key is sorted in non-descending order.
*/
typedef struct LinkNode {
int coefficient;
int exponent;
struct LinkNode *next;
} *LinkList, *NodePtr;
/**
* Initialize the list with a header.
* @return The pointer to the header.
*/
LinkList initLinkList() {
LinkList tempHeader = (LinkList) malloc(sizeof(struct LinkNode));
tempHeader->coefficient = 0;
tempHeader->exponent = 0;
tempHeader->next = NULL;
return tempHeader;
}// Of initLinkList
/**
* Print the list.
* @param paraHeader The header of the list.
*/
void printList(LinkList paraHeader) {
NodePtr p = paraHeader->next;
while (p != NULL && p->next!=NULL) {
cout << p->coefficient << " * " << "10^" << p->exponent << " + ";
p = p->next;
}// Of while
if(p!=NULL){
cout << p->coefficient << " * " << "10^" << p->exponent;
}//Of if
cout << endl;
}// Of printList
/**
* Print one node for testing.
* @param paraPtr The pointer to the node.
* @param paraChar The name of the node.
*/
void printNode(NodePtr paraPtr, char paraChar) {
if (paraPtr == NULL) {
cout << "NULL" << endl;
} else {
cout << "The element of " << paraChar << " is (" << paraPtr->coefficient << " * 10^" << paraPtr->exponent << ")"
<< ", the address is " << paraPtr << endl;
}// Of while
}// Of printNode
/**
* Add an element to the tail.
* @param paraCoefficient The coefficient of the new element.
* @param paraExponent The exponent of the new element.
*/
void appendElement(LinkList paraHeader, int paraCoefficient, int paraExponent) {
NodePtr p, q;
// Step 1. Construct a new node.
q = (NodePtr) malloc(sizeof(struct LinkNode));
q->coefficient = paraCoefficient;
q->exponent = paraExponent;
q->next = NULL;
// Step 2. Search to the tail.
p = paraHeader;
while (p->next != NULL) {
p = p->next;
}// Of while
// Step 3. Now add/link.
p->next = q;
}// Of appendElement
/**
* Polynomial addition.
* @param paraList1 The first list.
* @param paraList2 The second list.
*/
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.
cout << "Case 1:" << endl;
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.
cout << "Case 2:" << endl;
r->next = q;
r = q;
printNode(r, 'r');
q = q->next;
printNode(q, 'q');
} else {
cout << "Case 3:" << endl;
//Change the current node of the first list.
p->coefficient = p->coefficient + q->coefficient;
cout << "The coefficient is: " << p->coefficient << "." << endl;
if (p->coefficient == 0) {
cout << "Case 3.1:" << endl;
s = p;
p = p->next;
printNode(p, 'p');
// free(s);
} else {
cout << "Case 3.2:" << endl;
r = p;
printNode(r, 'r');
p = p->next;
printNode(p, 'p');
}// Of if
s = q;
q = q->next;
free(s);
}// Of if
cout << "p = " << p << ", q = " << q << endl;
} // Of while
cout << "End of while." << endl;
if (p == NULL) r->next = q;
else r->next = p;
cout << "Addition ends." << endl;
}// Of add
/**
* Unit test 1.
*/
void additionTest1() {
// Step 1. Initialize the first polynomial.
LinkList tempList1 = initLinkList();
appendElement(tempList1, 7, 0);
appendElement(tempList1, 3, 1);
appendElement(tempList1, 9, 8);
appendElement(tempList1, 5, 17);
printList(tempList1);
// Step 2. Initialize the second polynomial.
LinkList tempList2 = initLinkList();
appendElement(tempList2, 8, 1);
appendElement(tempList2, 22, 7);
appendElement(tempList2, -9, 8);
printList(tempList2);
// Step 3. Add them to the first.
add(tempList1, tempList2);
cout << "The result is: ";
printList(tempList1);
cout << endl;
}// Of additionTest1
/**
* Unit test 2.
*/
void additionTest2() {
// Step 1. Initialize the first polynomial.
LinkList tempList1 = initLinkList();
appendElement(tempList1, 7, 0);
appendElement(tempList1, 3, 1);
appendElement(tempList1, 9, 8);
appendElement(tempList1, 5, 17);
printList(tempList1);
// Step 2. Initialize the second polynomial.
LinkList tempList2 = initLinkList();
appendElement(tempList2, 8, 1);
appendElement(tempList2, 22, 7);
appendElement(tempList2, -9, 10);
printList(tempList2);
// Step 3. Add them to the first.
add(tempList1, tempList2);
cout << "The result is: ";
printList(tempList1);
cout << endl;
}// Of additionTest2
/**
* The entrance.
*/
int main() {
cout << "---PolynomialAddition Test begins---" << endl << endl;
additionTest1();
additionTest2();
cout << "---PolynomialAddition Test ends---";
return 0;
}// Of main
运行结果如下:
---PolynomialAddition Test begins---
7 * 10^0 + 3 * 10^1 + 9 * 10^8 + 5 * 10^17
8 * 10^1 + 22 * 10^7 + -9 * 10^8
The element of p is (7 * 10^0), the address is 0xee1670
The element of q is (8 * 10^1), the address is 0xee1710
The element of r is (0 * 10^0), the address is 0xee1650
Case 1:
The element of r is (7 * 10^0), the address is 0xee1670
The element of p is (3 * 10^1), the address is 0xee1690
p = 0xee1690, q = 0xee1710
Case 3:
The coefficient is: 11.
Case 3.2:
The element of r is (11 * 10^1), the address is 0xee1690
The element of p is (9 * 10^8), the address is 0xee16b0
p = 0xee16b0, q = 0xee1730
Case 2:
The element of r is (22 * 10^7), the address is 0xee1730
The element of q is (-9 * 10^8), the address is 0xee1750
p = 0xee16b0, q = 0xee1750
Case 3:
The coefficient is: 0.
Case 3.1:
The element of p is (5 * 10^17), the address is 0xee16d0
p = 0xee16d0, q = 0
End of while.
Addition ends.
The result is: 7 * 10^0 + 11 * 10^1 + 22 * 10^7 + 5 * 10^17
7 * 10^0 + 3 * 10^1 + 9 * 10^8 + 5 * 10^17
8 * 10^1 + 22 * 10^7 + -9 * 10^10
The element of p is (7 * 10^0), the address is 0xee1710
The element of q is (8 * 10^1), the address is 0xee17d0
The element of r is (0 * 10^0), the address is 0xee16f0
Case 1:
The element of r is (7 * 10^0), the address is 0xee1710
The element of p is (3 * 10^1), the address is 0xee1750
p = 0xee1750, q = 0xee17d0
Case 3:
The coefficient is: 11.
Case 3.2:
The element of r is (11 * 10^1), the address is 0xee1750
The element of p is (9 * 10^8), the address is 0xee1770
p = 0xee1770, q = 0xee17f0
Case 2:
The element of r is (22 * 10^7), the address is 0xee17f0
The element of q is (-9 * 10^10), the address is 0xee18a0
p = 0xee1770, q = 0xee18a0
Case 1:
The element of r is (9 * 10^8), the address is 0xee1770
The element of p is (5 * 10^17), the address is 0xee1790
p = 0xee1790, q = 0xee18a0
Case 2:
The element of r is (-9 * 10^10), the address is 0xee18a0
NULL
p = 0xee1790, q = 0
End of while.
Addition ends.
The result is: 7 * 10^0 + 11 * 10^1 + 22 * 10^7 + 9 * 10^8 + -9 * 10^10 + 5 * 10^17
---PolynomialAddition Test ends---