0% found this document useful (0 votes)
418 views

Adding Two Polynomials Using Linked List: Courses Suggest An Article

This document describes how to add two polynomials represented as linked lists by summing coefficients with the same powers. It provides an example of adding the lists 5x^2 + 4x^1 + 2x^0 and 5x^1 + 5x^0 to get 5x^2 + 9x^1 + 7x^0. It then presents a C++ program to implement this using linked lists that iterates through the lists, compares powers, and sums coefficients when powers match before outputting the resulting list.

Uploaded by

PratikRoy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
418 views

Adding Two Polynomials Using Linked List: Courses Suggest An Article

This document describes how to add two polynomials represented as linked lists by summing coefficients with the same powers. It provides an example of adding the lists 5x^2 + 4x^1 + 2x^0 and 5x^1 + 5x^0 to get 5x^2 + 9x^1 + 7x^0. It then presents a C++ program to implement this using linked lists that iterates through the lists, compares powers, and sums coefficients when powers match before outputting the resulting list.

Uploaded by

PratikRoy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

4/24/2019 Adding two polynomials using Linked List - GeeksforGeeks

Custom Search

Courses Login

Suggest an Article


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 coe cients who have same variable powers.

Example:

Input:
1st number = 5x^2 + 4x^1 + 2x^0
2nd number = 5x^1 + 5x^0
Output:
5x^2 + 9x^1 + 7x^0
Input:
1st number = 5x^3 + 4x^2 + 2x^0
2nd number = 5x^1 + 5x^0
Output:
5x^3 + 4x^2 + 5x^1 + 7x^0

Recommended: Please solve it on “PRACTICE” rst, before moving on to the solution.▲

https://www.geeksforgeeks.org/adding-two-polynomials-using-linked-list/ 1/6
4/24/2019 Adding two polynomials using Linked List - GeeksforGeeks

// C++ program for addition of two polynomials


// using Linked Lists
#include<bits/stdc++.h>
using namespace std;

// Node structure containing power and coefficient of variable


struct Node
{
int coeff;
int pow;
struct Node *next;
};

// Function to create new node


void create_node(int x, int y, struct Node **temp)
{
struct Node *r, *z;
z = *temp;
if(z == NULL)
{
r =(struct Node*)malloc(sizeof(struct Node));
r->coeff = x;
r->pow = y;
*temp = r;
r->next = (struct Node*)malloc(sizeof(struct Node));
r = r->next;
r->next = NULL;
}
else
{
r->coeff = x;
r->pow = y;
r->next = (struct Node*)malloc(sizeof(struct Node));
r = r->next;
r->next = NULL;
}
}

// Function Adding two polynomial numbers


void polyadd(struct Node *poly1, struct Node *poly2, struct Node *poly)
{
while(poly1->next && poly2->next)
{
// If power of 1st polynomial is greater then 2nd, then store 1st as it is
// and move its pointer
if(poly1->pow > poly2->pow)
{
poly->pow = poly1->pow;
poly->coeff = poly1->coeff;
poly1 = poly1->next;
}

// If power of 2nd polynomial is greater then 1st, then store 2nd as it is


// and move its pointer
else if(poly1->pow < poly2->pow)
{
poly->pow = poly2->pow; ▲
poly->coeff = poly2->coeff;
poly2 = poly2->next;
}

https://www.geeksforgeeks.org/adding-two-polynomials-using-linked-list/ 2/6
4/24/2019 Adding two polynomials using Linked List - GeeksforGeeks

// If power of both polynomial numbers is same then add their coefficients


else
{
poly->pow = poly1->pow;
poly->coeff = poly1->coeff+poly2->coeff;
poly1 = poly1->next;
poly2 = poly2->next;
}

// Dynamically create new node


poly->next = (struct Node *)malloc(sizeof(struct Node));
poly = poly->next;
poly->next = NULL;
}
while(poly1->next || poly2->next)
{
if(poly1->next)
{
poly->pow = poly1->pow;
poly->coeff = poly1->coeff;
poly1 = poly1->next;
}
if(poly2->next)
{
poly->pow = poly2->pow;
poly->coeff = poly2->coeff;
poly2 = poly2->next;
}
poly->next = (struct Node *)malloc(sizeof(struct Node));
poly = poly->next;
poly->next = NULL;
}
}

// Display Linked list


void show(struct Node *node)
{
while(node->next != NULL)
{
printf("%dx^%d", node->coeff, node->pow);
node = node->next;
if(node->next != NULL)
printf(" + ");
}
}

// Driver program
int main()
{
struct Node *poly1 = NULL, *poly2 = NULL, *poly = NULL;

// Create first list of 5x^2 + 4x^1 + 2x^0


create_node(5,2,&poly1);
create_node(4,1,&poly1);
create_node(2,0,&poly1);

// Create second list of 5x^1 + 5x^0


create_node(5,1,&poly2);
create_node(5,0,&poly2);

printf("1st Number: ");
show(poly1);

https://www.geeksforgeeks.org/adding-two-polynomials-using-linked-list/ 3/6
4/24/2019 Adding two polynomials using Linked List - GeeksforGeeks

printf("\n2nd Number: ");


show(poly2);

poly = (struct Node *)malloc(sizeof(struct Node));

// Function add two polynomial numbers


polyadd(poly1, poly2, poly);

// Display resultant List


printf("\nAdded polynomial: ");
show(poly);

return 0;
}

Output:

1st Number: 5x^2 + 4x^1 + 2x^0


2nd Number: 5x^1 + 5x^0
Added polynomial: 5x^2 + 9x^1 + 7x^0

Time Complexity: O(m + n) where m and n are number of nodes in rst and second lists respectively.

Related Article: Add two polynomial numbers using Arrays


This article is contributed by Akash Gupta. If you like GeeksforGeeks and would like to contribute, you
can also write an article using contribute.geeksforgeeks.org or mail your article to
[email protected]. See your article appearing on the GeeksforGeeks main page and help
other Geeks.

Please write comments if you nd anything incorrect, or you want to share more information about the
topic discussed above.

Recommended Posts:
Multiplication of two polynomials using Linked list
XOR Linked List – A Memory E cient Doubly Linked List | Set 2
XOR Linked List - A Memory E cient Doubly Linked List | Set 1
Convert singly linked list into circular linked list
Merge a linked list into another linked list at alternate positions
Difference between Singly linked list and Doubly linked list
Convert Singly Linked List to XOR Linked List
Check if a linked list is Circular Linked List
Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes
Partitioning a linked list around a given value and If we don't care about making the elements of the list
"stable" ▲

Length of longest palindrome list in a linked list using O(1) extra space

https://www.geeksforgeeks.org/adding-two-polynomials-using-linked-list/ 4/6
4/24/2019 Adding two polynomials using Linked List - GeeksforGeeks

Rotate the sub-list of a linked list from position M to N to the right by K places
Multiply two polynomials
Program to add two polynomials
Linked List Sum of Nodes Between 0s

Article Tags : Linked List Mathematical Amazon Linked-List-Polynomial maths-polynomial

Practice Tags : Amazon Linked List Mathematical


3

To-do Done 2.5

Based on 29 vote(s)

Feedback/ Suggest Improvement Add Notes Improve Article

Please write to us at [email protected] to report any issue with the above content.

Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.

Load Comments Share this post!

https://www.geeksforgeeks.org/adding-two-polynomials-using-linked-list/ 5/6
4/24/2019 Adding two polynomials using Linked List - GeeksforGeeks

5th Floor, A-118,


Sector-136, Noida, Uttar Pradesh - 201305
[email protected]

COMPANY LEARN
About Us Algorithms
Careers Data Structures
Privacy Policy Languages
Contact Us CS Subjects
Video Tutorials

PRACTICE CONTRIBUTE
Company-wise Write an Article
Topic-wise Write Interview Experience
Contests Internships
Subjective Questions Videos

@geeksforgeeks, Some rights reserved

https://www.geeksforgeeks.org/adding-two-polynomials-using-linked-list/ 6/6

You might also like