Created
October 6, 2016 15:33
-
-
Save priyadarshitathagat/0f3e7e2f0a0aa2e481bcdd1958429ace to your computer and use it in GitHub Desktop.
Addition of polynomials using linked list
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//addition of polynomials | |
#include <stdio.h> | |
#include<stdlib.h> | |
struct node | |
{ | |
int coeff; | |
int exp; | |
struct node *link; | |
}; | |
struct node* getnode( ) | |
{ | |
return ( struct node *)malloc( sizeof( struct node )); | |
}; | |
struct node* acc( struct node *); struct node* add(struct node *,struct node *,struct node *); | |
void display( struct node *); | |
int main() | |
{ | |
struct node *F; struct node *S; struct node *TOT; | |
int ch,x; | |
F = S = TOT = NULL; | |
printf("Enter Elements in 1st list\n"); | |
while( 1 ) | |
{ | |
F = acc(F); | |
printf("To continue enter 1 else enter 0\n"); | |
scanf("%d",&x); | |
if(x!=1) | |
break; | |
} | |
display(F); | |
printf("\nEnter Elements in 2nd list\n"); | |
while( 1 ) | |
{ | |
S = acc(S); | |
printf("To continue enter 1 else enter 0\n"); | |
scanf("%d",&x); | |
if(x!=1) | |
break; | |
} | |
display(S); | |
TOT=add(F,S,TOT); | |
display(TOT); | |
} | |
struct node* add( struct node *X,struct node* Y,struct node* Z) | |
{ struct node* T; | |
if((X==NULL)&&(Y==NULL)) | |
{return(Z);} | |
while((X!=NULL)&&(Y!=NULL)) | |
{ | |
if(Z==NULL) | |
{ Z=getnode(); T=Z; } | |
else | |
{ T->link=getnode(); T=T->link; } | |
if(X->exp<Y->exp) | |
{ | |
T->coeff=Y->coeff; T->exp=Y->exp; Y=Y->link; | |
} | |
else if(X->exp>Y->exp) | |
{ | |
T->coeff=X->coeff; T->exp=X->exp; X=X->link; | |
} | |
else | |
{ | |
if(X->exp==Y->exp) | |
{ | |
T->coeff=X->coeff+Y->coeff; T->exp=X->exp; X=X->link; Y=Y->link; | |
} | |
} | |
} | |
while((X!=NULL)) | |
{ | |
if(Z==NULL) | |
{ Z=getnode(); T=Z; } | |
else | |
{ T->link=getnode(); T=T->link; } | |
T->coeff=X->coeff; T->exp=X->exp; X=X->link; | |
} | |
while((Y!=NULL)) | |
{ | |
if(Z==NULL) | |
{ Z=getnode(); T=Z; } | |
else | |
{ T->link=getnode(); T=T->link; } | |
T->coeff=Y->coeff; T->exp=Y->exp; Y=Y->link; | |
} | |
T->link=NULL; | |
return(Z); | |
} | |
struct node* acc(struct node *FIRST) | |
{ | |
struct node *T; | |
if( FIRST == NULL ) | |
{ | |
FIRST = getnode(); | |
T = FIRST; | |
} | |
else | |
{ | |
T = FIRST; | |
while( T->link != NULL ) T = T->link; | |
T->link = getnode( ); | |
T = T->link; | |
} | |
printf("Enter coeff and exp\n"); | |
scanf("%d%d", &T->coeff,&T->exp); | |
T->link = NULL; | |
return FIRST; | |
} | |
void display( struct node *T) | |
{ | |
printf("The list contains : \n"); | |
while( T!= NULL ) | |
{ | |
printf("%d(X^%d) + ",T->coeff,T->exp); | |
T = T->link; | |
} | |
printf("0\n"); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment