0% found this document useful (0 votes)
18 views3 pages

Polynomial Addition Using LL

The document contains C code to implement a polynomial data structure using linked lists and functions to create, print, and add polynomials. It defines a node structure to store coefficient and exponent pairs and uses a head pointer to traverse the linked list. Functions are defined to insert nodes, create a polynomial by user input, print a polynomial, and add two polynomials by traversing both lists and inserting summed coefficient and exponent pairs into a new polynomial.

Uploaded by

Indonesia Server
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views3 pages

Polynomial Addition Using LL

The document contains C code to implement a polynomial data structure using linked lists and functions to create, print, and add polynomials. It defines a node structure to store coefficient and exponent pairs and uses a head pointer to traverse the linked list. Functions are defined to insert nodes, create a polynomial by user input, print a polynomial, and add two polynomials by traversing both lists and inserting summed coefficient and exponent pairs into a new polynomial.

Uploaded by

Indonesia Server
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <stdio.

h>
#include <stdlib.h>

struct node {
float coeff;
int expo;
struct node* link;
};

struct node* insert(struct node* head, float co, int ex)


{
struct node* temp;
struct node* newP = malloc(sizeof(struct node));
newP->coeff = co;
newP->expo = ex;
newP->link = NULL;

//If there is no node in the list OR given exponent is


//than the first node exponent
if(head == NULL || ex > head->expo)
{
newP->link = head;
head = newP;
}
else
{
temp = head;
while(temp->link != NULL && temp->link->expo >= ex)
temp = temp->link;
newP->link = temp->link;
temp->link = newP;
}
return head;
}

struct node* create(struct node* head)


{
int n, i;
float coeff;
int expo;

printf("Enter the number of terms: ");


scanf("%d", &n);

for(i=0; i<n; i++)


{
printf("Enter the coefficient for term %d: ", i+1);
scanf("%f", &coeff);

printf("Enter the exponent for term %d: ", i+1);


scanf("%d", &expo);

head = insert(head, coeff, expo);


}
return head;
}

void print(struct node* head)


{
if(head == NULL)
printf("No Polynomial.");
else {
struct node* temp = head;
while(temp != NULL)
{
printf("(%.1fx^%d)", temp->coeff, temp->expo);
temp = temp->link;
if(temp!=NULL)
printf(" + ");
else printf("\n");
}
}
}

void polyAdd(struct node* head1, struct node* head2)


{
struct node* ptr1 = head1;
struct node* ptr2 = head2;
struct node* head3 = NULL;
while(ptr1!=NULL && ptr2!=NULL)
{
if(ptr1->expo == ptr2->expo)
{
head3 = insert(head3, ptr1->coeff+ptr2->coeff, ptr1->expo);
ptr1 = ptr1->link;
ptr2 = ptr2->link;
}
else if(ptr1->expo > ptr2->expo)
{
head3 = insert(head3, ptr1->coeff, ptr1->expo);
ptr1 = ptr1->link;
}
else if(ptr1->expo < ptr2->expo)
{
head3 = insert(head3, ptr2->coeff, ptr2->expo);
ptr2 = ptr2->link;
}
}

while(ptr1!=NULL)
{
head3 = insert(head3, ptr1->coeff, ptr1->expo);
ptr1 = ptr1->link;
}
while(ptr2!=NULL)
{
head3 = insert(head3, ptr2->coeff, ptr2->expo);
ptr2 = ptr2->link;
}
printf("Added polynomial is: ");
print(head3);
}

int main()
{
struct node* head1 = NULL;
struct node* head2 = NULL;
printf("Enter the First polynomial\n ");
head1 = create(head1);
printf("Enter the second polynomial\n ");
head2 = create(head2);
polyAdd(head1, head2);
return 0;

You might also like