3. struct node int coeff, exp; node \"next; class e private: node \"begin, \"las
ID: 3726118 • Letter: 3
Question
3. struct node int coeff, exp; node "next; class e private: node "begin, "last;// begin points to the first node and last points to the last node public: void assign(int, int); printequation string search(int exp); The above declaration was used to create polynomial equation as a part of the assignment. Your task now is to write 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. 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" B.Explanation / Answer
#include <iostream>
using namespace std;
struct node{
int coeff,exp;
node *next;
};
class e
{
private:
node *begin,*last;
public:
e(){
begin = NULL;
last = NULL;
}
~e(){
if(begin)free(begin);
if(last)free(last);
}
void assign(int c,int e){
node *newNode = (node *)malloc(sizeof(node));
newNode->coeff = c;
newNode->exp = e;
newNode->next = NULL;
if(begin == NULL){
begin = newNode;
last = newNode;
}
else{
last->next = newNode;
last = newNode;
}
}
void printequation(){
node *start = begin->next;
cout<<begin->coeff<<"^"<<begin->exp;
while(start!= NULL){
cout<<" + "<<start->coeff<<"^"<<start->exp;
start = start->next;
}
cout<<endl;
}
string search(int ex){
node *start = begin;
while(start!=NULL){
if(start->exp == ex){
return "FOUND";
}
start = start->next;
}
return "NOT FOUND";
}
};
int main(){
e exp;
exp.assign(2,3);
exp.assign(3,4);
exp.assign(2,5);
exp.assign(5,6);
exp.assign(7,7);
exp.printequation();
cout<<exp.search(4);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.