Wednesday, September 28, 2022

Data Structures and Algorithms (Polynomials as Link List) - Tirthankar Pal - MBA from IIT Kharagpur, GATE, GMAT, IIT Written Test, Interview were a part of MBA Entrance, B.S. in Computer Science from NIELIT

 5x3+4x2+3x+2

POLY->(5,3)->(4,2)->(3,1)->(2,0)->NULL

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 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 + 152
Explanation −
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.

Algorithm

Input − polynomial p1 and p2 represented as a linked list.

Step 1: loop around all values of linked list and follow step 2& 3.
Step 2: if the value of a node’s exponent. is greater copy this node to 
result node and head towards the next node.
Step 3: if the values of both node’s exponent is same add the coefficients
and then copy the added value with node to the result.
Step 4: Print the resultant node.

Tirthankar Pal

MBA from IIT Kharagpur with GATE, GMAT, IIT Kharagpur Written Test, and Interview

2 year PGDM (E-Business) from Welingkar, Mumbai

4 years of Bachelor of Science (Hons) in Computer Science from the National Institute of Electronics and Information Technology

Google and Hubspot Certification

Brain Bench Certification in C++, VC++, Data Structure and Project Management

10 years of Experience in Software Development out of that 6 years 8 months in Wipro

Selected in Six World Class UK Universities:-

King's College London, Durham University, University of Exeter, University of Sheffield, University of Newcastle, University of Leeds



No comments:

Post a Comment