多项式加法是单链表的一个实际运用。在这个问题中,我们假设每个多项式项都由系数和指数表示,并且这些项存储在单链表中。每个节点可能系数、指数和一个指向下一个节点的指针。通过双指针算法将两个多项式相加。
代码如下:
#include<iostream>
using namespace std;
//定义数据结构
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 * x^%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 * x^%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) {
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)) {
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 = %p, q = %p\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");
}
//测试1
void additionTest1() {
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);
printf("The result is: ");
printList(tempList1);
printf("\r\n");
}
//测试2
void additionTest2() {
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, 10);
printList(tempList2);
add(tempList1, tempList2);
printf("The result is: ");
printList(tempList1);
printf("\r\n");
}
int main() {
additionTest1();
additionTest2();
printf("Finish.\r\n");
return 0;
}
补充:
输出优化
void printList(LinkList paraHeader) {
NodePtr p = paraHeader->next;
if (p != NULL) {
printf("%d * x^%d", p->coefficient, p->exponent);
p = p->next;
}
while (p != NULL) {
if (p->coefficient > 0) {
printf(" + %d * x^%d", p->coefficient, p->exponent);
}
else {
printf(" - %d * x^%d", -p->coefficient, p->exponent);
}
p = p->next;
}
printf("\r\n");
}
end