系统粉 > 电脑问答 > 其他问答

已解决求一程序

提问者:不玉则刚123  |  浏览次  |  提问时间:2017-01-10  |  回答数量:1

求一程序谁有数据结构的关于单链表的算法的完整的程序吗?

已有1条答案
囍你爱我225

囍你爱我225

回答数:19  |  被采纳数:51

#include<stdio.h>
#include<stdlib.h>
structpnode//链表的组成
{
float coef;
intexpn;
structpnode*next;
} polynode;
struct pnode *Creat(int n)//创建一链表
{ struct pnode *L,*p,*q;
int i;
q=L=(struct pnode *)malloc(sizeof(polynode));
L->next=NULL;
for(i=0;i<n;i++)
{p=(struct pnode *)malloc(sizeof(polynode));
scanf("%f%d",&p->coef,&p->expn);
p->next=L->next;
L->next=p;
}
return q;}
struct pnode*polyadd(struct pnode *LA,struct pnode *LB)
//实现A多项式加B多项式等于C多项式,并返回C多项式
//的表头指针
{ struct pnode *LC,*pc,*pa,*pb;
floatx;
pa=LA->next;
pb=LB->next ;
pc=(struct pnode *)malloc(sizeof(polynode));
LC=pc;

while (pa&&pb)
{if(pa->expn==pb->expn)//同类项合并
{x=pa->coef+pb->coef;
if(x!=0)
{pc->next=(struct pnode *)malloc(sizeof(polynode));
pc=pc->next;
pc->coef=x;
pc->expn=pa->expn;
}
pa=pa->next;
pb=pb->next;
}
else
{ pc->next=(struct pnode *)malloc(sizeof(polynode));
pc=pc->next;
if (pa->expn<pb->expn)//a的指数小于b的指数
{ pc->coef=pa->coef;
pc->expn=pa->expn;
pa=pa->next;}
else
{ pc->coef=pb->coef;
pc->expn=pb->expn;
pb=pb->next; }
}
}
while(pa)//还乘下 a 多项式
{ pc->next=(struct pnode *)malloc(sizeof(polynode));
pc=pc->next;
pc->coef=pa->coef;
pc->expn=pa->expn;
pa=pa->next;
}
while(pb)//还乘 下b 多项式
{ pc->next=(struct pnode *)malloc(sizeof(polynode));
pc=pc->next;
pc->coef=pb->coef;
pc->expn=pb->expn;
pb=pb->next;
}
pc->next=NULL;
returnLC;
}
struct pnode *print(struct pnode *L)//链表的打印
{
while(L->next->next)
{ L=L->next;
printf("%.2f^%d+",L->coef,L->expn);
}
L=L->next;
printf("%.2f^%d\n",L->coef,L->expn);

return L;
}
void main()
{ struct pnode *PA,*PB,*PC;
int i;
printf("请输入多项式项数");//创建一链表PA
scanf("%d",&i);

PA=Creat(i);

printf("请输入另一多项式项数");//创建一链表PB

scanf("%d",&i);

PB=Creat(i);

PC=polyadd( PA, PB);//得到链表PC

printf("多项式A=");
print(PA);

printf("多项式B=");
print(PB);

printf("运算后多项式=");
print(PC);
}
2017-01-10 15:42:37
赞 1
相关问答
最新其他问答
解决方法