#include<stdio.h> #include<conio.h> #include<stdlib.h> #define NULL 0 void main() { struct poly { int coff; int expo; struct poly *link; }*temp1,*start1,*temp2,*start2,*start3,*temp3; int n,i,z=1,num; char c; clrscr(); start1=NULL; printf("\tPOLYNOMIAL CREATION IN LINK LIST AND SUBTRACTION\n\n"); while(1) { if(start1==NULL) { temp1=(struct poly*)malloc(sizeof(struct poly)); printf("Enter Coefficient and Exponent for Node %d\n",z); scanf("%d %d",&temp1->coff,&temp1->expo); start1=temp1; temp1->link=NULL; } else { temp1->link=(struct poly*)malloc(sizeof(struct poly)); temp1=temp1->link; printf("Enter Coefficient and exponent for Node %d\n",z); scanf("%d %d",&temp1->coff,&temp1->expo); } z++; printf("Do you want to create another node\n"); fflush(stdin); scanf("%c",&c); if(c!='y') { temp1->link=NULL; break; } } start2=NULL; temp2=NULL; z=1; while(1) { if(start2==NULL) { temp2=(struct poly*)malloc(sizeof(struct poly)); printf("Enter Coefficient and exponent for Node %d\n",z); scanf("%d %d",&temp2->coff,&temp2->expo); start2=temp2; temp2->link=NULL; } else { temp2->link=(struct poly*)malloc(sizeof(struct poly)); temp2=temp2->link; printf("Enter Coefficient and exponent for Node %d\n",z); scanf("%d %d",&temp2->coff,&temp2->expo); } z++; printf("Do you want to create another node\n"); fflush(stdin); scanf("%c",&c); if(c!='y') { temp2->link=NULL; break; } } // TARVERSING temp1=NULL; temp2=NULL; temp1=start1; temp2=start2; printf("Traversal of Polynomial Linked List 1\n"); while(temp1!=NULL) { printf("%2dx^%d-",temp1->coff,temp1->expo); temp1=temp1->link; } printf("\b \b"); printf("\nTraversal of Polynomial Linked List 2\n"); while(temp2!=NULL) { printf("%2dx^%d -",temp2->coff,temp2->expo); temp2=temp2->link; } printf("\b \b"); // subtraction temp1=NULL; temp2=NULL; temp1=start1; temp2=start2; temp3=NULL; start3=NULL; while(1) { if((temp1!=NULL)||(temp2!=NULL)) { if(start3==NULL) { if((temp1->expo)==(temp2->expo)) { temp3=(struct poly*)malloc(sizeof(struct poly)); temp3->coff= (temp1->coff) - (temp2->coff); temp3->expo=temp1->expo; start3=temp3; temp1=temp1->link; temp2=temp2->link; temp3->link=NULL; } else if(temp1->expo>temp2->expo) { temp3=(struct poly*)malloc(sizeof(struct poly)); temp3->coff=temp1->coff; temp3->expo=temp1->expo; start3=temp3; temp1=temp1->link; temp3->link=NULL; } else { temp3=(struct poly*)malloc(sizeof(struct poly)); temp3->coff=temp2->coff; temp3->expo=temp2->expo; start3=temp3; temp2=temp2->link; temp3->link=NULL; } } else { if(temp1->expo==temp2->expo) { temp3->link=(struct poly*)malloc(sizeof(struct poly)); temp3=temp3->link; temp3->coff= (temp1->coff) - (temp2->coff); temp3->expo=temp1->expo; temp1=temp1->link; temp2=temp2->link; } else if(temp1->expo>temp2->expo) { temp3->link=(struct poly*)malloc(sizeof(struct poly)); temp3=temp3->link; temp3->coff=temp1->coff; temp3->expo=temp1->expo; temp1=temp1->link; } else { temp3->link=(struct poly*)malloc(sizeof(struct poly)); temp3=temp3->link; temp3->coff=temp2->coff; temp3->expo=temp2->expo; temp2=temp2->link; } } } else break; } temp3->link=NULL; //traversing temp3 temp3=NULL; temp3=start3; printf("\nTraversal of Polynomial Linked List after subtracting Temp1 & Temp2\n"); while(temp3!=NULL) { printf("%2dx^%d - ",temp3->coff,temp3->expo); temp3=temp3->link; } printf("\b\b"); printf("\nTHE END\n"); getch(); } OUTPUT: POLYNOMIAL CREATION IN LINK LIST AND SUBTRACTION Enter Coefficient and exponent for Node 1 6 3 Do you want to create another node Y Enter Coefficient and exponent for Node 2 8 2 Do u want to create another node N Enter Coefficient and exponent for Node 1 7 3 Do you want to create another node Y Enter Coefficient and exponent for Node 2 5 2 Do u want to create another node N Traversal of Polynomial Linked List 1 6x^3 + 8x^2 Traversal of Polynomial Linked List 2 7x^3 + 5x^2 Traversal of Polynomial Linked List after adding Temp1 & Temp2 -1X^3 + 3X^2 THE END Page 2
What is Polynomial in Mathematics ? Polynomial comes from poly (meaning “many”) and nomial (in this case meaning “term”) so it says “many terms” In mathematics, a polynomial is an expression consisting of variables and coefficients, that involves only the operations of addition, subtraction, multiplication, and non-negative integer exponents of variables. An example of a polynomial of a single indeterminate, x, is x2 − 4x + 7. A polynomial can have: Possible operations on Polynomials Except A polynomial can have constants, variables and exponents, but never division by a variable. Read more on Linked List…
Given two polynomial numbers represented by a linked list. Write a function that add these lists means add the coefficients who have same variable powers.
Output 1st Number: 5x^2+4x^1+2x^0 2nd Number: -5x^1-5x^0 Added polynomial: 5x^2-1x^1-3x^0 Time Complexity: O(m + n) where m and n are number of nodes in first and second lists respectively. Recursive Method : Algorithm :
Output Linked List 5x^2 4x^1 Linked List 6x^2 4x^1 Addition: 11x^2 8x^1 Time Complexity: O(m + n) where m and n are number of nodes in first and second lists respectively. Related Article: Add two polynomial numbers using Arrays Practice Tags : |