當前位置:編程學習大全網 - 編程語言 - C語言數據結構問題

C語言數據結構問題

#include<stdio.h>

typedef struct polynode

{

int coef;

int exp;

struct polynode *next;

}polynode,*polylist;

polylist polycreate()

{

polynode *head,*rear,*s;

int c,e;

head=(polynode *)malloc(sizeof(polynode));

rear=head;

scanf("%d,%d",&c,&e);

while(c!=0)

{

s=(polynode *)malloc(sizeof(polynode));

s->coef=c;

s->exp=e;

rear->next=s;

rear=s;

scanf("%d,%d",&c,&e);

}

rear->next=NULL;

return(head);

}

void polyadd(polylist polya,polylist polyb)

{

polynode *p,*q,*tail,*temp;

int sum;

p=polya->next;

q=polyb->next;

tail=polya;

while(p!=NULL&&q!=NULL)

{

if(p->exp<q->exp)

{tail->next=p;tail=p;p=p->next;}

else if(p->exp==q->exp)

{

sum=p->coef+q->coef;

if(sum!=0)

{

p->coef=sum;

tail->next=p;tail=p;

p=p->next;

temp=q;q=q->next;

free(temp);

}

else{

temp=p;p=p->next;free(temp);

temp=q;q=q->next;free(temp);

}

}

else{

tail->next=q;tail=q;

q=q->next;

}

}

if(p!=NULL) tail->next=p;

else if(q!=NULL) tail->next=q;

}

main()

{

int m;

polynode *p;

polylist la,lb;

printf("when input the data,please by count.\n");

printf("choose an operation(1.add or 2.minus)");

scanf("%d",&m);

if(m==1)

{

printf("Input first list(want to over,input 0)(coef,exp):\n");

la=polycreate();

printf("Input second list(wanto to over,input 0)(coef,exp):\n");

lb=polycreate();

polyadd(la,lb);

p=la->next;

printf("the result is:\n");

printf("%dX(%d)",p->coef,p->exp);

p=p->next;

while(p!=NULL)

{

printf("%+dX(%d)",p->coef,p->exp);

p=p->next;

}

}

if(m==2)

{

printf("input first list(want to over,input 0)(coef,exp):\n");

la=polycreate();

printf("input second list(want to over,input 0)(coef,exp):\n");

lb=polycreate();

p=lb;

while(p!=NULL)

{

p->coef=(-p->coef);

p=p->next;

}

polyadd(la,lb);

p=la->next;

printf("the result is:\n");

printf("%dX(%d)",p->coef,p->exp);

p=p->next;

while(p!=NULL)

{

printf("%+dX(%d)",p->coef,p->exp);

p=p->next;

}

}

}

  • 上一篇:減負不是減少應管教育,應該是什麽?
  • 下一篇:能不能給我講壹個笑話
  • copyright 2024編程學習大全網