3. struct node ( int coeff, exp; node \"next; class e f private: node·begin, * l
ID: 3726099 • Letter: 3
Question
3. struct node ( int coeff, exp; node "next; class e f private: node·begin, * last; // begin points to the first node and last points to the last node public: re): void assign(int, int): printequation() string search(int exp): e above declaration was used to create polynomial equation as a part of the assignment. Your te the code for BOTH main function and search member function. 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. Write a COMPLETE code for the search member function. The purpose of this function is to search whether the given exponent (exp) is in the list or not. If it is found return "FOUND" else return "NOT FOUND" A. .
Explanation / 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
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.