Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need help with implementing a C++ function- Linked List I was given a .h file de

ID: 3841034 • Letter: N

Question

Need help with implementing a C++ function- Linked List

I was given a .h file dealing with linked list and there is one function I'm stuck on: the set_recent function

These are the instructions:

****************************

The set_recent function will set the recent_ptr to the node that contains the requested exponent. If there is no such exponent, then recent_ptr should be set to the last node that is still less than the specified exponent. Note that set_recent is a const member function, but that it can still change the mutablerecent_ptr member variable. My implementation of set_recent used three cases:

If the requested exponent is zero, then set recent_ptr to the head of the list.

Else if the exponent is greater than or equal to the current degree, then set recent_ptr to the tail of the list.

Else if the exponent is smaller than the exponent in the recent node, then move the recent_ptr backward as far as needed.

Else move the recent_ptr forward as far as needed.

*****************************

I'm having trouble with the last two parts with the "far as needed" part. How do I go about this?

This is the .h file:

class polynomial

{

public:

// CONSTRUCTORS and DESTRUCTOR

polynomial(double c = 0.0, unsigned int exponent = 0);

polynomial(const polynomial& source);

~polynomial( );

// MODIFICATION MEMBER FUNCTIONS

polynomial& operator =(const polynomial& source);

void add_to_coef(double amount, unsigned int exponent);

void assign_coef(double coefficient, unsigned int exponent);

void clear( );

  

// CONSTANT MEMBER FUNCTIONS

double coefficient(unsigned int exponent) const;

unsigned int degree( ) const { return current_degree; }

polynomial derivative( ) const;

double eval(double x) const;

void find_root(

double& answer,

bool& success,

unsigned int& iterations,

double guess = 0,

unsigned int maximum_iterations = 100,

double epsilon = 1e-8

)

const;

unsigned int next_term(unsigned int e) const;

unsigned int previous_term(unsigned int e) const;

  

// CONSTANT OPERATORS

double operator( ) (double x) const { return eval(x); }

private:

polynode *head_ptr; // Head pointer for list of terms

polynode *tail_ptr; // Tail pointer for list of terms

mutable polynode *recent_ptr; // Most recently used term

unsigned int current_degree; // Current degree of the polynomial

// A private member function to aid the other functions:

void set_recent(unsigned int exponent) const;

};

  

// NON-MEMBER BINARY OPERATORS

polynomial operator +(const polynomial& p1, const polynomial& p2);

polynomial operator -(const polynomial& p1, const polynomial& p2);

polynomial operator *(const polynomial& p1, const polynomial& p2);

  

// NON-MEMBER OUTPUT FUNCTION

std::ostream& operator << (std::ostream& out, const polynomial& p);

Explanation / Answer


using namespace std;

class Program
{
private:struct Node
{
string name;
int num;
struct Node *nxt;
};

Node *head;

public:Program()
{
head = NULL;
}
void addLast(string, int);
void Insrt(string, int);
void Del(string, int);
void Dsply() const;
};

void Program::addLast(string data1, int data2)
{
Node *newNode;
Node *nodePtr;

newNode = new Node;
newNode->name = data1;
newNode->num = data2;
newNode->next = NULL;

if (!head)
head = newNode;
else
{
nodePtr = head;
while (nodePtr->next)
nodePtr = nodePtr->next;
nodePtr->next = newNode;
}
}
void Program::Insrt(string data1, int data2)
{
int comp;
Node *newNode;
Node *nodePtr;
Node *prevNode = NULL;

newNode = new Node;
newNode->name = data1;
newNode->number = data2;

if(!head)
{
head = newNode;
newNode->next = NULL;
}
else
{
nodePtr = head;
prevNode = NULL;

while((nodePtr != NULL) && (nodePtr->name.compare(data1) < 0))

{
prevNode = nodePtr;
nodePtr = nodePtr->next;
}

if(prevNode == NULL)
{
head = newNode;
newNode->next = nodePtr;
}
else
{
prevNode->next = newNode;
newNode->next = nodePtr;
}
}
}
void Program::Del(string data1, int data2)

{
int comp;
Node *nodePtr;
Node *prevNode;

if(!head)
return;
if(nodePtr->name.compare(data1) == 0)
{
nodePtr = head-> next;
del head;
head = nodePtr;
}
else
{
nodePtr = head;
while((nodePtr != NULL) && (nodePtr->name.compare(data1) != 0))
{
prevNode = nodePtr;
nodePtr = nodePtr-> next;
}

if(nodePtr)
{
prevNode->next = nodePtr->next;
delete nodePtr;
}
}
}

void Program::Dsply() const
{
Node *nodePtr;
nodePtr = head;

while(nodePtr)
{
cout << nodePtr->name << nodePtr->number << endl;
nodePtr = nodePtr->next;
}
}
int main()
{
ifstream inFile;
const int SIZE = 51;
string nm, info[SIZE],num;// input, num;
string input;
char ch;
int i = 0;

inFile.open("/home/abhishek/view/sample_abhi/cpluplus/cppluvr8.txt");
if(!inFile)
{
cout << "Error! File cannot open." << endl;
return 0;
}
else
cout << "File has been opened." << endl;

getline(inFile,input);
while(!inFile.eof())
{
if (input == "%INSERT")
{
while(
(input != "%DELETE") && (input != "%SEARCH") && (input != "%PRINT") && (input != "%END"))

{
getline(inFile,input);
cout << input << endl;
}
continue;
}
else if (input == "%DELETE")
{
cout << "delete" << endl;
}
else if (input == "%SEARCH")
{
cout << "search" << endl;
}
else if (input == "%PRINT")
cout << "print" << endl;
else if (input == "%END")
return 0;
getline(inFile,input);
}