0% found this document useful (0 votes)
177 views5 pages

Application of Linked List-Polynomial Manipulation

This document describes using linked lists to represent and manipulate polynomials. It defines a Node struct to store coefficient and power pairs. Functions are provided to create nodes, add two polynomials by traversing the lists and summing coefficients of matching powers, and display a linked list polynomial. The main function demonstrates creating two sample polynomials as linked lists, adding them using polyadd, and printing the result.

Uploaded by

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

Application of Linked List-Polynomial Manipulation

This document describes using linked lists to represent and manipulate polynomials. It defines a Node struct to store coefficient and power pairs. Functions are provided to create nodes, add two polynomials by traversing the lists and summing coefficients of matching powers, and display a linked list polynomial. The main function demonstrates creating two sample polynomials as linked lists, adding them using polyadd, and printing the result.

Uploaded by

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

APPLICATION OF LINKED LIST-POLYNOMIAL

MANIPULATION
struct Node
{
int coeff;
int pow;
struct Node *next;
};

// Function to create new node


void create_node(int x, int y, struct Node **temp)
{
struct Node *r, *z;
z = *temp;
if(z == NULL)
{
r =(struct Node*)malloc(sizeof(struct Node));
r->coeff = x;
r->pow = y;
*temp = r;
r->next = (struct Node*)malloc(sizeof(struct Node));
r = r->next;
r->next = NULL;
}
else
{
r->coeff = x;
r->pow = y;
r->next = (struct Node*)malloc(sizeof(struct Node));
APPLICATION OF LINKED LIST-POLYNOMIAL
MANIPULATION
r = r->next;
r->next = NULL;
}
}

// Function Adding two polynomial numbers


void polyadd(struct Node *poly1, struct Node *poly2,
struct Node *poly)
{
while(poly1->next && poly2->next)
{
if(poly1->pow > poly2->pow)
{
poly->pow = poly1->pow;
poly->coeff = poly1->coeff;
poly1 = poly1->next;
}
else if(poly1->pow < poly2->pow)
{
poly->pow = poly2->pow;
poly->coeff = poly2->coeff;
poly2 = poly2->next;
}

else
{
poly->pow = poly1->pow;
APPLICATION OF LINKED LIST-POLYNOMIAL
MANIPULATION
poly->coeff = poly1->coeff+poly2->coeff;
poly1 = poly1->next;
poly2 = poly2->next;
}

// Dynamically create new node


poly->next = (struct Node *)malloc(sizeof(struct
Node));
poly = poly->next;
poly->next = NULL;
}
while(poly1->next || poly2->next)
{
if(poly1->next)
{
poly->pow = poly1->pow;
poly->coeff = poly1->coeff;
poly1 = poly1->next;
}
if(poly2->next)
{
poly->pow = poly2->pow;
poly->coeff = poly2->coeff;
poly2 = poly2->next;
}
poly->next = (struct Node *)malloc(sizeof(struct
Node));
APPLICATION OF LINKED LIST-POLYNOMIAL
MANIPULATION
poly = poly->next;
poly->next = NULL;
}
}

// Display Linked list


void show(struct Node *node)
{
while(node->next != NULL)
{
printf("%dx^%d", node->coeff, node->pow);
node = node->next;
if(node->next != NULL)
printf(" + ");
}
}

// Driver program
int main()
{
struct Node *poly1 = NULL, *poly2 = NULL, *poly =
NULL;

// Create first list of 5x^2 + 4x^1 + 2x^0


create_node(5,2,&poly1);
create_node(4,1,&poly1);
create_node(2,0,&poly1);
APPLICATION OF LINKED LIST-POLYNOMIAL
MANIPULATION

// Create second list of 5x^1 + 5x^0


create_node(5,1,&poly2);
create_node(5,0,&poly2);

printf("1st Polynomial Equation: ");


show(poly1);

printf("\n2nd Polynomial Equation: ");


show(poly2);

poly = (struct Node *)malloc(sizeof(struct Node));

// Function add two polynomial numbers


polyadd(poly1, poly2, poly);

// Display resultant List


printf("\nAdded polynomial: ");
show(poly);
return 0;
}
Output:
1st Polynomial Equation: 5x^2 + 4x^1 + 2x^0
2nd Polynomial Equation: 5x^1 + 5x^0
Added polynomial: 5x^2 + 9x^1 + 7x^0

You might also like