Question

In: Computer Science

A polynomial can be represented using linked list, storing coefficient and exponent of each component of...

A polynomial can be represented using linked list, storing coefficient and exponent of each component of polynomial in a node of it. Write a class in C++ to implement polynomials in three variables x, y and z (a polynomial may also have a constant term). Your class should implement methods for following operations.
• Add two polynomials.
• Multiply two polynomials.
Use a two way header list for implementation. Your program should print the coefficient and exponent of each component of output polynomial.

Solutions

Expert Solution

#include <bits/stdc++.h>

using namespace std;

class Term

{

public:

    int x_expo;

    int coeff;

    int y_expo;

    int z_expo;

    Term *next;

};

// function to create a node in the linked list

void createNode(Term **head, int c, int xe, int ye, int ze)

{

    Term *newNode = new Term();

    Term *curr = *head;

    newNode->coeff = c;

    newNode->x_expo = xe;

    newNode->y_expo = ye;

    newNode->z_expo = ze;

    newNode->next = NULL;

    if (*head == NULL)

    {

        *head = newNode;

        return;

    }

    while (curr->next != NULL)

        curr = curr->next;

    curr->next = newNode;

    return;

}

void removeDuplicates(Term *head)

{

    Term *p1, *p2, *dup;

    p1 = head;

    // Pick elements one by one

    while (p1 != NULL && p1->next != NULL)

    {

        p2 = p1;

        // Compare the picked element with rest of the elements

        while (p2->next != NULL)

        {

            if (p1->x_expo == p2->next->x_expo && p1->y_expo == p2->next->y_expo && p1->z_expo == p2->next->z_expo)

            {

                // Add their coefficients and put it in 1st element

                p1->coeff = p1->coeff + p2->next->coeff;

                dup = p2->next;

                p2->next = p2->next->next;

                // remove the 2nd element

                delete (dup);

            }

            else

                p2 = p2->next;

        }

        p1 = p1->next;

    }

}

Term *addPolynomials(Term *p1, Term *p2)

{

    Term *p = NULL;

    while (p1 != NULL && p2 != NULL)

    {

        // if the powers of all variables is the same we add the coefficients and if its not we create two separate nodes for the two powers

        if (p1->x_expo == p2->x_expo && p1->y_expo == p2->y_expo && p1->z_expo == p2->z_expo)

        {

            createNode(&p, p1->coeff + p2->coeff, p1->x_expo, p1->y_expo, p1->z_expo);

            p1 = p1->next;

            p2 = p2->next;

        }

        else

        {

            createNode(&p, p1->coeff, p1->x_expo, p1->y_expo, p1->z_expo);

            createNode(&p, p2->coeff, p2->x_expo, p2->x_expo, p2->x_expo);

            p1 = p1->next;

            p2 = p2->next;

        }

    }

    // if there are remaining elements in the polynomials we add it to the resulting polynomials

    while (p1 != NULL || p2 != NULL)

    {

        if (p1 != NULL)

        {

            createNode(&p, p1->coeff, p1->x_expo, p1->y_expo, p1->z_expo);

            p1 = p1->next;

        }

        if (p2 != NULL)

        {

            createNode(&p, p2->coeff, p2->x_expo, p2->x_expo, p2->x_expo);

            p2 = p2->next;

        }

    }

    // to remove the duplicate elements present in the resulting polynomials

    removeDuplicates(p);

    return p;

}

// function to multiply two polynomials

Term *multiplyPolynomials(Term *p1, Term *p2)

{

    Term *poly3 = NULL;

    // Create two pointer and store the address of 1st and 2nd polynomials

    Term *ptr1, *ptr2;

    ptr1 = p1;

    ptr2 = p2;

    while (ptr1 != NULL)

    {

        while (ptr2 != NULL)

        {

            int coeff, x_power, y_power, z_power;

            // Multiply the coefficient of both polynomials and store it in coeff

            coeff = ptr1->coeff * ptr2->coeff;

            // Add the corresponding powers of both polynomials and store it in power

            x_power = ptr1->x_expo + ptr2->x_expo;

            y_power = ptr1->y_expo + ptr2->y_expo;

            z_power = ptr1->z_expo + ptr2->z_expo;

            createNode(&poly3, coeff, x_power, y_power, z_power);

            ptr2 = ptr2->next;

        }

        ptr2 = p2;

        ptr1 = ptr1->next;

    }

    // to remove duplicate elements in the linked list

    removeDuplicates(poly3);

    return poly3;

}

// function to print polynomials

void printPoly(Term *head)

{

    while (head != NULL)

    {

        cout << head->coeff << "x" << head->x_expo << "y" << head->y_expo << "z" << head->z_expo;

        head = head->next;

        if (head != NULL)

            cout << " + ";

    }

}

int main()

{

    Term *p1 = NULL, *p2 = NULL, *p = NULL, *m = NULL;

    // creating the first polynomial

    createNode(&p1, 7, 2, 2, 2);

    createNode(&p1, 5, 1, 1, 1);

    createNode(&p1, 3, 0, 0, 0);

    // creating the second polynomial

    createNode(&p2, 8, 1, 1, 1);

    createNode(&p2, 9, 2, 2, 2);

    createNode(&p2, 3, 0, 0, 0);

    cout << "POLYNOMIAL 1 = ";

    printPoly(p1);

    cout << endl;

    cout << "POLYNOMIAL 2 = ";

    printPoly(p2);

    cout << endl;

    cout << "ADDITION = ";

    p = addPolynomials(p1, p2);

    printPoly(p);

    m = multiplyPolynomials(p1, p2);

    cout << endl;

    cout << "MULTIPLICATION = ";

    printPoly(m);

    return 0;

}

//SAMPLE OUTPUT

******************************************************************************************
PLEASE LIKE IT RAISE YOUR THUMBS UP
IF YOU ARE HAVING ANY DOUBT FEEL FREE TO ASK IN COMMENT SECTION
******************************************************************************************


Related Solutions

//LinkNode is a class for storing a single node of a linked list storing integer values....
//LinkNode is a class for storing a single node of a linked list storing integer values. It has two public data fields for the data and the link to //the next node in the list and has three constructors: public class LinkNode { public int data;       public LinkNode next; // post: constructs a node with data 0 and null link public ListNode() {      this(0, null); } // post: constructs a node with given data and null link public LinkNode (int...
In short, you’re going to implement a linked-list class for storing integers, using a provided main...
In short, you’re going to implement a linked-list class for storing integers, using a provided main program to help you interact and test your work. You’ll want to build the linked-list class function by function, working in “Develop” mode to test out each function you write. main.cpp is a read only file linkedlist.h is the file to work on. main.cpp #include #include #include "linkedlist.h" using namespace std; int main() { linkedlist LL; string cmd; int value, key; // // user...
(using single linkedlist c++)In this assignment, you will implement a Polynomial linked list(using single linkedlist only),...
(using single linkedlist c++)In this assignment, you will implement a Polynomial linked list(using single linkedlist only), the coefficients and exponents of the polynomial are defined as a node. The following 2 classes should be defined. p1=23x 9 + 18x 7+3 1. Class Node ● Private member variables: coefficient (double), exponents (integer), and next pointer. ● Setter and getter functions to set and get all member variables ● constructor 2. Class PolynomialLinkedList ● Private member variable to represent linked list (head)...
In this assignment, you will implement a Polynomial linked list, the coefficients and exponents of the...
In this assignment, you will implement a Polynomial linked list, the coefficients and exponents of the polynomial are defined as a node. The following 2 classes should be defined.
In this assignment, you will implement a Polynomial linked list, the coefficients and exponents of the...
In this assignment, you will implement a Polynomial linked list, the coefficients and exponents of the polynomial are defined as a node. The following 2 classes should be defined. p1=23x 9 + 18x 7+3 1. Class Node ● Private member variables: coefficient (double), exponents (integer), and next pointer. ● Setter and getter functions to set and get all member variables ● constructor 2. Class PolynomialLinkedList ● Private member variable to represent linked list (head) ● Constructor ● Public Function to...
Can you make this singular linked list to doubly linked list Create a Doubly Linked List....
Can you make this singular linked list to doubly linked list Create a Doubly Linked List. Use this to create a Sorted Linked List, Use this to create a prioritized list by use. Bring to front those links recently queried. -----link.h------ #ifndef LINK_H #define LINK_H struct Link{ int data; Link *lnkNxt; }; #endif /* LINK_H */ ----main.cpp---- //System Level Libraries #include <iostream> //I/O Library using namespace std; //Libraries compiled under std #include"Link.h" //Global Constants - Science/Math Related //Conversions, Higher Dimensions...
Using C++, Create a singly Linked List of patients list so that you can sort and...
Using C++, Create a singly Linked List of patients list so that you can sort and search the list by last 4 digits of the patient's Social Security number. Implement Node insertion, deletion, update and display functionality in the Linked List. Each patient's record or node should have the following data: Patient Name: Age: Last 4 digits of Social Security Number: The program should first display a menu that gives the user the option to: 1) Add a Patient's record...
IN JAVA LANGUAGE Linked List-Based Queue Implementation Implement Queue using a Linked List. Use the language...
IN JAVA LANGUAGE Linked List-Based Queue Implementation Implement Queue using a Linked List. Use the language library LinkedList Queue methods will call the LinkedList methods You can use string as the object Instead of using an array, as the QueueLab did, here you will use a Linked List from your language's library. Implement all the methods of Stack : enqueue(), dequeue(), size(), printQueue(), etc, using calls to the linked list methods that correspond to the actions need. In the array...
IN JAVA LANGUAGE Linked List-Based Stack Implementation Implement Stack using a Linked List Use the language...
IN JAVA LANGUAGE Linked List-Based Stack Implementation Implement Stack using a Linked List Use the language library LinkedList Stack methods will call the LinkedList methods You can use string as the object Instead of using an array, as the StackLab did, here you will use a Linked List from your language's library. Implement all the methods of Stack : push(), pop(), size(), printStackDown(), etc, using calls to the linked list methods that correspond to the actions need. In the array...
how do you add two matrices linked list in java? (am using linked list because 2D...
how do you add two matrices linked list in java? (am using linked list because 2D arrays are not allowed.) ex [1st matrix] 1 3 2 4 2 1 3 2 4 + [2nd matrix] 3 2 3 2 1 4 5 2 3 = [3rd matrix] 4 5 5 6 3 5 8 4 7
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT