多项式合并
思路
-
多项式合并
P1 = 5 + 2x + 8x ^8 +3x^16
P2 = 6x + 16x^6 - 8x^8
P = P1 + P2 = 5 + 8x + 16x^6 + 3x^16
-
使用带头结点的单向不循环链表
-
每个节点分为三个部分,系数项,指数项,指针域
-
结构体表示为
struct node_st { int exponent; int coefficient; struct node_st *next; };
-
定义两个指针p,q,分别指向多项式P1,P2第一个有效节点
-
在定义一个指针r,保存合并结果,p的前驱指向不确定,r表示p的前驱,r可以指向p,也可以指向q
-
比较p指向节点的指针项与q指向节点的指针项
-
p->exponent == q->exponent
指数项不变,系数项相加
- 若系数之和为零
p = p->next;
q = q->next;-
若系数之和不为零
r->next = p;
r = p;p = p->next;
q = q->next;
-
p->exponent > q->exponent
r->next = q;
r = q;
q = q->next; -
p->exponent < q->exponent
r->next = p;
r= p;
p = p->next;
-
代码
main.c(负责测试)
#include<stdio.h>
#include<stdlib.h>
#include "polynomial.h"
int main()
{
//p1,p2表示多项式
struct node_st *p1 = NULL, *p2 = NULL;
//数组中第一个元素为系数,第二个元素为指数
int arr1[][2] = { {5,0},{3,16},{8,8},{2,1} };
int arr2[][2] = { {16,6},{-8,8},{6,1} };
p1 = poly_create(arr1,4);
if (p1 == NULL)
{
return -1;
}
p2 = poly_create(arr2,3);
if (p2 == NULL)
{
return -1;
}
poly_show(p1);
poly_show(p2);
poly_union(p1, p2);
poly_show(p1);
poly_destroy(p1);
poly_destroy(p2);
return 0;
}
polynomial.c(负责函数定义)
#include<stdio.h>
#include<stdlib.h>
#include "polynomial.h"
struct node_st* poly_create(int arr[][2],int row)
{
struct node_st *ps = NULL,*prenode = NULL,*curnode = NULL;
struct node_st *newnode = NULL;
int i = 0;
//生成头结点
ps = (struct node_st*)malloc(sizeof(struct node_st));
if (ps == NULL)
{
return NULL;
}
ps->next = NULL;
for (i = 0; i < row; i++)
{
prenode = ps;
curnode = ps->next;
//按照指数从小到大的顺序插入有效节点
while ((curnode != NULL) && (curnode->exponent < arr[i][1]))
{
prenode = curnode;
curnode = curnode->next;
}
//生成新节点
newnode = (struct node_st*)malloc(sizeof(struct node_st));
if (newnode == NULL)
{
return NULL;
}
newnode->coeffitient = arr[i][0];
newnode->exponent = arr[i][1];
newnode->next = prenode->next;
prenode->next = newnode;
}
return ps;
}
void poly_show(struct node_st* ps)
{
struct node_st *p = ps->next;
printf("%3s\t%3s\n", "系数", "指数");
while (p)
{
printf("%3d\t%3d\n", p->coeffitient, p->exponent);
p = p->next;
}
}
//多项式合并,p2向p1上合并,p1保存合并后的结果
void poly_union(struct node_st *p1, struct node_st *p2)
{
struct node_st *p = p1->next;
struct node_st *q = p2->next;
struct node_st *r = p1,*temp=NULL;
while (p&&q)
{
if (p->exponent > q->exponent)
{
temp = (struct node_st*)malloc(sizeof(struct node_st));
if (temp == NULL)
{
return;
}
temp->coeffitient = q->coeffitient;
temp->exponent = q->exponent;
temp->next = q->next;
r->next = temp;
r = temp;
q = q->next;
}
else if (p->exponent < q->exponent)
{
r->next = p;
r = p;
p = p->next;
}
else if (p->exponent == q->exponent)
{
p->coeffitient += q->coeffitient;
if (p->coeffitient)
{
r->next = p;
r = p;
}
p = p->next;
q = q->next;
}
}
if (p == NULL)
{
r->next = q;
}
if (q == NULL)
{
r->next = p;
}
}
void poly_destroy(struct node_st* ps)
{
struct node_st *cur = ps->next,*next = NULL;
while (cur)
{
next = cur->next;
free(cur);
cur = next;
}
free(ps);
ps = NULL;
}
polynomial.h(负责函数声明)
#ifndef POLYNOMIAL_H__
#define POLYNOMIAL_H__
struct node_st
{
int coeffitient;
int exponent;
struct node_st *next;
};
struct node_st* poly_create(int arr[][2], int row);
void poly_show(struct node_st* ps);
void poly_destroy(struct node_st* ps);
void poly_union(struct node_st *p1, struct node_st *p2);
#endif