Adding two polynomials using 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.
Example:
Adding two polynomials using Linked List in C++.
To understand this concept better let's first brush up all the basic contents that are required.
linked list is a data structure that stores each element as an object in a node of the list. every note contains two parts data han and links to the next node.
Polynomial is a mathematical expression that consists of variables and coefficients. for example x^2 - 4x + 7
In the Polynomial linked list, the coefficients and exponents of the polynomial are defined as the data node of the list.
For adding two polynomials that are stored as a linked list. We need to add the coefficients of variables with the same power. In a linked list node contains 3 members, coefficient value link to the next node.
a linked list that is used to store Polynomial looks like −
Polynomial : 4x7 + 12x2 + 45
This is how a linked list represented polynomial looks like.
Adding two polynomials that are represented by a linked list. We check values at the exponent value of the node. For the same values of exponent, we will add the coefficients.
Example,
Input : p1= 13x8 + 7x5 + 32x2 + 54 p2= 3x12 + 17x5 + 3x3 + 98 Output : 3x12 + 13x8 + 24x5 + 3x3 + 32x2 + 152Explanation − For all power, we will check for the coefficients of the exponents that have the same value of exponents and add them. The return the final polynomial.
1. Introduction
In mathematics, a polynomial is an expression that contains a sum of powersin one or morevariablesmultiplied bycoefficients. A polynomial in one variable,
In this tutorial, we’ll show how to add and multiply two polynomials using a linked list data structure.