#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
float coef;
int expn;
struct node *next;
}node,*linklist;
linklist L1,L2,head;
int num;
void create_linklist(linklist *S)
{
int i=0;
linklist p;
float data;
head=NULL;
printf("\nplease input nodes' coef (group by expn(from 0 ~%d)):\n",num-1);
while(i<num)
{
p=(node *)malloc(num*sizeof(node));
if(!p)
{
printf("overflow!");
exit(0);
}
p->next=head;
head=p;
scanf("%f",&data);
head->coef=data;
head->expn=i;
i++;
}
*S=head;
printf("\nhaving created a linklist:\n");
i=0;
p=*S;
printf("%d",num);
while(p!=NULL)
{
printf(" %f",p->coef);
p=p->next;
}
}
void computer_linklist()
{
linklist p1,p2;
float data;
printf("\nhow many expns of the expression do you want to set up?:");
scanf("%d",&num);
printf("\nstart to create linklist L1:");
create_linklist(&L1);
printf("\nstart to create linklise L2:");
create_linklist(&L2);
p1=L1;
p2=L2;
printf("\nthe L1+L2's result is:");
printf("%d",num);
while(p2!=NULL&&p1!=NULL)
{
data=p2->coef+p1->coef;
p1=p1->next;
p2=p2->next;
printf(" %f",data);
}
printf("\nthe L1-L2's result is:");
p1=L1;
p2=L2;
printf("%d",num);
while(p2!=NULL&&p1!=NULL)
{
data=p2->coef-p1->coef;
p1=p1->next;
p2=p2->next;
printf(" %f",data);
}
getch();
}
main()
{
clrscr();
computer_linklist();
}
正文
利用链表对多项式运算2005-05-07 22:48:00
【评论】 【打印】 【字体:大 中 小】 本文链接:http://blog.pfan.cn/jay0518/882.html
阅读(4294) | 评论(0)
版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!
评论