Tuesday, January 30, 2024

POLYNOMIALS (Using Linked List)
Implemented using C Language

Polynomial: E.g.:   3x5 - 2x3 + x2 + 4  

The important information about the polynomial is contained in the coefficients and exponents of x; the variable x itself is just a place holder (a dummy variable). We can think of a polynomial as a sum of terms, each of which consists of a coefficient and an exponent. It can be represented as a list of pairs of coefficients and exponents. A linked list is preferable to a contiguous list for representing a polynomial because we may not know the bound on the degree, or only a few non-zero terms may appear.
 

Functionality Implemented in this Program:

  1. typedef struct polynominal
  2. typedef struct polynominallist
  3. poly *createpoly(void)
  4. void addterm(poly *p,float c,int e)
  5. poly *addpoly(poly *p1, poly *p2)
  6. void printpoly(poly *p)
  7. void killpoly(poly *p)

C Language Code:

              POLYNOM.H           

// DSTC -- POLYNOM.H -- polynomial header file ..
// 3x^2 + 3x + 1
// Operations .... create,addition of 2 polynomials...
//1.poly *createpoly(void);

//#include "assert.h"

typedef struct polynominal
{
float coeff ;
int exp;
struct polynominal *next ;
}term ;

typedef struct polynominallist
{
term *first ;
term *last ;
}poly ;

poly *createpoly(void)
{
poly *p;
p = (poly *) malloc (sizeof(poly)) ;
p->first = NULL ;
p->last = NULL ;
return (p) ;
}

void addterm(poly *p,float c,int e)
{
term *nterm,*temp ;
if (c!=0)
{
nterm=(term *)malloc(sizeof(term));
nterm->coeff = c ;
nterm->exp = e ;
nterm->next = NULL ;
}
if (p->first == NULL)
{
p->first = nterm ;
p->last = nterm ;
}
else
{
if (p->last->exp > e)
{
p->last->next = nterm ;
p->last = nterm ;
}
}
}

poly *addpoly(poly *p1, poly *p2)
{
term *t1,*t2 ;
poly *p3;
float sum ;
p3 = createpoly();
t1 = p1->first ;
t2 = p2->first ;

while(t1 && t2)
{
if (t1->exp == t2->exp)
{
// when degree/power of X is same for both then add both and
// store in the next of new poly-list
sum = t1->coeff + t2->coeff ;
addterm(p3,sum,t1->exp) ;
t1 = t1->next ;
t2 = t2->next ;
}
else if(t1->exp > t2->exp)
{
// when, suppose 3x^2 is in poly1 and poly2 is not having power of 2
// then just add the term of p1 into new poly-list
addterm(p3,t1->coeff,t1->exp) ;
t1 = t1->next ;
}
else
{
// when, suppose 3x^2 is in poly2 and poly1 is not having power of 2
// then just add the term of p2 into new poly-list
addterm(p3, t2->coeff,t2->exp) ;
t2=t2->next;
}
}

while (t1)
{
// adding the remaining/extra terms in the p1 into p3
addterm(p3,t1->coeff,t1->exp) ;
t1 = t1->next ;
}

while (t2)
{
// adding the remaining/extra terms in the p2 into p3
addterm(p3,t2->coeff,t2->exp) ;
t2 = t2->next ;
}

return(p3);
}

// prints the polynomial

void printpoly(poly *p)
{
term *temp ;
if (p->first != NULL)
{
temp = p->first;
while(temp)
{
if(temp == p->last)
printf("%fx^%d",temp->coeff,temp->exp) ;
else
printf("%fx^%d + ",temp->coeff,temp->exp) ;

temp = temp->next ;
}
}

}

void killpoly(poly *p)
{
term *save, *temp ;
save = p->first ;
while(save)
{
temp = save->next ;
free(save);
save = temp ;
}
free(p);
}

              POLYNOM.CPP           

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include "polynom.h"

main()
{
poly *p,*p1, *res ;
float c ;
int e ;
p = createpoly() ;
p1 = createpoly() ;

clrscr() ;

printf("\n Enter the 1st Term of Poly in Coeff,Exponent form:" );
scanf("%f,%d", &c,&e) ;

addterm(p,c,e) ;

printf("\n Enter the 2nd Term of Poly in Coeff,Exponent form:" );
scanf("%f,%d", &c,&e) ;

addterm(p,c,e) ;

printf("\n Enter the 3rd Term of Poly in Coeff,Exponent form:") ;
scanf("%f,%d", &c,&e) ;

addterm(p,c,e) ;

printf("\n Print Polynomial ...\n") ;

printpoly(p);
/////////////////

printf("\n Enter the 1st Term of Poly in Coeff,Exponent form:" );
scanf("%f,%d", &c,&e) ;

addterm(p1,c,e) ;

printf("\n Enter the 2nd Term of Poly in Coeff,Exponent form:" );
scanf("%f,%d", &c,&e) ;
addterm(p1,c,e) ;
printf("\n Enter the 3rd Term of Poly in Coeff,Exponent form:") ;
scanf("%f,%d", &c,&e) ;
addterm(p1,c,e) ;
printf("\n Print Polynomial ...\n") ;
printpoly(p1);

res=addpoly(p,p1) ;
printf("\n res \n") ;
printpoly(res) ;

}

No comments: