Question: (in C programing language) please help me with my code I have this code is not complete plus it doesn't work well and it break

(in C programing language) please help me with my code I have this code is not complete plus it doesn't work well and it break in middle i have no idea why //-------- THE CODE IS : //------ #include
struct Node
{
int coeff;
int pow;
struct Node *next;
};
// Function to create new node
void create_node(int x, int y, struct Node **temp)
{
struct Node *r, *z;
z = *temp;
if(z == NULL)
{
r =(struct Node*)malloc(sizeof(struct Node));
r->coeff = x;
r->pow = y;
*temp = r;
r->next = (struct Node*)malloc(sizeof(struct Node));
r = r->next;
r->next = NULL;
}
else
{
r->coeff = x;
r->pow = y;
r->next = (struct Node*)malloc(sizeof(struct Node));
r = r->next;
r->next = NULL;
}
}
//Answer 1 (c)
// Function Adding two polynomial numbers
void polyadd(struct Node *poly1, struct Node *poly2, struct Node *poly)
{
while(poly1->next && poly2->next)
{
// If power of 1st polynomial is greater then 2nd, then store 1st as it is
// and move its pointer
if(poly1->pow > poly2->pow)
{
poly->pow = poly1->pow;
poly->coeff = poly1->coeff;
poly1 = poly1->next;
}
// If power of 2nd polynomial is greater then 1st, then store 2nd as it is
// and move its pointer
else if(poly1->pow pow)
{
poly->pow = poly2->pow;
poly->coeff = poly2->coeff;
poly2 = poly2->next;
}
// If power of both polynomial numbers is same then add their coefficients
else
{
poly->pow = poly1->pow;
poly->coeff = poly1->coeff+poly2->coeff;
poly1 = poly1->next;
poly2 = poly2->next;
}
// Dynamically create new node
poly->next = (struct Node *)malloc(sizeof(struct Node));
poly = poly->next;
poly->next = NULL;
}
while(poly1->next || poly2->next)
{
if(poly1->next)
{
poly->pow = poly1->pow;
poly->coeff = poly1->coeff;
poly1 = poly1->next;
}
if(poly2->next)
{
poly->pow = poly2->pow;
poly->coeff = poly2->coeff;
poly2 = poly2->next;
}
poly->next = (struct Node *)malloc(sizeof(struct Node));
poly = poly->next;
poly->next = NULL;
}
}
// Print linklist in polynomial form
void show(struct Node *node)
{
while(node->next != NULL)
{
if(node->pow>1)
printf("%dx^%d", node->coeff, node->pow);
else if(node->pow==1)
printf("%dx", node->coeff);
else
printf("%d", node->coeff);
node = node->next;
if(node->next != NULL)
printf(" + ");
}
}
//Answer 1 (b)
//creating new polynomial till exponential is not negative
void create_new_node(struct Node **temp){
int n,m; printf (" enter your poly");
scanf("%d%d",&n,&m);
while(m>=0){
create_node(n,m,*temp);
}
}
// Driver program
int main()
{
struct Node *poly1 = NULL, *poly2 = NULL, *poly = NULL;
// Create first polynomial
create_new_node(&poly1);
// Create second polynomial
create_new_node(&poly2);
printf("1st Number: ");
show(poly1);
printf(" 2nd Number: ");
show(poly2);
poly = (struct Node *)malloc(sizeof(struct Node));
// Function add two polynomial numbers
polyadd(poly1, poly2, poly);
// Display resultant List
printf(" Added polynomial: ");
show(poly);
return 0;
}
Consider a linked list to store a polynomial, that is, every node of the linked list hasa coefficient, exponent and pointer to the next node in the list. a) Define a structure for a node of such a list. b) Write a function to read a polynomial from the user and store the elements in decreasing order according to their exponents. Input should stop when a negative exponent is given. c) Write a function to add two such polynomials. The function should accept pointers to the two polynomials as arguments and return the pointer to the resulting polynomial. Assume that the polynomials passed to the function are in decreasing order according to their exponents d) Write a function to print the linked list in polynomial form. (Use A to signify power, for example: 3x3 +6x2+1 would be printed as 3x A3+6x 2+1) e) Write a main program to input two polynomials from the user, find the result of the sum of the two polynomials, then print the two polynomials and their sum on the sereen
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
