Question: [Data Structures] C++: Please answer Question 5, the code is below for your convenience. Some editing and debug is required. Take a screen shot of
[Data Structures] C++: Please answer Question 5, the code is below for your convenience. Some editing and debug is required.
Take a screen shot of the output on IDE to validate your answer and an upvote!
![[Data Structures] C++: Please answer Question 5, the code is below for](https://dsd5zvtm8ll6.cloudfront.net/si.experts.images/questions/2024/09/66f5075c3971f_23566f5075b9bb34.jpg)
/**************************polynomial.h*************************************/
#ifndef Header_h #define Header_h #include
class polyll { private: struct polynode { int exp; polynode* link; float coef; }*p; public: polyll(); ~polyll(); void poly_add(float c, int e); float coefficient(int power); int degree(); void display_poly(); void poly_add(polyll& l1, polyll& l2); void changeCoefficient(float newCoefficient, int power); }; #endif /* Header_h */
polyll::polyll() { p = NULL; } void polyll::poly_add(float c, int e) { polynode* temp = p; if (temp == NULL) { temp = new polynode; p=temp; } else { while (temp->link != NULL) temp = temp->link; temp->link = new polynode; temp = temp->link; } temp->coef=c; temp->exp = e; temp->link = NULL; } float polyll::coefficient(int power) { polynode* temp = p; while (temp->link != NULL) { if(temp->exp==power) { return temp->coef; break; } temp=temp->link; } } void polyll::changeCoefficient(float newCoefficient, int power) { polynode* temp = p; if(temp==NULL) { temp = new polynode; p = temp; } else { while(temp->link !=NULL) { if(temp->exp==power) { temp->coef = newCoefficient; } temp=temp->link; } } } int polyll::degree() { polynode* temp = p; int degree = -100; while (temp->link != NULL) { if(temp ->exp > degree) { degree=temp->exp; } temp=temp->link; } return degree; } void polyll::display_poly() { polynode* temp = p; int f=0; cout coef >0) cout exp !=0) cout coef exp; else cout coef; temp = temp->link; f=1; } } void polyll::poly_add(polyll& l1, polyll& l2) { polynode* z; if(l1.p==NULL && l2.p==NULL) return; polynode *temp1, *temp2; temp1 = l1.p; temp2 = l2.p; while(temp1 != NULL && temp2 != NULL) { if(p == NULL) { p = new polynode; z = p; } else { z->link = new polynode; z = z->link; } if(temp1->exp exp) { z->coef=temp2->coef; z->exp=temp2->exp; temp2=temp2->link; } else { if(temp1->exp > temp2->exp) { z->coef=temp1->coef; z->exp = temp1->exp; temp1 = temp1->link; } else { if(temp1->exp == temp2->exp) { z->coef=temp1->coef+temp2->coef; z->exp=temp1->exp; temp1 = temp1->link; temp2 = temp2->link; } } } } while(temp1 != NULL) { if(p==NULL) { p = new polynode; z = p; } else { z->link=new polynode; z=z->link; } z->coef=temp1->coef; z->exp=temp1->exp; temp1=temp1->link; } while(temp2 != NULL) { if(p == NULL) { p=new polynode; z=p; } else { z->link=new polynode; z=z->link; } z->coef=temp2->coef; z->exp=temp2->exp; temp2=temp2->link; } z->link = NULL; } polyll::~polyll() { polynode* q; while(p != NULL) { q = p->link; delete p; p=q; } }
/**************************main*************************************/
#include
#include "polynomial.h" using namespace std;
int main() { polyll p1; p1.poly_add(1.4,5); p1.poly_add(1.5,4); p1.poly_add(1.7,2); p1.poly_add(1.8,1); p1.poly_add(1.9,0); cout
Consider a sparse implementation of the ADT polynomial that stores only the terms with nonzero coefficients For example, you can represent the revised polynomial p in Exercise 9 of Chapter 1 with the linked chain shown in Figure 4-10 5. a. Complete the sparse implementation. b. Define a traverse operation for the ADT polynomial that will allow you to add two sparse polynomials without having to consider terms with zero coefficients explicitly Define a class DoublyLinkedBag that implements the ADT bag by using a doubly linked chain, as shown in Figure 4-9. Use the class of nodes that Exercise 10 defines. 6. Use the classes for a set or a bag, as defined in this chapter or described in the previous projects, to create a spell checker. Consult the details given in Programming Problems 13 and 14 of Chapter 3. Specify and implement an ADT character string by using a linked chain of characters. Include typical opera- tions such as finding its length, appending one string to another, finding the index of the leftmost occurrence of a character in a string, and testing whether one string is a substring of another. 8. FIGURE 4-10 A sparse polynomial degree headPtr coeff power next
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
