数据结构一元多项式运算分析

 一元多项式的加法,减法,乘法用到比较多的是插入,删除操作。所以一般选择单链表作为存储结构。每个结点分别有系数,指针作为数据域,以及指针域。
算法实现:
一.一元多项式加法:
1.建立多项式:尾插法建立一元多项式的链表,通过键盘输入多项式的系数和指数,以输入系数0为结束标志,并约定建立一元多项式链表时,总是按指数从小到大的顺序排列。
2.输出多项式:从单链表的第一项开始逐项读出系数和指数,按多项式的形式输出。
3.多项式相加:设La和Lb分别表示两个多项式。Lc表示和多项式。p,q,r分别表示指向单链表的当前项比较指数大小。
(1)若La->exp<Lb->exp,则结点p应是和多项式中的一项,将p复制到r,并使p后移。
(2)若La->exp = Lb->exp,则将两个结点中的系数相加,当和不为0时,La的系数域加上Lb的系数域作为Lc的系数域;若和为0,则和多项式中没有这一项,p,q后移。
(3)若La->exp > Lb->exp,则将结点q复制到Lc中,q后移。

二.一元多项式减法
算法:将减数多项式的所有系数先变为相反数,然后调用多项式相加的函数进行运算。

三.一元多项式乘法
两个多项式相乘,应该是第一个多项式中的每一项分别与第二个多项式相乘,将相乘得到的结果都存在第一个多项式中,再调用合并多项式的函数。我写的多项式相乘用到了两重循环,合并多项式也用到了两重循环。


小结:在这次代码实现中,主要还是基础的一些线性表的操作的练习,还可以进一步改良的地方就是可以在合并多项式这部分与多项式相加写成一个函数,这样可以减少代码量,代码的通用性更高。

附:源代码
#include<stdio.h>
#include<stdlib.h>


typedef struct NODE{
int coef; //系数
int exp; //指数
struct NODE *next;
}polynode,*Polylist;

int output_list(Polylist head);

Polylist Creat_list(){ //创建链表
Polylist head,rear,s;
head = (Polylist)malloc(sizeof(polynode));
rear = head;

while(1){
printf("input the coef,exp:\n");
s = (Polylist)malloc(sizeof(polynode));
scanf("%d", &s->coef);
if(!s->coef){
free(s);
break;
}
scanf("%d", &s->exp);
rear->next = s;
rear = s;
}
rear->next = NULL;
return (head);
}


Polylist add_list(Polylist head1,Polylist head2){ //多项式加法
Polylist p, q, s, head, tail; 
p = head1->next;
q = head2->next;
tail = head = (Polylist)malloc(sizeof(polynode));
while(p && q){
s = (Polylist)malloc(sizeof(polynode));
if(p->exp < q->exp){ //当P的指数小于Q的指数时
s->coef = p->coef;
s->exp = p->exp;
tail->next = s; 
tail = s;
tail ->next = NULL;
p = p->next;

}
else if(p->exp == q->exp){ //当指数相等时
s->coef = p->coef + q->coef;
s->exp = p->exp;
if (!s->coef){
free(s);
}
else{
tail->next = s; 
tail = s;
tail ->next = NULL;
}
p = p->next;
q = q->next;
}

else{
s->coef = q->coef;
s->exp = q->exp;
tail->next = s; 
tail = s;
tail ->next = NULL; 
q = q->next;
}
}
if(p)
tail->next = p;
else
tail->next = q;
return head;
}


Polylist sub_list(Polylist head1,Polylist head2){ //多项式减法
Polylist p ,head;
p = head2->next;
do{
p->coef = -p->coef;
p = p->next;
}while(p);
head =add_list(head1,head2);
return head; 
}



Polylist polyadd_list(Polylist head){ //合并多项式
Polylist p,q,m;
p = m = head->next;
while(p ->next ){
m = p;
q = p->next;
while(q){
if(q->exp == p->exp){ 
p->coef += q->coef;
m->next = q->next;
free(q);
q = m->next;
}
else{
m = q;
q = q->next;
}
}
p = p->next;
}
return head;
}


Polylist multi_list(Polylist head1,Polylist head2){ //多项式乘法
Polylist head,p,q,r,m;
head = r = (Polylist)malloc(sizeof(polynode));

for(p = head1->next;p != NULL;p = p->next)
{
for(q = head2->next;q != NULL;q = q->next)
{
m = (Polylist)malloc(sizeof(polynode));
m->coef = p->coef * q->coef;
m->exp = p->exp + q->exp;
r->next = m;
r = m;
}
}
r->next = NULL;
output_list(head);
polyadd_list(head);
return head;
}



int output_list(Polylist head){ //输出多项式
Polylist p;
int n = 1;
for(p = head->next;p !=NULL;p = p->next){
printf("第%d项:%d   %d   \n", n++,p->coef,p->exp);
}
return 0;
}

 
int main(){
Polylist head1,head2,head;
head1 = Creat_list();
head2 = Creat_list();

 
printf("多项式加法:\n");
    head =add_list(head1,head2);
output_list(head);

printf("多项式减法:\n");
head = sub_list(head1,head2); 
 output_list(head);
 

printf("多项式乘法:\n");
head = multi_list(head1,head2);
output_list(head);


return 0;
  • 14
    点赞
  • 48
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值