0% found this document useful (0 votes)
432 views9 pages

Polynomial Manipulation

- Polynomials are represented in memory using linked lists, with each term having a coefficient and power value stored as nodes. - Functions are defined to create, add, subtract, multiply and display polynomials by performing operations on the corresponding nodes of the linked lists representing each polynomial.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
432 views9 pages

Polynomial Manipulation

- Polynomials are represented in memory using linked lists, with each term having a coefficient and power value stored as nodes. - Functions are defined to create, add, subtract, multiply and display polynomials by performing operations on the corresponding nodes of the linked lists representing each polynomial.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Polynomial Manipulation

• Polynomial is represented in the memory using a linked list.


• Every individual term in a polynomial consists of two parts, a coefficient and a power.
• Here, 6, 9, 7, and 1 are the coefficients of the terms that have 3, 2, 1, and 0 as their powers respectively.
• Every term of a polynomial can be represented as a node of the linked list.
• Consider a polynomial
struct node
{
int num;
int coeff;
struct node *next;
}; struct node *start1 = NULL;
struct node *start2 = NULL;
struct node *start3 = NULL;
struct node *start4 = NULL;
struct node *start5 = NULL;
CREATING POLYNOMIAL EXPRESSION

struct node *create_poly(struct node *start) {


{ ptr = start;
struct node *new_node, *ptr; while(ptr -> next != NULL)
int n, c; ptr = ptr -> next;
printf("\n Enter the number : "); new_node = (struct node *)malloc(sizeof(struct
scanf("%d", &n); node));
printf("\t Enter its coefficient : "); new_node -> num = n;
scanf("%d", &c); new_node -> coeff = c;
while(n != –1) new_node -> next = NULL;
{ ptr -> next = new_node;
if(start==NULL) }
{ printf("\n Enter the number : ");
new_node = (struct node *)malloc(sizeof(struct node)); scanf("%d", &n);
new_node -> num = n; if(n == –1)
new_node -> coeff = c; break;
new_node -> next = NULL; printf("\t Enter its coefficient : ");
start = new_node; scanf("%d", &c);
} }
else return start;
}
ADDING TWO POLYNOMIALS else if(ptr1 -> coeff < ptr2 -> coeff)
{
start3 = add_node(start3, ptr2 -> num, ptr2 -> coeff);
struct node *add_poly(struct node *start1, struct node *start2,
ptr2 = ptr2 -> next;
struct node *start3)
} }
{
if(ptr1 == NULL)
struct node *ptr1, *ptr2;
{
int sum_num, c;
while(ptr2 != NULL)
ptr1 = start1, ptr2 = start2; {
while(ptr1 != NULL && ptr2 != NULL)
start3 = add_node(start3, ptr2 -> num, ptr2 -> coeff);
{
ptr2 = ptr2 -> next;
if(ptr1 -> coeff == ptr2 -> coeff) }
{ }
sum_num = ptr1 -> num + ptr2 -> num; if(ptr2 == NULL)
start3 = add_node(start3, sum_num, ptr1 -> coeff); {
ptr1 = ptr1 -> next;
while(ptr1 != NULL)
ptr2 = ptr2 -> next;
{
}
start3 = add_node(start3, ptr1 -> num, ptr1 -> coeff);
else if(ptr1 -> coeff > ptr2 -> coeff)
ptr1 = ptr1 -> next;
{
}
start3 = add_node(start3, ptr1 -> num, ptr1 -> coeff);
}
ptr1 = ptr1 -> next;
return start3;
}
}
SUBTRACTING TWO POLYNOMIALS else if(ptr1 -> coeff < ptr2 -> coeff)
{
start4 = add_node(start4, ptr2 -> num, ptr2 -> coeff);
struct node *sub_poly(struct node *start1, struct node *start2, ptr2 = ptr2 -> next;
struct node *start4) }
{ }while(ptr1 != NULL || ptr2 != NULL);
struct node *ptr1, *ptr2; if(ptr1 == NULL)
int sub_num, c; {
ptr1 = start1, ptr2 = start2; while(ptr2 != NULL)
do {
{ start4 = add_node(start4, ptr2 -> num, ptr2 -> coeff);
if(ptr1 -> coeff == ptr2 -> coeff) ptr2 = ptr2 -> next;
{ }
sub_num = ptr1 -> num – ptr2 -> num; }
start4 = add_node(start4, sub_num, ptr1 -> coeff); if(ptr2 == NULL)
ptr1 = ptr1 -> next; {
ptr2 = ptr2 -> next; while(ptr1 != NULL)
} {
else if(ptr1 -> coeff > ptr2 -> coeff) start4 = add_node(start4, ptr1 -> num, ptr1 -> coeff);
{ ptr1 = ptr1 -> next;
start4 = add_node(start4, ptr1 -> num, ptr1 -> coeff); }
ptr1 = ptr1 -> next; }
} return start4;
}
ADDING NODE

else
{
struct node *add_node(struct node *start, int n, ptr = start;
int c) while(ptr -> next != NULL)
ptr = ptr -> next;
{
new_node = (struct node *)malloc(sizeof(struct node));
struct node *ptr, *new_node;
new_node -> num = n;
if(start == NULL) new_node -> coeff = c;
{ new_node -> next = NULL;
new_node = (struct node ptr -> next = new_node;
*)malloc(sizeof(struct node)); }
new_node -> num = n; return start;
new_node -> coeff = c; }
new_node -> next = NULL;
start = new_node;
}
struct node *mul_poly(struct node *start1, struct node *start2,
MULTIPLY TWO POLYNOMIALS struct node *start5)
{
struct node *ptr1, *ptr2;
int res1, res2;
ptr1 = start1,
ptr2 = start2;
while(ptr1!=null)
{
while(ptr2!=null)
{
res1=ptr1->num*ptr2->num;
res2=ptr1->coeff+ptr2->coeff;
start5=add_node(start5,res1,res2);
ptr2=ptr2->next;
}
ptr1=ptr1->next;
}
return start5;
}
DISPLAY POLYNOMIALS

struct node *display_poly(struct node


*start)
{
struct node *ptr;
ptr = start;
while(ptr != NULL)
{
printf("\n%d x %d\t", ptr -> num, ptr ->
coeff);
ptr = ptr -> next;
}
return start;
}

You might also like