Polynomial subtraction using linked list in c

#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

Polynomial subtraction using linked list in c

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:

  • constants (like 3, −20, or ½)
  • variables (like x and y)
  • exponents (like the 2 in y2), but only 0, 1, 2, 3 etc are allowed.

Possible operations on Polynomials

  • Addition
  • Subtraction
  • Multiplication
  • Division

Except

  • Division by a variable ( something like 2/x )

A polynomial can have constants, variables and exponents, but never division by a variable.

Read more on Linked List…

What is Linked List ? | Advantages and Disadvantages of Linked List | Examples of Linked List using C Programming Language

Polynomial Operation using Linked List C Programs

1. Polynomial Addition using Linked List

2. Polynomial Subtraction using Linked List

3. Polynomial Multiplication using Linked List

4. Polynomial Addition and Multiplication 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:  

Input: 1st number = 5x2 + 4x1 + 2x0 2nd number = -5x1 - 5x0 Output: 5x2-1x1-3x0 Input: 1st number = 5x3 + 4x2 + 2x0 2nd number = 5x^1 - 5x^0 Output: 5x3 + 4x2 + 5x1 - 3x0

void create_node(int x, int y, struct Node** temp)

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

        r->next = (struct Node*)malloc(sizeof(struct Node));

        r->next = (struct Node*)malloc(sizeof(struct Node));

void polyadd(struct Node* poly1, struct Node* poly2,

    while (poly1->next && poly2->next) {

        if (poly1->pow > poly2->pow) {

            poly->coeff = poly1->coeff;

        else if (poly1->pow < poly2->pow) {

            poly->coeff = poly2->coeff;

            poly->coeff = poly1->coeff + poly2->coeff;

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

    while (poly1->next || poly2->next) {

            poly->coeff = poly1->coeff;

            poly->coeff = poly2->coeff;

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

void show(struct Node* node)

    while (node->next != NULL) {

        printf("%dx^%d", node->coeff, node->pow);

    struct Node *poly1 = NULL, *poly2 = NULL, *poly = NULL;

    create_node(5, 2, &poly1);

    create_node(4, 1, &poly1);

    create_node(2, 0, &poly1);

    create_node(-5, 1, &poly2);

    create_node(-5, 0, &poly2);

    printf("\n2nd Number: ");

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

    polyadd(poly1, poly2, poly);

    printf("\nAdded polynomial: ");

import java.util.Scanner;

    public static Node addPolynomial(Node p1, Node p2)

        Node a = p1, b = p2, newHead = new Node(0, 0),

        while (a != null || b != null) {

            else if (a.pow == b.pow) {

                c.next = new Node(a.coeff + b.coeff, a.pow);

            else if (a.pow > b.pow) {

                c.next = new Node(a.coeff, a.pow);

            else if (a.pow < b.pow) {

                c.next = new Node(b.coeff, b.pow);

    public static void main(String args[])

        Node start1 = null, cur1 = null, start2 = null,

        int[] list1_coeff = { 5, 4, 2 };

        int[] list1_pow = { 2, 1, 0 };

        int n = list1_coeff.length;

            Node ptr = new Node(a, b);

        int[] list2_coeff = { -5, -5 };

        int[] list2_pow = { 1, 0 };

            Node ptr = new Node(a, b);

        Polynomial obj = new Polynomial();

        Node sum = obj.addPolynomial(start1, start2);

            System.out.print(trav.coeff + "x^" + trav.pow);

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 :

  1. If both the numbers are null then return
  2. else if compare the power, if same then  add the coefficients and recursively call  addPolynomials on the next elements of both the numbers.
  3. else if the power of first number is greater then print the current element of first number and recursively call addPolynomial on the next element of the first number and current element of the second number.
  4. else print the current element of the second number and recursively call addPolynomial on the current element of first number and next element of second number.

  Node(int coeff, int power){

void addPolynomials(Node *head1, Node *head2){

  if(head1==NULL && head2==NULL)

  else if(head1->power == head2->power){

    cout<<" "<<head1->coeff +  head2->coeff<<"x^"<<head1->power<<" ";

    addPolynomials(head1->next,head2->next);

  else if(head1->power > head2->power){

    cout<<" "<<head1->coeff<<"x^"<<head1->power<<" ";

    addPolynomials(head1->next,head2);

    cout<<" "<<head2->coeff<<"x^"<<head2->power<<" ";

    addPolynomials(head1,head2->next);

void insert(Node *head, int coeff, int power){

  Node *new_node = new Node(coeff,power);

void printList(Node *head){

  cout<<"Linked List"<<endl;

    cout<<" "<<head->coeff<<"x"<<"^"<<head->power;

  Node *head=new Node(5,2);

  Node *head2 = new Node(6,2);

  cout<<endl<<"Addition:"<<endl;

  addPolynomials(head,head2);

  constructor(coeff, power){

function addPolynomials(head1, head2){

  document.write(head1.power, head2.power)

  if(head1==null && head2==null)

  else if(head1.power == head2.power){

    document.write(` ${head1.coeff + head2.coeff}x^${head1.power} `);

    addPolynomials(head1.next, head2.next);

  else if(head1.power > head2.power){

    document.write(` ${head1.coeff}x^${head1.power} `);

    addPolynomials(head1.next, head2);

    document.write(` ${head2.coeff}x^${head2.power} `);

    addPolynomials(head1, head2.next);

function insert(head, coeff, power){

  let new_node = new Node(coeff,power);

function printList(head){

  document.write("Linked List","</br>");

    document.write(` ${head.coeff}x^${head.power}`);

let head = new Node(5,2);

let head2 = new Node(6,2);

document.write("Addition:");

addPolynomials(head,head2);

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 
This article is contributed by Akash Gupta and Akshita Patel. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


Practice Tags :