多项式加法
多项式加法是对单链表的进一步应用,掌握了可以帮助我们更好地理解单链表。
在我使用老师代码时,我更换了一组测试用例后发现运行结果是乱码,我就先检查了发现建立多项式和打印的函数没有问题。就进一步检查加法函数,就发现了跟踪新多项式的指针r并没有指向下一位(即缺少了r->next = q;或r->next = p;的操作),添加这一步后发现我的测试用例可以实现,至于为什么老师的代码在错误的情况下依旧可以实现特殊用例,还不太清楚。
函数实现
1.定义
typedef struct LinkNode
{
int coefficient;//系数
int exponent;//指数
struct LinkNode *next;
} *LinkList, *NodePtr;
2.初始化
LinkList initLinkList()
{
LinkList tempHeader = (LinkList)malloc(sizeof(struct LinkNode));
tempHeader->coefficient = 0;
tempHeader->exponent = 0;
tempHeader->next = NULL;
return tempHeader;
}
3.打印
void printList(LinkList paraHeader)
{
NodePtr p = paraHeader->next;
while(p != NULL)
{
if(p->next == NULL)
{
printf("%d * x^%d ",p->coefficient,p->exponent);
}
else
printf("%d * x^%d + ",p->coefficient,p->exponent);
p = p->next;
}
printf("\r\n");
}
4.添加
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;
}
5.加法运算
void add(NodePtr paraList1, NodePtr paraList2)
{
NodePtr p, q, r, s;
//p,q为临时结点,r追踪新链表 ,s释放空间
//寻找位置
p = paraList1->next;
q = paraList2->next;
r = paraList1;
free(paraList2);
//分三种情况
while ((p != NULL) && (q != NULL))
{
if (p->exponent < q->exponent)
{
r->next = p;//原代码缺少
r = p;
p = p->next;
}
else if(p->exponent > q->exponent)
{
r->next = q;
r = q;
q = q->next;
}
//讨论相加后系数是否为0
else
{
p->coefficient = p->coefficient + q->coefficient;
if(p->coefficient == 0)
{
s = p;
p = p->next;
free(s);
}
else
{
r->next = p;//原代码缺少
r = p;
p = p->next;
}
s = q;
q = q->next;
free(s);
}
}
if(p == NULL)
{
r->next = q;
}else
{
r->next = p;
}
}
功能测试
1.测试函数
void additionTest(){
//初始化
LinkList tempList1 = initLinkList();
LinkList tempList2 = initLinkList();
//创建第一个多项式
appendElement(tempList1, 5, 0);
appendElement(tempList1, 3, 1);
appendElement(tempList1, 4, 3);
appendElement(tempList1, 7, 10);
appendElement(tempList1, -2, 12);
printList(tempList1);
//创建第二个多项式
appendElement(tempList2, 8, 1);
appendElement(tempList2, 5, 2);
appendElement(tempList2, -7, 10);
appendElement(tempList2, 9, 12);
printList(tempList2);
//相加
add(tempList1, tempList2);
printList(tempList1);
}
原测试用例:7+3x+9x^8+5x^17 + 8x+22x^7-9x^8(缺少关键步骤仍能实现)
2.测试结果
5 * x^0 + 3 * x^1 + 4 * x^3 + 7 * x^10 + -2 * x^12
8 * x^1 + 5 * x^2 + -7 * x^10 + 9 * x^12
5 * x^0 + 11 * x^1 + 5 * x^2 + 4 * x^3 + 7 * x^12
3.测试图解
总代码
#include <stdio.h>
#include <malloc.h>
//定义
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)
{
if(p->next == NULL)
{
printf("%d * x^%d ",p->coefficient,p->exponent);
}
else
printf("%d * x^%d + ",p->coefficient,p->exponent);
p = p->next;
}
printf("\r\n");
}
//添加
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,q为临时结点,r追踪新链表 ,s释放空间
//寻找位置
p = paraList1->next;
q = paraList2->next;
r = paraList1;
free(paraList2);
//分三种情况
while ((p != NULL) && (q != NULL))
{
if (p->exponent < q->exponent)
{
r->next = p;
r = p;
p = p->next;
}
else if(p->exponent > q->exponent)
{
r->next = q;
r = q;
q = q->next;
}
//讨论相加后系数是否为0
else
{
p->coefficient = p->coefficient + q->coefficient;
if(p->coefficient == 0)
{
s = p;
p = p->next;
free(s);
}
else
{
r->next = p;
r = p;
p = p->next;
}
s = q;
q = q->next;
free(s);
}
}
if(p == NULL)
{
r->next = q;
}else
{
r->next = p;
}
}
//测试
void additionTest(){
//初始化
LinkList tempList1 = initLinkList();
LinkList tempList2 = initLinkList();
//创建第一个多项式
appendElement(tempList1, 5, 0);
appendElement(tempList1, 3, 1);
appendElement(tempList1, 4, 3);
appendElement(tempList1, 7, 10);
appendElement(tempList1, -2, 12);
printList(tempList1);
//创建第二个多项式
appendElement(tempList2, 8, 1);
appendElement(tempList2, 5, 2);
appendElement(tempList2, -7, 10);
appendElement(tempList2, 9, 12);
printList(tempList2);
//相加
add(tempList1, tempList2);
printList(tempList1);
}
/**
* The entrance.
*/
void main()
{
additionTest();
}