Вы находитесь на странице: 1из 5

PROGRAM: #include<stdio.h> #include<conio.h> #include<malloc.

h> struct barbie { int coeff; int pow; struct barbie *link; } *ptr,*start1,*node,*start2,*start3,*ptr1,*ptr2; typedef struct barbie bar; int temp1,temp2; void main() { void create(void); void prnt(void); void sum1(void); void sort(void); clrscr(); printf("\nenter the element of the first poly:"); node=(bar*)malloc(sizeof(bar)); start1=node; if(start1==NULL) { printf("unable to create memory:"); getch(); exit(); } create(); printf("enter the element of second poly"); node=(bar*)malloc(sizeof(bar)); start2=node; if(start2==NULL) { printf("\nunable to create memory"); getch(); exit(); } create(); clrscr(); printf("\nthe elements of poly first one"); ptr=start1; prnt(); printf("\nthe element of the poly second are");

ptr=start2; prnt(); printf("\nthe first sorted list is"); ptr=start1; sort(); ptr=start1; prnt(); printf("\nthe second sorted list is"); ptr=start2; sort(); ptr=start2; prnt(); printf("\nthe sum of the two list are"); sum1(); ptr=start3; prnt(); getch(); } void create() { char ch; while(1) { printf("\nenter the coeff and pow"); scanf("%d%d",&node->coeff,&node->pow); if(node->pow==0) { ptr=node; node=(bar*)malloc(sizeof(bar)); node=NULL; ptr->link=node; break; } printf("do you want to enter more coeff?(y/n)"); fflush(stdin); scanf("%c",&ch); if(ch=='n') { ptr=node; node=(bar*)malloc(sizeof(bar)); node= NULL; ptr->link=node; break; } ptr=node; node=(bar*)malloc(sizeof(bar));

ptr->link=node; } } void prnt() { int i=1; while(ptr!=NULL) { if(i!=1) printf("+"); printf("%dX^%d",ptr->coeff,ptr->pow); ptr=ptr->link; i++; } } void sort() { for(;ptr->coeff!=NULL;ptr=ptr->link) for(ptr2=ptr->link;ptr2->coeff!=NULL;ptr2=ptr2->link) { if(ptr->pow>ptr2->pow) { temp1=ptr->coeff; temp2=ptr->pow; ptr->coeff=ptr2->coeff; ptr->pow=ptr2->pow; ptr2->coeff=temp1; ptr2->pow=temp2; } } } void sum1() { node=(bar*)malloc(sizeof(bar)); start3=node; ptr1=start1; ptr2=start2; while(ptr1!=NULL&&ptr2!=NULL) { ptr=node; if(ptr1->pow>ptr2->pow) { node->coeff=ptr2->coeff; node->pow=ptr2->pow; ptr2=ptr2->link;// Update ptr list b }

else if(ptr1->pow<ptr2->pow) { node->coeff=ptr1->coeff; node->pow=ptr1->pow; ptr1=ptr1->link; } else { node->coeff=ptr2->coeff+ptr1->coeff; node->pow=ptr2->pow; ptr1=ptr1->link; ptr2=ptr2->link; } node=(bar*)malloc(sizeof(bar)); ptr->link=node; } if(ptr1==NULL) { while(ptr2!=NULL) { node->coeff=ptr2->coeff; node->pow=ptr2->pow; ptr2=ptr2->link; ptr=node; node=(bar*)malloc(sizeof(bar)); ptr->link=node; } } else if(ptr2==NULL) { while(ptr1!=NULL) { node->coeff=ptr1->coeff; node->pow=ptr1->pow; ptr1=ptr1->link; ptr=node; node=(bar*)malloc(sizeof(bar)); ptr->link=node; } } node=NULL; ptr->link=node; }

OUTPUT:

enter the element of the first poly: enter the coeff and pow1 2 do you want to enter more coeff?(y/n)y enter the coeff and pow2 4 do you want to enter more coeff?(y/n)y enter the coeff and pow4 5 do you want to enter more coeff?(y/n)n enter the element of second poly enter the coeff and pow7 8 do you want to enter more coeff?(y/n)y enter the coeff and pow5 6 do you want to enter more coeff?(y/n)n the elements of poly first one1X^2+2X^4+4X^5 the element of the poly second are7X^8+5X^6 the first sorted list is1X^2+2X^4+4X^5 the second sorted list is5X^6+7X^8 the sum of the two list are1X^2+2X^4+4X^5+5X^6+7X^8

Вам также может понравиться