天天看點

多項式加法在c語言中,資料結構算法(多項式加法)的C語言完美實作

該樓層疑似違規已被系統折疊 隐藏此樓檢視此樓

#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日

後面的算法源代碼将陸續公布�