struct node int coeff, exp node \"next class e l private: node \"begin, \"last/
ID: 3726172 • Letter: S
Question
struct node int coeff, exp node "next class e l private: node "begin, "last/ begin points to the first node and last points to the last node ed void assignint, Int) public rintequationl: string searchfint expl The above declaration was used to create polymomial equation as a part of the assignment. Your task now is to vrite the code for BOTH main function and search member function. A. The main function should declare an object first and then write a COMPLETE CODE to create a link list with 5 nodes using the assign member function. Also write a calling statement to the search member function and out put the result. 8 Write a coMPLETE code for the search member function. Th e purpose of this function is to search whether the øiven exponent (exp) is in the list or not. If it is found return "FOUND" else return "NOT FOUNDExplanation / Answer
#include<iostream>
#include<string>
using namespace std;
struct node{
int coeff, exp;
node *next;
};
class e{
private:
node *begin, *last;
public:
e(){}
~e(){}
void assign(int coef, int ex){
node *temp = new node;
temp->coeff = coef;
temp->exp = ex;
temp->next = NULL;
if(begin==NULL)
begin = temp;
else{
last->next = temp;
}
last = temp;
}
void printequation(){
node *temp = begin;
while(temp!=NULL){
cout<<" "<<temp->coeff<<"^"<<temp->exp<<" ";
temp = temp->next;
if(temp!=NULL)
cout<<"+";
}
}
string search(int exp){
node *temp = begin;
while(temp!=NULL){
if(temp->exp==exp){
return "FOUND";
}
temp = temp->next;
}
return "NOT FOUND";
}
};
int main(){
e *eq = new e();
eq->assign(2,5);
eq->assign(2, 4);
eq->assign(2,3);
cout<<"Searching for coefficient 2: ";
cout<<eq->search(2)<<endl;
eq->assign(2, 2);
eq->assign(2, 1);
cout<<"The equation is: ";
eq->printequation();
cout<<endl;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.