【数据结构——链表应用:多项式求和】
链表的应用——多项式加法
结构体定义
typedef struct LinkNode
{
int coefficient;
int power;
struct LinkNode* next;
} *LinkList, *NodePtr, Node;
初始化链表
// 初始化链表
LinkList initLinkList()
{
LinkList tempHeader = (LinkList)malloc(sizeof(Node));
tempHeader->coefficient = 0;
tempHeader->power = 0;
tempHeader->next = NULL;
return tempHeader;
}
打印链表
//打印链表
void printList(LinkList tempHeader)
{
NodePtr p = tempHeader->next;
while(p)
{
printf("%d * 10^%d + ",p->coefficient,p->power);
p = p->next;
}
printf("\n");
}
在尾部添加一个元素
//在尾部添加一个元素
void appendElement(LinkList tempHeader, int tempCoefficient, int tempPower)
{
NodePtr p,q;
q = (NodePtr)malloc(sizeof(Node));
q->coefficient = tempCoefficient;
q->power = tempPower;
q->next = NULL;
p = tempHeader;
while (p->next)
{
p = p->next;
}
p->next = q;
}
多项式加法
算法步骤:
- 初始化指针p和q,分别指向tempList1和tempList2的头结点。
- r指向和多项式(此次为tempList1)的当前结点(初始值为tempList1的头结点)。
- 当p和q均未到达表尾时,比较p和q所指结点对应的power值,其情况如下:
- 当p->power < q->power时,取p所指结点插入到tempList1中
- 当p->power > q->power时,取q所指结点插入到tempList1中
- 当p->power = q->power时,将两结点中系数相加,若和不为零,则修改p所指结点的系数值,同时删除q所指结点;若和为零,则删除p和q所指节点
如下图所示:
代码如下:
//多项式加法
void polynomialAddition(NodePtr tempList1, NodePtr tempList2)
{
NodePtr p,q,r,s;
/*
p为tempList内的结点
q为tempList内的结点
r为和多项式当前结点
s为暂存删除要删除的结点
*/
p = tempList1->next;
printNode(p,'p');
q = tempList2->next;
printNode(q,'q');
r = tempList1;
printNode(r,'r');
free(tempList2);
while(p && q)
{
if(p->power < q->power)
{
printf("First case!\n");
r->next = p;
r = p;
//printNode(r,'r');
p = p->next;
//printNode(p,'p');
}else if(p->power > q->power)
{
printf("Second case!\n");
r->next = q;
r = q;
//printNode(r,'r');
q = q->next;
//printNode(q,'q');
}else
{
printf("Third case!\n");
p->coefficient += q->coefficient;
printf("The coefficent is: %d.\n",p->coefficient);
if (!(p->coefficient))
{
printf("The first case of the third case!\n");
s = p;
p = p->next;
free(s);
s = q;
q = q->next;
free(s);
}else
{
r->next = p;
r = p;
p = p->next;
s = q;
q = q->next;
free(s);
}
}
}
p ? r->next = p : r->next = q;
printf("Addition ends.\n");
}
完整代码及运行截图
完整代码如下:
#include <stdio.h>
#include <malloc.h>
typedef struct LinkNode
{
int coefficient;
int power;
struct LinkNode* next;
} *LinkList, *NodePtr, Node;
// 初始化链表
LinkList initLinkList()
{
LinkList tempHeader = (LinkList)malloc(sizeof(Node));
tempHeader->coefficient = 0;
tempHeader->power = 0;
tempHeader->next = NULL;
return tempHeader;
}
//打印链表
void printList(LinkList tempHeader)
{
NodePtr p = tempHeader->next;
while(p)
{
printf("%d * 10^%d + ",p->coefficient,p->power);
p = p->next;
}
printf("\n");
}
void printNode(NodePtr paraPtr, char paraChar)
{
if(!paraPtr)
{
printf("NULL\n");
}else
{
printf("The element of %c is(%d * 10^%d)\n",paraChar,paraPtr->coefficient,paraPtr->power);
}
}
//在尾部添加一个元素
void appendElement(LinkList tempHeader, int tempCoefficient, int tempPower)
{
NodePtr p,q;
q = (NodePtr)malloc(sizeof(Node));
q->coefficient = tempCoefficient;
q->power = tempPower;
q->next = NULL;
p = tempHeader;
while (p->next)
{
p = p->next;
}
p->next = q;
}
//多项式加法
void polynomialAddition(NodePtr tempList1, NodePtr tempList2)
{
NodePtr p,q,r,s;
/*
p为tempList内的结点
q为tempList内的结点
r为和多项式当前结点
s为暂存删除要删除的结点
*/
p = tempList1->next;
printNode(p,'p');
q = tempList2->next;
printNode(q,'q');
r = tempList1;
printNode(r,'r');
free(tempList2);
while(p && q)
{
if(p->power < q->power)
{
printf("First case!\n");
r->next = p;
r = p;
//printNode(r,'r');
p = p->next;
//printNode(p,'p');
}else if(p->power > q->power)
{
printf("Second case!\n");
r->next = q;
r = q;
//printNode(r,'r');
q = q->next;
//printNode(q,'q');
}else
{
printf("Third case!\n");
p->coefficient += q->coefficient;
printf("The coefficent is: %d.\n",p->coefficient);
if (!(p->coefficient))
{
printf("The first case of the third case!\n");
s = p;
p = p->next;
free(s);
s = q;
q = q->next;
free(s);
}else
{
r->next = p;
r = p;
p = p->next;
s = q;
q = q->next;
free(s);
}
}
}
p ? r->next = p : r->next = q;
printf("Addition ends.\n");
}
//测试
void additionTest()
{
printf("--------initTempList1--------\n");
LinkList tempList1 = initLinkList();
appendElement(tempList1,7,0);
appendElement(tempList1,3,1);
appendElement(tempList1,9,8);
appendElement(tempList1,5,17);
printList(tempList1);
printf("--------initTempList2--------\n");
LinkList tempList2 = initLinkList();
appendElement(tempList2,8,1);
appendElement(tempList2,22,7);
appendElement(tempList2,-9,8);
printList(tempList2);
printf("--------Add them to the first--------\n");
polynomialAddition(tempList1,tempList2);
printList(tempList1);
}
int main()
{
additionTest();
}
运行截图