# Answered Essay: FIGURE 4-10 A sparse polynomial degree headPtr coeff power next

Language C++

Consider a sparse implementation of the ADT polynomial that stores only the terms with nonzero coefﬁ cients. For example, you can represent the revised polynomial p in Exercise 9 of Chapter 1 with the linked chain shown in Figure 4-10.

a. Complete the sparse implementation.

b. Deﬁ ne a traverse operation for the ADT polynomial that will allow you to add two sparse polynomials without having to consider terms with zero coefﬁ cients explicitly.

FIGURE 4-10 A sparse polynomial degree headPtr coeff power next

Given below is an implemention of polynomial as described in the question. Also a sample program to create and add polynomials is shown. Please do rate . the answer if it helped. Thank you very much.

polynomial.h

#ifndef polynomial_h
#define polynomial_h

struct term
{
int coeff;
int power;
struct term* next;
};

class polynomial
{
private:
int degree;
public:
polynomial();
void addTerm(int coeff, int power); //stores terms in decreasing order of power
int getDegree() const;
int getCoeff(int power) const;
void display();
};

#endif /* polynomial_h */

polynomial.cpp

#include “polynomial.h”
#include <iostream>
using namespace std;
polynomial::polynomial()
{
degree = 0;
}
//adds terms in sorted decreasing order of power. If a term with same power exists, then the coeffs are added together
{
struct term* prev = 0;
while(curr != 0 )
{
if(power > curr->power)
break;
else if(curr->power == power) //already a term with given power exists, add the coeff
{
curr->coeff += coeff;
return;
}
else
{
prev = curr;
curr = curr->next;
}
}

struct term* t = new term;
t->coeff = coeff;
t->power = power;
t->next = 0;

if(prev == 0) //adding as 1st term
{
degree = power;
}
else
{
prev->next = t;
t->next = curr;
}
}
{
polynomial result;

while(poly1 != 0 && poly2 != 0)
{
if(poly1->power > poly2->power)
{
poly1 = poly1->next;
}
else if(poly2->power > poly1->power)
{
poly2 = poly2->next;
}
else
{
int sum = poly1->coeff + poly2->coeff;
if(sum != 0)
poly1 = poly1->next;
poly2 = poly2->next;
}
}

//add any left out terms from poly1 and poly2
while(poly1 != 0)
{
poly1 = poly1->next;
}

while(poly2 != 0)
{
poly2 = poly2->next;
}
return result;

}
int polynomial::getDegree() const
{
return degree;
}
int polynomial::getCoeff(int power) const
{
if(power > degree)
return 0;
while(curr != 0)
{
if(curr->power == power)
return curr->coeff;
else if(curr->power < power) //no need to search further since we have come to lower powers
break;
else
curr = curr->next;
}
return 0;
}

void polynomial::display()
{
cout << “0”;

else
{
//display 1st term
cout << curr->coeff << “x^” << curr->power ;
curr = curr->next;
while(curr != 0)
{
if(curr->coeff > 0)
cout << ” + ” << curr->coeff << “x^” << curr->power ;
else
cout << ” – ” << -(curr->coeff) << “x^” << curr->power ;

curr = curr->next;
}

}

cout << endl;
}

polymain.cpp

#include “polynomial.h”

#include <iostream>
using namespace std;
int main()
{
polynomial p1;
polynomial p2;

//p1 = 3x^4 – 2x^2 + 5

//p2 = 8x^5 + 3x^4 + x^2 -2

cout << “p1 = ” ; p1.display();
cout << “p2 = ” ; p2.display();

cout << “p3 = p1 + p2 = ” ; p3.display();

}

output

p1 = 3x^4 – 2x^2 + 5x^0
p2 = 8x^5 + 3x^4 + 1x^2 – 2x^0
p3 = p1 + p2 = 8x^5 + 6x^4 – 1x^2 + 3x^0

Pages (550 words)
Approximate price: -

Help Me Write My Essay - Reasons:

Best Online Essay Writing Service

We strive to give our customers the best online essay writing experience. We Make sure essays are submitted on time and all the instructions are followed.

Our Writers are Experienced and Professional

Our essay writing service is founded on professional writers who are on stand by to help you any time.

Free Revision Fo all Essays

Sometimes you may require our writers to add on a point to make your essay as customised as possible, we will give you unlimited times to do this. And we will do it for free.

Timely Essay(s)

We understand the frustrations that comes with late essays and our writers are extra careful to not violate this term. Our support team is always engauging our writers to help you have your essay ahead of time.

Customised Essays &100% Confidential

Our Online writing Service has zero torelance for plagiarised papers. We have plagiarism checking tool that generate plagiarism reports just to make sure you are satisfied.

Try it now!

## Calculate the price of your order

Total price:
\$0.00

How it works?

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

HOW OUR ONLINE ESSAY WRITING SERVICE WORKS

Let us write that nagging essay.

By clicking on the "PLACE ORDER" button, tell us your requires. Be precise for an accurate customised essay. You may also upload any reading materials where applicable.

Pick A & Writer

Our ordering form will provide you with a list of writers and their feedbacks. At step 2, its time select a writer. Our online agents are on stand by to help you just in case.

Editing (OUR PART)

At this stage, our editor will go through your essay and make sure your writer did meet all the instructions.