Created
October 29, 2018 19:26
-
-
Save saifsmailbox98/0b8813a7edc42a2ff3fd653d3d8f7ec3 to your computer and use it in GitHub Desktop.
This file contains 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
#include <stdio.h> | |
#include <stdlib.h> | |
#define MALLOC(P, N, T) P=(T *)malloc(N*sizeof(T)) | |
struct node { | |
int c; | |
int e; | |
struct node *link; | |
}; | |
typedef struct node * NODE; | |
NODE read_poly(); | |
NODE insert_front(NODE first, int c, int e); | |
NODE read_poly(){ | |
int n, c, e; | |
NODE first = NULL; | |
printf("Enter the number of terms:"); | |
scanf("%d", &n); | |
for(int i =0; i<n; i++){ | |
printf("Enter c and e:"); | |
scanf("%d %d", &c, &e); | |
first = insert_front(first, c, e); | |
} | |
return first; | |
} | |
NODE insert_front(NODE first, int c, int e){ | |
NODE temp; | |
MALLOC(temp, 1, struct node); | |
temp->c = c; | |
temp->e = e; | |
temp->link = NULL; | |
if(first == NULL) | |
return temp; | |
temp->link = first; | |
first = temp; | |
return first; | |
} | |
void display(NODE first){ | |
while(first!=NULL){ | |
printf("+ %dx^%d ", first->c, first->e); | |
first = first->link; | |
} | |
printf("\n"); | |
} | |
void add_poly(NODE first, NODE second){ | |
if(first == NULL){ | |
display(second); | |
return ; | |
} | |
if(second == NULL){ | |
display(first); | |
return; | |
} | |
if(first->e == second->e){ | |
printf("+ %dx^%d ", first->c + second->c, first->e); | |
add_poly(first->link, second->link); | |
}else if(first->e < second->e){ | |
printf("+ %dx^%d ", first->c, first->e); | |
add_poly(first->link, second); | |
}else{ | |
printf("+ %dx^%d ", second->c, second->e); | |
add_poly(first, second->link); | |
} | |
} | |
int main() | |
{ | |
NODE first; | |
NODE second; | |
first = read_poly(); | |
second = read_poly(); | |
display(first); | |
display(second); | |
add_poly(first, second); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment