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 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.
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
TITLE Updating Accounts Using Doubly Linked List TOPICS Doubly Linked List DESCRIPTION General Write a program...
TITLE Updating Accounts Using Doubly Linked List TOPICS Doubly Linked List DESCRIPTION General Write a program that will update bank accounts stored in a master file using updates from a transaction file. The program will maintain accounts using a doubly linked list. The input data will consist of two text files: a master file and a transaction file. See data in Test section below.  The master file will contain only the current account data. For each account, it will contain account...
Describe the physiological events occurring in the heart muscle represented by each ECG component (the P...
Describe the physiological events occurring in the heart muscle represented by each ECG component (the P wave, QRS complex, and T wave). Why does the QRS complex have the largest amplitude?
(a) Write a stack class that is based on a linked list. It can be just...
(a) Write a stack class that is based on a linked list. It can be just pop(), push(), and anything you need for those methods or testing. (b) Write a queue class that is based on a linked list. As above, it can be just enqueue() and dequeue(), as well as anything you need for those methods or testing. (c) Write some test cases, trying to include edge cases. Why did you choose those tests? Did you get the results...
C++ Remove every other node in a circular linked list USING RECURSION. You can only use...
C++ Remove every other node in a circular linked list USING RECURSION. You can only use the preprocessor directives <iostream> and using namespace std; You must use this prototype: int remove_every_other(node *&rear)
The file supplied.o contains code that can build, display, and destroy a linear linked list (singly-linked)....
The file supplied.o contains code that can build, display, and destroy a linear linked list (singly-linked). For this lab, you will need to write the following two functions in list.cpp, and add function prototypes for them to list.h. The provided main.cpp has calls to each of these functions commented out. As you write the functions, uncomment them from main.cpp. void reverse(node * head, node *& newHead) Recursively make a revserse copy of the source list with head where newhead is...
What are the advantages of a using a linked list rather than a conventional array in...
What are the advantages of a using a linked list rather than a conventional array in Java? and when would it be more efficient to use an array rather than a linked list? Explain your answer.
implementing linked list using c++ Develop an algorithm to implement an employee list with employee ID,...
implementing linked list using c++ Develop an algorithm to implement an employee list with employee ID, name, designation and department using linked list and perform the following operations on the list. Add employee details based on department Remove employee details based on ID if found, otherwise display appropriate message Display employee details Count the number of employees in each department
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT