How to write a C Program to add two polynomials using linked list in C Programming Language ?
Solution:
/*Program to add two polynomials using linked list*/
#include <stdio.h>
#include<stdlib.h>
struct poly* link(struct poly*);
void display(struct poly*);
struct poly* addlink(struct poly*,struct poly*, struct poly*);
struct poly
{
int coeff, pow;
struct poly *ptr;
};
int main()
{
struct poly *head1,*head2,*head3;
int num;
head1=NULL;
head2=NULL;
head3=NULL;
while(1)
{
printf("\nEnter\n1->To create first polynomial function\n2->To create second polynomial function\n3->To display first polynomial function\n4->To display second polynomial function\n5->To add both the polynomial functions\n6->To display the new polynomial created after addition\n");
scanf("%d",&num);
switch(num)
{
case 1: head1=link(head1);
break;
case 2: head2=link(head2);
break;
case 3: display(head1);
break;
case 4: display(head2);
break;
case 5: head3=addlink(head1,head2,head3);
break;
case 6: display(head3);
break;
default: printf("\nBoss ! Enter the correct value\n");
}
}
}
struct poly* link(struct poly *head1)
{
struct poly *newnode;
int value,degree;
newnode=NULL;
printf("\nEnter the coefficient of the polynomial\n");
scanf("%d",&value);
printf("\nEnter the degree for variable x\n");
scanf("%d",°ree);
newnode=(struct poly*) malloc (sizeof (struct poly));
newnode->pow=degree;
newnode->coeff=value;
if(head1==NULL)
{
printf("\nThe first node is created\n");
newnode->ptr=NULL;
}
else
{
printf("\nThe next node is being linked\n");
newnode->ptr=head1;
}
return newnode;
}
void display(struct poly* head)
{
if(head == NULL)
{
printf("\nThe polynomial function is not created.\nPlease create a new polynomial function.\n");
}
while(head != NULL && head->ptr != NULL)
{
printf("The polynomial function has been detected\n");
printf("%dx^%d+",head->coeff,head->pow);
head=head->ptr;
}
if(head->ptr == NULL)
{
printf("\nEntering the last node\n");
printf("%dx^%d",head->coeff,head->pow);
head=head->ptr;
}
}
struct poly* addlink(struct poly* head,struct poly* tail,struct poly *head3 )
{
struct poly *temp1,*temp,*newnode;
temp=newnode=temp1=head3=NULL;
newnode=( struct poly*) malloc(sizeof(struct poly));
if( head == NULL || tail == NULL)
{
printf("\nYou have no node to get added\n");
}
if(head !=NULL && tail == NULL)
{
printf("\nThe existing first polynomial is the final polynomial\n");
return head;
}
if(head == NULL && tail !=NULL)
{
printf("\nThe existing second polynomial is the final polynomial\n");
return tail;
}
head3=newnode;
if(head3==NULL)
{
printf("\nEnter a node first\n");
}
if(head3 != NULL)
{
while(head !=NULL && tail !=NULL)
{
if(head->pow > tail->pow)
{
temp=tail;
temp1=head;
while((head->pow) > (temp->pow))
{
printf("\nThe element's degree in first poly. is greater than the second\n");
if(temp->ptr == NULL)
{
newnode->coeff=temp->coeff;
newnode->pow=temp->pow;
newnode->ptr=NULL;
head3=newnode;
return head3;
temp1=temp1->ptr;
}
temp=temp->ptr;
}
printf("\nYou have come to the end of the tail pointer\n");
}
if(tail->pow > head->pow)
{
temp=head;
temp1=tail;
while((tail->pow) > (temp->pow))
{
printf("\nThe element's degree in second poly. is greater than the first\n");
if(temp->ptr ==NULL)
{
newnode->coeff=temp->coeff;
newnode->pow=temp->pow;
newnode->ptr=NULL;
head3=newnode;
return head3;
temp1=temp1->ptr;
}
temp=temp->ptr;
}
printf("\nYou have come to the end of the tail pointer\n");
}
if(head->pow == tail->pow)
{
temp=head;
temp1=tail;
while((temp->pow == temp1->pow))
{
newnode->coeff = (temp->coeff + tail->coeff);
newnode->pow=temp->pow;
head3=newnode;
return head3;
temp=temp->ptr;
temp1=temp1->ptr;
}
}
}
}
}
Learn More :
Add
- C Program To Add Digits Of Entered Number
- C Program To Add n Numbers
- C program to add first seven terms of the following series using a for loop: (1/1!) + (2/2!) + (3/3!) + .....
- C Program to Adds literal to list of literals
- C Program to Add-Subtract Series 5-10+15
- C Program to Add Series 1+3+5
- C program that lets the user choose if he wants to add a item to the shopping list or print it out
- C Program that prompts the user to input a string, (whitespaces allowed)
- C Program to : add/sub/multi/divi works
- Adding two polynomial functions C Program Using Structure
Data Structure
- C program to remove last of singly linked list and insert it at beginning of list
- C Program To Read A Parenthesised Infix Expression From The User And Check Whether It Is Well Parenthesised Or Not
- C Program To Remove Last Of Singly Linked List And Insert It At Beginning of List
- C Program To Remove First Node Of Singly Linked List And Insert It At End Of The List
- C Program To Read The Adjecancy Matrix of Directed Graph And Convert It Into Adjecancy List
- C Program To Create Two Singly Linked List and Perform Following Operation
- C Program - Hash Table to store information about a student
- Create Two Singly Linked List Perform Differences Display It C Program
- GJK C Program Example-1
- Adding two polynomial functions C Program Using Structure
- Adding Two Polynomial Functions in C Program
Polynomial
Two
- Write a c program to find out H.C.F. of two numbers.
- Write a C program to find maximum or equal between two numbers ?
- ADDITION OF TWO MATRICES USING C PROGRAM
- C Program To Find Product Of Two No Using MACRO
- C Program To Swap Two Numbers Without Using Third Variable
- C Program Concatenating Two Strings Into A Third System
- C Program to concatenate two strings without using string functions
- C Program to Interchanging Two Numbers
- C Program produsul scalar a doi vectori .
- C Program The dot product of two vectors
- C Program to two timing functions to measure process time
- C Program to calculate sum of two m*n matrices & store the result in 3 matrix
- C Program to swap the values of two variables by using call by reference
- Find the union and intersection of two sets of integer store it in two array C Program
- Menu driven program in C to Calculate Length, Copy into Another Compare Concatenate of Two String
- C Program To Multiply Two Polynomials
- Create Two Singly Linked List Perform Differences Display It C Program
- C Program to Input Student Details into a File For Two Subjects
- Adding two polynomial functions C Program Using Structure
- Menu Driven Program to Read Two Integers Find Sum, Difference and Product C Program
- Input Two Numbers and Print Greater Number C Program
Linked List
- C Program to Demonstrates a linked list for numbers.
- C Program to Implementation of List ADT as linked-list
- Pre Order, Post order, In order Implement Binary Tree using linked list
- C program allocates new nodes and creates a four element list with fixed values
- C Program Simple linked list
- Linked List For Getting Employee Details, Display and Search For Salary C Program
- Menu driven program in the creation,display,search, insertion and deletion of a node in the linked list