Adding two polynomials using Circular Linked ListGiven two polynomial numbers represented by a circular linked list, the task is to add these two polynomials by adding the coefficients of the powers of the same variable. Show Note: In given polynomials, the term containing the higher power of x will come first. Examples:
Recommended: Please try your approach on {IDE} first, before moving on to the solution. Approach: Follow the below steps to solve the problem:
Below is the implementation of the above approach: C++
Output
Polynomial 1
5x^2 * y^1 + 4x^1 * y^2 + 3x^1 * y^1 + 2x^1 + 3y^1 + 2
Polynomial 2
3x^1 * y^2 + 4x^1 + 2y^1 + 6
Polynomial after addition
5x^2 * y^1 + 7x^1 * y^2 + 3x^1 * y^1 + 6x^1 + 5y^1 + 8
Time Complexity: O(M + N), where M and N are number of nodes in the first and second lists respectively.
Article Tags :
Linked List Mathematical
circular linked list Linked-List-Polynomial maths-polynomial Practice Tags :
Linked List Mathematical circular linked list Adding two polynomials using Linked ListGiven two polynomial numbers represented by a linked list. Write a function that add these lists means add the coefficients who have same variable powers. Polynomial Addition using Circular Header Linked List#include<stdio.h> #include<stdlib.h> struct node { int coeff; int expon; struct node *link; }; typedef struct node *NODE; NODE getnode() { NODE x; x = (NODE) malloc(sizeof(struct node)); return x; } NODEattach(int coeff, int expon, NODE head) { NODE temp, cur; temp = getnode(); temp->coeff = coeff; temp->expon = expon; cur = head->link; while(cur->link != head) { cur = cur->link; } cur->link = temp; temp->link = head; return head; } NODE read_poly(NODE head) { int i = 1, coeff, expon; printf("\nEnter the coefficient as -999 to end the polynomial "); while(1) { printf("\nEnter the %d term:\n",i++); printf("\n\tCoeff = "); scanf("%d", &coeff); if(coeff == -999) break; printf("\n\tPow x = "); scanf("%d", &expon); head = attach(coeff, expon, head); } return head; } NODEpoly_add(NODE head1, NODE head2, NODE head3) { NODE a,b; int coeff; a = head1->link; b = head2->link; while(a != head1 && b != head2) { if(a->expon == b->expon) { coeff = a->coeff + b->coeff; if(coeff != 0) head3 = attach(coeff, a->expon, head3); a = a->link; b = b->link; } else if(a->expon > b->expon) { head3 = attach(a->coeff, a->expon, head3); a = a->link; } else { head3 = attach(b->coeff, b->expon, head3); b = b->link; } } while(a != head1) { head3 = attach(a->coeff, a->expon, head3); a = a->link; } while(b != head2) { head3 = attach(b->coeff, b->expon, head3); b = b->link; } return head3; } voiddisplay(NODE head) { NODE temp; if(head->link == head) { printf("\nPolynomial does not exist"); return; } temp = head->link; while(temp != head) { printf("%dx^%d",temp->coeff, temp->expon); temp = temp->link; if(temp != head) printf(" + "); } } void main() { NODE head1, head2, head3; head1 = getnode(); head2 = getnode(); head3 = getnode(); head1->link=head1; head2->link=head2; head3->link=head3; printf("\nEnter the first polynomial \n"); head1 = read_poly(head1); printf("\nEnter the second polynomial \n"); head2 = read_poly(head2); head3 = poly_add(head1, head2, head3); printf("\nPolynomial 1:\t"); display(head1); printf("\nPolynomial 2:\t"); display(head2); printf("\nPolynomial Result:\t"); display(head3); } Output: Enter the first polynomial Enter the coefficient as -999 to end the polynomial Enter the 1 term: Coeff = 1 Pow x = 3 Enter the 2 term: Coeff = 1 Pow x = 2 Enter the 3 term: Coeff = 1 Pow x = 1 Enter the 4 term: Coeff = 1 Pow x = 0 Enter the 5 term: Coeff = -999 Enter the second polynomial Enter the coefficient as -999 to end the polynomial Enter the 1 term: Coeff = 7 Pow x = 2 Enter the 2 term: Coeff = 5 Pow x = 1 Enter the 4 term: Coeff = -999 Polynomial 1: 1x^3 + 1x^2 + 1x^1 + 1x^0 Polynomial 2: 7x^2 + 5x^1 Polynomial Result: 1x^3 + 8x^2 + 6x^1 + 1x^0Program to add two polynomials given as linked lists using PythonPythonServer Side ProgrammingProgramming Suppose, we are given two polynomials and we have to find out the addition of the two polynomials. The polynomials have to be represented as linked lists; the terms of the polynomials will be represented as a linked list node. Each linked list node will contain the coefficient value, power value, and the pointer to the next linked list node. We have to return a third linked list which is the addition of two linked list polynomials. So, if the input is like 1x^1 + 1x^2 = 0 and 2x^1 + 3x^0 = 0, then the output will be 3x^1 + 1x^2 + 3x^0 = 0 To solve this, we will follow these steps −
Let us see the following implementation to get better understanding − |