Question: Given the definitions of the classes Node and List below, define the member function insert_after and the destructor. The member function insert_after passes in the

Given the definitions of the classes Node and List below, define the member function insert_after and the destructor.

The member function insert_after passes in the address to a Node (Node pointer) currently in the list and an integer value to be inserted into the list. This function should then create a new Node containing this integer value passed in and insert it into the list after the Node whose address is passed in. If the address passed in is 0, then insert the new Node at the front of the List.

The insert_after function is a private helper function that will be used within the insert_sorted public member function. You DO NOT need to define the insert_sorted function.

The destructor should ensure there are no memory leaks.

Notice there are no push_front, push_back or pop_front functions declared or defined for this List class. You may NOT define these or any other member functions, even as private helper functions.

struct Node {

 public: Node *next; int data; Node(int val) 
 : next(0), data(val) {} 

};

class List {

 private: Node *head; Node *tail; 

public: List() : head(0), tail(0) {} ~List(); //IMPLEMENT void insert_sorted(int); //DON'T IMPLEMENT

private:

void insert_after(Node *, int); //IMPLEMENT

};

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!