一、代码实现
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedef struct LinkNode {
int coef;
int exp;
struct LinkNode* next;
}*LinkList;
LinkList initLinkList()
{
LinkList tempList = (LinkList)malloc(sizeof(struct LinkNode));
tempList->coef = 0;
tempList->exp = 0;
tempList->next = NULL;
return tempList;
}//off initLinkList
void printList(LinkList paraHeader)
{
LinkList p = paraHeader->next;
while (p != NULL)
{
printf("%d * 10^%d +", p->coef, p->exp);
p = p->next;
}//off while
printf("\r\n");
}//off printList
void printNode(LinkList paraPtr, char paraChar)
{
if (paraPtr == NULL)
{
printf("NULL\r\n");
}//off if
else
{
printf("the element of %c is (%d * 10^%d)\r\n", paraChar, paraPtr->coef, paraPtr->exp);
}//off else
}//off printNode
void appendElement(LinkList paraHeader, int paraCoff, int paraExp)
{
LinkList p, q;
q = (LinkList)malloc(sizeof(struct LinkNode));
q->coef = paraCoff;
q->exp = paraExp;
q->next = NULL;
p = paraHeader;
while (p->next != NULL)
{
p = p->next;
}//off while
p->next = q;
}//off appendElement
void add(LinkList paraHeader1, LinkList paraHeader2)
{
LinkList p, q, r, s;
p = paraHeader1->next;
printNode(p, 'p');
q = paraHeader2->next;
printNode(q, 'q');
r = paraHeader1;
printNode(r, 'r');
free(paraHeader2);
while (p != NULL && q != NULL)
{
if (p->exp < q->exp)
{
printf("case 1\r\n");
r->next = p;
r = p;
printNode(r, 'r');
p = p->next;
printNode(p, 'p');
}//off if
else if (p->exp > q->exp)
{
printf("case 2\r\n");
r->next = q;
r = q;
printNode(r, 'r');
q = q->next;
printNode(q, 'q');
}//off else if
else
{
printf("case 3\r\n");
p->coef = p->coef + q->coef;
printf("the coefficient is: %d\r\n", p->coef);
if (p->coef == 0)
{
printf("case 3.1\r\n");
s = p;
p = p->next;
printNode(p, 'p');
}//off is
else
{
printf("case 3.2\r\n");
r = p;
printNode(r, 'r');
p = p->next;
printNode(p, 'p');
}//off else
s = q;
q = q->next;
free(s);
}//off else
printf("p = %ld, q = %ld\r\n", q, p);
}//off while
printf("end of while\r\n");
if (p == NULL)
{
r->next = q;
}//off if
else
{
r->next = p;
}//off else
printf("addiction end\r\n");
}//off add
void Test1()
{
LinkList tempList1 = initLinkList();
LinkList tempList2 = initLinkList();
appendElement(tempList1, 7, 0);
appendElement(tempList1, 3, 1);
appendElement(tempList1, 9, 8);
appendElement(tempList1, 5, 17);
printList(tempList1);
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");
}//off Test
void Test2()
{
LinkList tempList1 = initLinkList();
LinkList tempList2 = initLinkList();
appendElement(tempList1, 7, 0);
appendElement(tempList1, 3, 1);
appendElement(tempList1, 9, 8);
appendElement(tempList1, 5, 17);
printList(tempList1);
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");
}//off Test2
int main()
{
Test1();
Test2();
return 0;
}
运行结果:
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 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 = 1612636144, q = 1612635264
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 = 1612635344, q = 1612636544
case 2
the element of r is (22 * 10^7)
the element of q is (-9 * 10^8)
p = 1612635984, q = 1612636544
case 3
the coefficient is: 0
case 3.1
the element of p is (5 * 10^17)
p = 0, q = 1612635904
end of while
addiction end
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 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 = 1612636624, q = 1612636464
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 = 1612634944, q = 1612635504
case 2
the element of r is (22 * 10^7)
the element of q is (-9 * 10^10)
p = 1612635024, q = 1612635504
case 1
the element of r is (9 * 10^8)
the element of p is (5 * 10^17)
p = 1612635024, q = 1612636064
case 2
the element of r is (-9 * 10^10)
NULL
p = 0, q = 1612636064
end of while
addiction end
the result is: 7 * 10^0 +11 * 10^1 +22 * 10^7 +9 * 10^8 +-9 * 10^10 +5 * 10^17 +
二、心得体会
作为重要的算法之一,学习数据结构中的多项式加法算法,让我更加深入地了解了数据结构和算法之间的互相作用。
多项式加法算法是基于链表的数据结构实现的。学习多项式加法算法的过程中,我深刻地认识到,链表这种数据结构在实现复杂的应用程序时,其灵活性和可扩展性是无与伦比的。
在实现多项式加法算法的过程中,我了解到了许多关于链表的基本操作和算法,例如链表的遍历、插入、删除、反转等等。这些基本操作和算法是将链表用于实际应用的基础,在学习多项式加法算法的同时也更深入地理解了链表的本质和优势。
在学习多项式加法算法的过程中,我发现设计高效的算法并不是一件容易的事情。在使用链表实现多项式加法算法时,考虑链表的特点,尽可能利用链表的优势,降低算法复杂度,提高算法的执行效率。这一过程不仅要求掌握数据结构和算法的基础知识,而且需要不断的实践和思考。