c语言降序多项式加法,数据结构算法(多项式加法)的C语言完美实现
該樓層疑似違規已被系統折疊?隱藏此樓查看此樓
/*數據結構多項式加法用C語言的完美實現,書43頁?算法?2.23[?數據結構(C語言版)?嚴尉敏主編]*/
/*注釋就不加了,本程序的所有函數書上都有說明*/
/*本程序Tourboc?2.0編譯通過*/
#include?
#include?
#include?
#include?
#define??TURE?1
#define??FALSE?0
#define??OK??1
typedef??int??status;
typedef??struct?{
float??coef;
int????expn;
}term,ElemType;
typedef??struct?LNode?{
ElemType?data;
struct?LNode?*next;
}*link,*Position,NodeType;
typedef??struct?{
link??head,tail;
int??len;
}linklist;
typedef??linklist??polynomial;
Position??Nextpos(linklist?*l,link?p)
{?if(l->head&&p->next)??return(p->next);
else????return??NULL;
}
int?cmp(term?a,term?b)
{?if(a.expn>b.expn)??return?1;
else?if(a.expn==b.expn)??return?0;
else??return?-1;
}
status?Makenode(link?*p,ElemType?e)
{?*p=(link)malloc(sizeof(NodeType));
if(!*p)?{printf("Error,the?memory?is?overflow!\n");exit(FALSE);}
(*p)->data=e;
(*p)->next=NULL;
return??OK;
}
status?Initlist(polynomial?*p)
{?p->head=(link)malloc(sizeof(NodeType));
if(!p->head)??{?printf("Cannot?find?space!");exit(FALSE);}
p->len=0;
p->tail=p->head;
return??OK;
}
status?Insfirst(polynomial?*l,link?*q,link?s)
{?if(l->head&&*q&&s)
{?s->next=(*q)->next;(*q)->next=s;
if(l->tail==*q)?l->tail=s;
l->len++;
}
}
status?LocateElem(linklist?*l,ElemType?e,Position?*q,int?(*cmp)(ElemType,ElemType))
{?link?p1;
int?t;
if(l->head)
{p1=l->head;*q=p1->next;t=(*cmp)((*q)->data,e);
while(*q&&t)?{?p1=*q;*q=(*q)->next;}
if(*q&&t)???return??OK;
else
{*q=p1;?return??FALSE;}
}
else??return?FALSE;
}
status?Delfirst(polynomial?*l,link?*h,link?*q)
{?if((*h)->next)
{?*q=(*h)->next;(*h)->next=(*q)->next;
if(l->tail==*q)?l->tail=l->head;
l->len--;
}
}
status?Append(linklist?*l,link?s)
{?if(l->head&&s)
{if(l->tail!=l->head)??l->tail->next=s;
else?l->head->next=s;
l->tail=s;
l->len++;
}
}
void?Creatpolyn(polynomial?**p,int?m)
{?ElemType?e;
link?h,q,s;
int?i;
Initlist(*p);
h=(*p)->head;
e.coef=0.0;e.expn=-1;h->data=e;h->next=NULL;
printf("Input?Coef?and?Expn:\n");
for(i=1;i<=m;i++)
{scanf("%f%d",&e.coef,&e.expn);
if(!LocateElem(*p,e,&q,cmp))
if(Makenode(&s,e))???Insfirst(*p,&q,s);
}
}
void?Addpolyn(polynomial?**pa,polynomial?**pb)
{?link?ha,hb,qa,qb;
ElemType?a,b;
int?sum;
ha=(*pa)->head;hb=(*pb)->head;
qa=Nextpos(*pa,ha);qb=Nextpos(*pb,hb);
while(qa&&qb)
{?a=qa->data;b=qb->data;
switch(cmp(a,b))
{case??-1:
ha=qa;qa=Nextpos(*pa,qa);break;
case??0:
sum=a.coef+b.coef;
if(sum!=0.0)
{qa->data.coef=sum;
ha=qa;
}
else
{Delfirst(*pa,&ha,&qa);free(qa);
}
Delfirst(*pa,&hb,&qb);free(qb);qb=Nextpos(*pb,hb);
qa=Nextpos(*pa,ha);break;
case??1:
Delfirst(*pa,&hb,&qb);Insfirst(*pa,&ha,qb);
qb=Nextpos(*pb,hb);ha=Nextpos(*pa,ha);break;
}
}
if((*pb)->head!=(*pb)->tail)?Append(*pa,qb);
free(hb);
}
void?print(polynomial?*l)
{?link?p;
p=l->head;
p=p->next;
printf("\n");
while(p)
{?printf("%f?X^%d",p->data.coef,p->data.expn);
p=p->next;
if(p)?printf("+");
}
printf("\n");
}
main()
{polynomial?*p1,*p2;
link?a1,a2;
int?n1,n2;
printf("\nInput?the?length?of?polynomial?L1(n1):\n");
scanf("%d",&n1);
Creatpolyn(&p1,n1);
printf("\nThe?Polynomial?L1:\n");
print(p1);
printf("\nInput?the?length?of?polynomial?L2(n2):\n");
scanf("%d",&n2);
Creatpolyn(&p2,n2);
printf("\nThe?Polynomial?L2:\n");
print(p2);
Addpolyn(&p1,&p2);
printf("\nPolynomial?L1?+?Polynomial?L2:\n");
print(p1);
}
——Djl23?于05年10月9日
后面的算法源代碼將陸續公布�
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的c语言降序多项式加法,数据结构算法(多项式加法)的C语言完美实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: navicat mysql 远程_Nav
- 下一篇: mac 串口调试工具_MACamp;串口