C LINKED LIST
#include <stdlib.h>
#include <stdio.h>
//Struct for a polynomial term (double linked list object)
typedef struct Polynomial t_Polynomial;
typedef struct Term
{
// coefficient of the polynomial term associated with the
object
int coef;
// power of the polynomial term associated with the object
int power;
// pointer to the next object (to facilitate a doubly linked
list)
struct Term *next;
// pointer to the previous object (to facilitate a doubly linked
list)
struct Term *prev;
// pointer to the Doubly Linked List this object is in (so we don't
lose it)
struct Polynomial *parent;
} t_Term;
// Struct for the polynomial (double linked list, list)
typedef struct Polynomial
{
// number of items in the list
int numTerms;
// pointer to the first term in the list
struct Term *head;
// pointer to the last term in the list
struct Term *tail;
} t_Polynomial;
COMPLETE THE BELOW FUNCTION- C only
//Add the given term to the given polynomial.
int
Add_Terml(t_Polynomial *poly, t_Term *term)
{
// input -> ((2x^2 + 5x^4), 3x^3)
// output should be -> 2x^2 + 3x^3 + 5x^4
//OR
// input -> ((2x^2 + 5x^4), 3x^2)
// output should be -> 5x^2 + 5x^4
return 0;
}
Below is the function asked, if the power exists already, the coefficients are added. Else a new term is added to the end of the polynomial.
int Add_Terml(t_Polynomial *poly, t_Term *term){
struct Term *temp = poly->head;
int flag=0;
while(temp!=NULL){
if(temp->power==term->power){
temp->coef+=term->coef;
flag=1;
break;
}
}
if(flag==0){
temp=poly->tail;
temp->next=term;
}
return 0;
}
Get Answers For Free
Most questions answered within 1 hours.