求两个多项式的相加运算
目的:掌握线性表的应用和有序单链表的二路归并算法设计。
内容:用单链表储存一元多项式,并实现两个多项式的相加运算。
思路:用结点存储系数和指数,遇到指数相同的结点就使其系数相加,将其放在第三个链表中。
定义结构体:
typedef int ElemType;
typedef struct LNode
{
ElemType coe; //系数
ElemType exp; //指数
struct LNode* next;
}LinkNode;
算法实现
bool Addition(LinkNode* L1, LinkNode* L2, LinkNode*& L3) //L1、L2、L3为三个单链表的头结点
{
//如果L1和L2中均没有数据
if (L1->next == NULL && L2->next == NULL) {
return false;
}
//直到L3申请成功
while (!L3) {
L3 = (LinkNode*)malloc(sizeof(LNode));
}
LinkNode* p, * q, * r, * s = NULL;
p = L1->next; //p为L1的工作指针
q = L2->next; //q为L2的工作指针
r = L3; //r为L3的尾指针
//当L1和L2均不为空时
while (p && q) {
s = (LinkNode*)malloc(sizeof(LNode));
//如果申请失败继续申请,直到申请成功,因为malloc可能申请失败
while (!s) {
s = (LinkNode*)malloc(sizeof(LNode));
}
if (p->exp > q->exp) {
s->coe = p->coe; //如果此时p指向的结点系数较大,复制该结点
s->exp = p->exp;
r->next = s; //将s接到尾指针后面
r = r->next; //尾指针后移
p = p->next; //p继续向后扫描L1
}
else if (q->exp < p->exp) {
s->coe = q->coe; //如果此时q指向的结点系数较大,复制该结点
s->exp = q->exp;
r->next = s; //将s接到尾指针后面
r = r->next; //尾指针后移
q = q->next; //q继续向后扫描L2
}
else {
s->coe = p->coe + q->coe; //如果两个值相等,将两个结点的系数相加
s->exp = p->exp;
r->next = s; //将s接到尾指针后面
r = r->next; //尾指针后移
p = p->next; //p继续向后扫描L1
q = q->next; //q继续向后扫描L2
}
}
//当p不为空时,将剩余结点接到尾指针后面
while (p) {
s = (LinkNode*)malloc(sizeof(LNode));
//如果申请失败继续申请,直到申请成功,因为malloc可能申请失败
while (!s) {
s = (LinkNode*)malloc(sizeof(LNode));
}
s->coe = p->coe; //复制结点的值
s->exp = p->exp;
r->next = s; //将s接到尾指针后面
r = r->next; //尾指针后移
p = p->next; //p继续向后扫描L1
}
//当q不为空时,将剩余结点接到尾指针后面
while (q) {
s = (LinkNode*)malloc(sizeof(LNode));
//如果申请失败继续申请,直到申请成功,因为malloc可能申请失败
while (!s) {
s = (LinkNode*)malloc(sizeof(LNode));
}
s->coe = q->coe; //复制结点的值
s->exp = q->exp;
r->next = s; //将s接到尾指针后面
r = r->next; //尾指针后移
q = q->next; //q继续向后扫描L2
}
r->next = NULL; //此时将尾指针后面置空,单链表L3建成
return true;
}
bool Addition(LinkNode* L1, LinkNode* L2, LinkNode*& L3)中用到了引用参数,引用参数在C++里才有,所以源代码文件格式应为".cpp"。
与主函数相结合
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
typedef int ElemType;
typedef struct LNode
{
ElemType coe; //系数
ElemType exp; //指数
struct LNode* next;
}LinkNode;
bool Addition(LinkNode* L1, LinkNode* L2, LinkNode*& L3); //L1、L2、L3为三个单链表的头结点
bool InitList(LinkNode*& L); //链表产生
bool DispList(LinkNode* L); //链表输出
int main(int argc, const char* argv[])
{
LinkNode* L1 = NULL, * L2 = NULL, * L3 = NULL;
bool flag = false;
InitList(L1);
InitList(L2);
flag = Addition(L1, L2, L3);
printf("多项式相加结果为:\n");
flag = DispList(L3);
return 0;
}
bool InitList(LinkNode*& L) //建立单链表
{
while (!L) {
L = (LinkNode*)malloc(sizeof(LNode));
}
L->next = NULL;
int i, n;
LinkNode* p = NULL, * q = NULL;
q = L;
printf("请输入数据规模:\n");
scanf("%d", &n);
printf("请输入数据:\n");
for (i = 0; i < n; i++) {
while (!p) {
p = (LinkNode*)malloc(sizeof(LNode));
}
printf("系数:");
scanf("%d", &p->coe);
printf("指数:");
scanf("%d", &p->exp);
q->next = p;
q = p;
p = q->next = NULL;
}
return true;
}
bool DispList(LinkNode* L) //输出单链表
{
LinkNode* p = L->next;
bool flag = true;
while (p) {
if (flag) {
printf("(%dx%d)", p->coe, p->exp);
flag = false;
}
else {
if(p->exp==0)
printf("+(%d)", p->coe);
else
printf("+(%dx%d)", p->coe, p->exp);
}
p = p->next;
}
printf("\n");
return true;
}
bool Addition(LinkNode* L1, LinkNode* L2, LinkNode*& L3) //L1、L2、L3为三个单链表的头结点
{
//如果L1和L2中均没有数据
if (L1->next == NULL && L2->next == NULL) {
return false;
}
//直到L3申请成功
while (!L3) {
L3 = (LinkNode*)malloc(sizeof(LNode));
}
LinkNode* p, * q, * r, * s = NULL;
p = L1->next; //p为L1的工作指针
q = L2->next; //q为L2的工作指针
r = L3; //r为L3的尾指针
//当L1和L2均不为空时
while (p && q) {
s = (LinkNode*)malloc(sizeof(LNode));
//如果申请失败继续申请,直到申请成功,因为malloc可能申请失败
while (!s) {
s = (LinkNode*)malloc(sizeof(LNode));
}
if (p->exp > q->exp) {
s->coe = p->coe; //如果此时p指向的结点系数较大,复制该结点
s->exp = p->exp;
r->next = s; //将s接到尾指针后面
r = r->next; //尾指针后移
p = p->next; //p继续向后扫描L1
}
else if (q->exp < p->exp) {
s->coe = q->coe; //如果此时q指向的结点系数较大,复制该结点
s->exp = q->exp;
r->next = s; //将s接到尾指针后面
r = r->next; //尾指针后移
q = q->next; //q继续向后扫描L2
}
else {
s->coe = p->coe + q->coe; //如果两个值相等,将两个结点的系数相加
s->exp = p->exp;
r->next = s; //将s接到尾指针后面
r = r->next; //尾指针后移
p = p->next; //p继续向后扫描L1
q = q->next; //q继续向后扫描L2
}
}
//当p不为空时,将剩余结点接到尾指针后面
while (p) {
s = (LinkNode*)malloc(sizeof(LNode));
//如果申请失败继续申请,直到申请成功,因为malloc可能申请失败
while (!s) {
s = (LinkNode*)malloc(sizeof(LNode));
}
s->coe = p->coe; //复制结点的值
s->exp = p->exp;
r->next = s; //将s接到尾指针后面
r = r->next; //尾指针后移
p = p->next; //p继续向后扫描L1
}
//当q不为空时,将剩余结点接到尾指针后面
while (q) {
s = (LinkNode*)malloc(sizeof(LNode));
//如果申请失败继续申请,直到申请成功,因为malloc可能申请失败
while (!s) {
s = (LinkNode*)malloc(sizeof(LNode));
}
s->coe = q->coe; //复制结点的值
s->exp = q->exp;
r->next = s; //将s接到尾指针后面
r = r->next; //尾指针后移
q = q->next; //q继续向后扫描L2
}
r->next = NULL; //此时将尾指针后面置空,单链表L3建成
return true;
}