多项式可表示为多个系数的指数之和,我没可以用链表存储系数与其指数,按指数从小到达排列,当两个链表相加时,通过比较两个链表的结点的指数大小,谁小谁排在前面,相等时则相加,等于零时则跳过
#include <stdio.h>
#include <malloc.h>
//创造结点coefficient为系数,exponent为指数
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 && q){
if(p->exponent < q->exponent){
//插入到第一个列表
printf("case 1\n");
r->next = p;
r = p;
printNode(r,'r');
p = p->next;
printNode(p,'p');
}
else if(p->exponent > q->exponent){
printf("case 2\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');
}
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("End of while.\r\n");
if (p == NULL) {
r->next = q;
} else {
r->next = p;
} // Of if
printf("Addition ends.\r\n");
}
//测试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);
printf("The result is: ");
printList(tempList1);
printf("\r\n");
}
//测试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);
printf("The result is: ");
printList(tempList1);
printf("\r\n");
}
int main(){
additionTest1();
additionTest2();
printf("Finish.\r\n");
return 0;
}
测试结果