您的位置:首页 > 其它

链表实现多项式相加 相乘

2014-03-26 22:41 330 查看
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();

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: