Thursday, 1 September 2022

POLYNOMIAL ADDITION USING LINKED LIST

 IMPLEMENTATION OF POLYNOMIAL ADDITION USING LINKED LIST

Exercise.No:5 

TO LEARN C PROGRAMMING FOLLOW THIS YOUTUBE CHANNEL : 

NOTE : 

        In the below content incase of some error problem the program was slightly modified, so to remodel it the following steps should be taken 

            1.)In "co eff" remove the space between "co" and "eff".

            2.)In "print f" remove the space between "print" and "f".

            3.)In "scan f" remove the space between "scan" and "f".

            4.)In "std io. h" remove the space between "std" and "io" then space between "io." and "h".

            5.)In "con io. h" remove the space between "con" and "io" then space between "io." and "h".

            6.)In "p add" remove the space between "p" and "add".

            7.)In "size of" remove the space between "size" and "of".

    * For any queries comment below ,instantly the solution will be posted*

AIM: 

To implement the program for polynomial addition using linked List. 

ALGORITHM: 

1. Using the function poly1() read the coefficient and exponent terms of the first polynomial 

 until exponent term is zero 

2. Using the function poly2() read the coefficient and exponent terms of the second 

 polynomial until exponent term is zero 

3. Using the function polyadd() add the two polynomials with the following comparisons 

4. If the exponent term in the first polynomial is greater than the exponent in the second 

 polynomial, add the node of the first polynomial with the resultant polynomial 

5. If the exponent term in the first polynomial is lees than the exponent in the second 

 polynomial, add the node of the second polynomial with the resultant polynomial 

6. If the exponent term in the first polynomial is equal to the exponent in the second 

 polynomial, add both the coefficient of the first and second polynomial and the node to the 

 resultant polynomial 

7. Traverse both the polynomial according to the above comparison up to the NULL value of 

 both the polynomials are reached. 

8. Display the resultant polynomial 

PROGRAM :

#include <std io. h>

#include <con io. h>

#include <std lib. h>

#include <math. h>

typedef struct node

{ int power;

float co eff;

struct node *next;

}node;

node * insert(node *head, int power, float co eff);

node * create();

node * p add(node *head1,node *head2);

void print(node *head);

int main()

{

node *head1,*head2,*head3;

int op;

float value, x;

print f("\n Enter 1st Polynomial : ");

head1=create();

print(head1);

print f("\n Enter 2nd Polynomial : ");

head2=create();

print(head2);

head3=p add(head1,head2); 

print f("\n Addition of two polynomials : ");

print(head3);

}

node * insert(node *head, int power, float co eff)

{ node *p,*q;

p=(node*)malloc(size of(node));

p->power=power; p->co eff=co eff;

p->next=NULL;

if(head==NULL)

return(p);

else

if(power<head->power)

{ p->next=head;

return(p);

}

else

{ q=head;

while(q->next!=NULL && power>=q->next->power)

q=q->next;

p->next=q->next;

q->next=p;

if(q->power==p->power)

{

q->co eff=q->co eff +p->co eff;

q->next=p->next;

free(p);

}

return(head);

}

}

node * create()

{

int n, I, power;

float co eff;

node *head;

head=NULL;

print f("\n Enter No. of Terms:");

scan f("%d", &n);

print f("\n enter a term as a tuple of (power, coefficient)");

for(I=1;I<=n; I++)

{

scan f("%d %f", &power, &co eff);

head=insert(head, power, co eff);

}

return(head);

}

node * p add(node *head1,node *head2)

{

node *head=NULL;

int power; float co eff;

while(head1 != NULL && head2 != NULL)

{

if(head1->power < head2->power)

{

head=insert(head,head1->power,head1->co eff);

head1=head1->next;

continue;

}

if(head2->power < head1->power)

{ head=insert(head,head2->power,head2->co eff);

head2=head2->next;

continue;

}

head=insert(head,head1->power,head1->coeff+head2->co eff);

head1=head1->next;

head2=head2->next;

}

while(head1!=NULL)

{head=insert(head,head1->power,head1->co eff);

head1=head1->next;

}

while(head2!=NULL)

{head=insert(head,head2->power,head2->co eff);

head2=head2->next;

}

return(head);

}

void print(node *head)

{ print f("\n");

while(head!=NULL)

{

print f("%6.2fX^%d ",head->co eff, head->power);

head=head->next;

}}

OUTPUT :

Enter 1st Polynomial :

Enter No. of Terms:3

enter a term as a tuple of (power, coefficient)3 3 2 2 1 1

1.00X^1 2.00X^2 3.00X^3

Enter 2nd Polynomial :

Enter No. of Terms:2

enter a term as a tuple of (power, coefficient)2 2 5 5

2.00X^2 5.00X^5

Addition of two polynomials :

1.00X^1 4.00X^2 3.00X^3 5.00X^5

RESULT: 

Thus the program for polynomial addition using linked list was implemented and it’s 

executed successfully.

            *Let us promote a thing, "Code with us" YOUTUBE channel doing a coding works for cheap price their services are 
                        1.) APP DEVELOPMENT
                        2.)WEB DEVELOPMENT
                        3.)REVERSE ENGINEERING (Penetration testing)
                        4.)TEACHING PROGRAMMING LANGUAGES
           To reach "Code with us" immediately comment below to book your time slot.

No comments:

Post a Comment

FRIENDSHIP & GOALS

WHAT IS FRIENDSHIP:  For all the attention we pay to love stories, some of the most compelling stories (in fiction or not) are about best fr...