1.一个多项式可以表示为X的各次幂与系数乘积的和,多项式的加法,是对单链表的一个基础应用,通过每一个链表来存储不同次幂的不同系数,达到一个链表存储一个多项式的效果
2.为了存储多项式,需要设立一个结构体,包含系数和指数两个变量,以及一个指针变量
3.两个多项式相加时,对多项式按指数进行排列,从大到小或从小到大,指数相同的项进行合并,系数相加后若为零则要删除这个节点
代码
#include <stdio.h>
#include <malloc.h>
/**
*Linked list of integers.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.
*@rerturn the pointer to the header.
*/
LinkList init_LinkList()
{
LinkList tempHeader = (LinkList)malloc(sizeof(struct LinkNode));
tempHeader->coefficient = 0;
tempHeader->exponent = 0;
tempHeader->next = NULL;
return tempHeader;
}//of init_LinkList
/**
*Print the list.
*@param paraHeader the header of the list.
*/
void print_List(LinkList paraHeader)
{
NodePtr p = paraHeader->next;
while (p != NULL)
{
printf("%d * 10^%d + ", p->coefficient, p->exponent);
p = p->next;
}//of while
printf("\n");
}//of print_List
/**
*Print one node for testing.
*@param paraPtr the pointer to the node.
*@param paraChar the name of the node.
*/
void print_Node(NodePtr paraPtr, char paraChar)
{
if (paraPtr == NULL)
{
printf("NULL\n");
}
else
{
printf("The element of %c is (%d * 10^%d)\n", paraChar, paraPtr->coefficient, paraPtr->exponent);
}//of if
}//of print_Node
/**
*Add an elment to the tsil.
*@param paraCoefficient the coefficient of the new element.
*@param paraExponent the exponent of the new element.
*/
void append_Element(LinkList paraHeader, int paraCoefficient, int paraExponent)
{
NodePtr p, q;
//Step1.Construct a new node.
q = (NodePtr)malloc(sizeof(struct LinkNode));
q->coefficient = paraCoefficient;
q->exponent = paraExponent;
q->coefficient = NULL;
//Step2.Search to the tail.
p = paraHeader;
while (p->next != NULL)
{
p = p->next;
}//of while
//Step3.Now add/link.
p->next = q;
}//of append_Element
/**
*Polynomial addition.
*@param paraList1 the first list.
*@param paraList2 the second list.
*/
void add(NodePtr paraList1, NodePtr paraList2)
{
NodePtr p, q, r, s;
//Step1.Search the position.
p = paraList1->next;
print_Node(p, 'p');
q = paraList2->next;
print_Node(q, 'q');
r = paraList1;//Previous pointer for inserting.
print_Node(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.
printf("Case 1\n");
r->next = p;
r = p;
print_Node(r, 'r');
p = p->next;
print_Node(p, 'p');
}
else if (p->exponent > q->exponent)
{
//Link the curent node of the second list.
printf("Case 2\n");
r->next = q;
r = q;
print_Node(r,'r');
q = q->next;
print_Node(q, 'q');
}
else
{
printf("Cse 3\n");
//Change the current node of the first list.
p->coefficient = p->coefficient + q->coefficient;
printf("The coefficient is :%d.\n", p->coefficient);
if (p->coefficient == 0)
{
printf("Case 3.1\n");
s = p;
p = p->next;
print_Node(p, 'p');
}
else
{
printf("case 3.2\n");
r = p;
print_Node(r, 'r');
p = p->next;
print_Node(p, 'p');
}//of if
s =q;
q =q->next;
free(s);
}//of if
printf("p = %ld, q = %ld\n", p, q);
}//of while
printf("End of while\n");
if (p == NULL)
{
r->next = q;
}
else
{
r->next = p;
}//of if
printf("Addition ends.\n");
}//of add
/**
*Unit test.
*/
void addition_Test1()
{
//Step1.Initialize the first polynommial.
LinkList tempList1 = init_LinkList();
append_Element(tempList1, 7, 0);
append_Element(tempList1, 3, 1);
append_Element(tempList1, 9, 8);
append_Element(tempList1, 5, 17);
print_List(tempList1);
//Step2.Initialize the second polynomial.
LinkList tempList2 = init_LinkList();
append_Element(tempList2, 8, 1);
append_Element(tempList2, 22, 7);
append_Element(tempList2,-9, 8);
print_List(tempList2);
//Step3.Addthem to the first.
add(tempList1, tempList2);
printf("The result is: ");
printf("\n");
}//of addition_Test1
/**
*Unit test2.
*/
void addition_Test2()
{
//Step1.Initialize the first polynomial.
LinkList tempList1 = init_LinkList();
append_Element(tempList1, 7, 8);
append_Element(tempList1, 3, 1);
append_Element(tempList1, 9, 8);
append_Element(tempList1, 5, 17);
print_List(tempList1);
//Step2.Initialize the second polynomial.
LinkList tempList2 = init_LinkList();
{
append_Element(tempList2, 8, 1);
append_Element(tempList2, 22, 7);
append_Element(tempList2, -9, 10);
print_List(tempList2);
}
//Step3.Add them to the first.
add(tempList1, tempList2);
printf("The result is: ");
print_List(tempList1);
printf("\n");
}//of addition_Test2
/**
*The entrance.
*/
int main()
{
addition_Test1();
addition_Test2();
printf("Finish.\n");
return 0;
}//of main
运行结果