链表实现多项式相加 相乘

csdn太差还是自己操作不对 代码插入就会卡死


#include<stdio.h>

#include<stdlib.h>
#include"string.h"


typedef int elemType;
struct Node
{
elemType coefficient;
int highPower;
Node *next;
}; 


struct List
{
Node *header;
Node *tail;
};


typedef Node* node;
typedef List* list;


//创建一个链表
list createList()
{
list l=(list)malloc(sizeof(List));
node n1=(node)malloc(sizeof(Node));
n1->next=NULL;


l->header=l->tail=n1;
return l;

}
//在链表中寻找次数重复的项,若有,返回所在node,没有则返回NULL
node find(int power,list l)
{
node p=l->header->next;
while(p!=NULL)
{
if (p->highPower==power) 
{
return p;
}
p=p->next;
}
return (node)NULL;
}
//给定一个递减排列链表,返回插入元素应该所在的位置的前一个元素
node getPosition(int power,elemType coeff,list l)
{

node p=l->header->next;
node p1=l->header;

while(p!=NULL)
{

//大于次数
   if (power>p->highPower) {

return p1;
}



p=p->next;
p1=p1->next;
}
return p1;
}
//在链表的某一位置插入某一项,若已存在,则直接合并同类项
void insert(int power,elemType coeff,list l)
{                                                                                                                                                                                                                                                                 
    


node curN;
if (l->header->next==NULL) {
node nod=(node)malloc(sizeof(Node));
nod->coefficient=coeff;
nod->highPower=power;
nod->next=NULL;
l->header->next=nod;
}
else if((curN=find(power,l))!=(node)NULL)
{
curN->coefficient+=coeff;
}
else
{
node pos=getPosition(power,coeff,l);
node nod=(node)malloc(sizeof(Node));
nod->coefficient=coeff;
nod->highPower=power;
nod->next=NULL;
//pos为空值,表明这个元素在末尾,因此用tail来定位
if(pos==NULL)
{
     
l->tail->next=nod;
l->tail=nod;




}
else
{
 
nod->next=pos->next;
pos->next=nod;
}


}


}








void printList(list l)
{
if(l->header->next==NULL)
{
printf("\n是空链表");
getchar();
exit(3000);
}
node p=l->header->next;
while (p!=NULL) {
printf("\n次数:%d   系数:%d",p->highPower,p->coefficient);
p=p->next;
}


}
//将两个多项式相加
list addPoly(list l1,list l2)
{
     //新建一个链表
list l=createList();
node p1=l1->header->next;
node p2=l2->header->next;
     //都不为空
     while(p1&&p2)
{
int coeff;
if(p1->highPower==p2->highPower)
{
coeff=p1->coefficient+p2->coefficient;
insert(p1->highPower,coeff,l);
}
else
{
insert(p1->highPower,p1->coefficient,l);
insert(p2->highPower,p2->coefficient,l);
}
p1=p1->next;
p2=p2->next;
}
//将p1与p2剩下的添加到l中
while(p1)
{
insert(p1->highPower,p1->coefficient,l);
p1=p1->next;
}
while(p2)
{

insert(p2->highPower,p2->coefficient,l);
p2=p2->next;
}
return l;
}
//将两个多项式相乘
list multiply(list l1,list l2)
{
//新建一个链表
list l=createList();
node p1=l1->header->next;
node p2;
while (p1) 
{


p2=l2->header->next;
while(p2)
{
float coeff=p1->coefficient*p2->coefficient;
int power=p1->highPower+p2->highPower;
insert(power,coeff,l);
printf("\ndone");
p2=p2->next;
}
p1=p1->next;

}
return l;


}
//
//测试
int main()
{
   list l1=createList();
   insert(1,4,l1);
   insert(2,6,l1); 
   //
   list l2=createList();
   insert(2,4,l2);
   insert(3,5,l2);
 
   printf("\nl1多项式为");
   printList(l1);
   printf("\nl12多项式为");
   printList(l2);
   list lm=multiply(l1,l2);
   printf("\nlq求积多项式为");
    printList(lm);
   list l=addPoly(l1,l2);
   printf("\nlq求和多项式为");
   printList(l);
  
   
   


getchar();
}



























































































































  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值