Question

In: Computer Science

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.

Solutions

Expert Solution

Solution: 
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

// 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;

        }

         

        // 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);

     

    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 first and second lists respectively.

note: plzzz don't give dislike.....plzzz comment if you have any problem i will try to solve your problem.....plzzz give thumbs up i am in need....


Related Solutions

Implement a class Polynomial that uses a dynamic array of doubles to store the coefficients for...
Implement a class Polynomial that uses a dynamic array of doubles to store the coefficients for a polynomial. Much of this work can be modelled on the C++ dynamic array of ints List that we discussed in class. This class does not need the method the overloaded += operator. Your class should have the following methods: Write one constructor that takes an integer n for the degree of a term and a double coefficient c for the coefficient of the...
In this homework, you will implement a single linked list to store a list of employees...
In this homework, you will implement a single linked list to store a list of employees in a company. Every employee has an ID, name, department, and salary. You will create 2 classes: Employee and EmployeeList. Employee class should have all information about an employee and also a “next” pointer. See below: Employee Type Attribute int ID string name string department int salary Employee* next Return Type Function (constructor) Employee(int ID, string name, string department, int salary) EmployeeList class should...
In C++, Implement the queue ADT with a singly linked list
In C++, Implement the queue ADT with a singly linked list
Assume that you want to implement binary search with a linked list. What would be the...
Assume that you want to implement binary search with a linked list. What would be the performance of this algorithm? Compare and contrast this algorithm with the implementation of binary search on traditional sorted array and give a discussion. Your discussion and analysis must explain what the possibilities, issues and consequences of such design are, and then explain whether these issues would exist in the traditional array approach. Your answer can be around 1-2 paragraph of writing backed-up with algorithmic...
Implement the SimpleQueue interface with the MyQueue class. You can use either a linked list or...
Implement the SimpleQueue interface with the MyQueue class. You can use either a linked list or a dynamic array to implement the data structure. A queue is a specialised form of list in which you can only get and remove the first element in the queue. The class should be able to work with the following code: SimpleQueue queue = new MyQueue(); NB: You cannot import anything from the standard library for this task. The data structure must be of...
C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you...
C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you will implement the functionality of a stack and a queue using a linked list. Your program must use of the declaration of the Stack and Queue class in Stack.h and Queue.h You have to implement the functionalities of queue (enq, deq, displayQueue) in a file called Queue.cpp. All the functions in Queue.cpp should follow the prototypes declared in Queue.h. Your code should make use...
How do you implement stack by using linked list? No code just explain it.
How do you implement stack by using linked list? No code just explain it.
The goal of this assignment is to implement a set container using linked lists. Use the...
The goal of this assignment is to implement a set container using linked lists. Use the authors bag3.h and bag3.cpp as a basis for implementing your set container using linked lists. The authors bag3.h and bag3.cpp can be found here https://www.cs.colorado.edu/~main/chapter5/ Since you are using the authors bag3.h and bag3.cpp for your Set container implementation, make sure that you change the name of the class and constructors to reflect the set class. Additionally you will need to implement the follow...
Write a program where you- 1. Create a class to implement "Double Linked List" of integers....
Write a program where you- 1. Create a class to implement "Double Linked List" of integers. (10) 2. Create the list and print the list in forward and reverse directions. (10)
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT