Question: creat the function merge_with in the following code #include #include /** * class List * * General description: class for storing and manipulating sequences of

creat the function merge_with in the following code

#include #include

/** * class List * * General description: class for storing and manipulating sequences of items * where item type is specified via template. * * Underlying organization: the implementation uses a singly-linked list data structure * with pointers to both the front (first node) and back (last node) of the list. * * A private struct Node is used to represent individual nodes in a list. */

template class List { private:

struct Node { T data; Node *next;

Node(const T &d = T{}, Node *n = nullptr) : data{d}, next{n} {} };

/* Data members of List class: a front and back pointer */ Node *front; Node *back; int list_size; public: // constructor List() { front = nullptr; back = nullptr; list_size = 0; }

// destructor ~List() { clear(); } /** * Disclaimer: C++ conventions tell us that we should have a couple * of additional constructors here (a copy constructor, assignment operator * etc.) * * However, to keep things simple for now we will ignore that convention * (since the exposure to C++ is pretty variable it seems -- some students * having taken 141 before last semester; some students coming from 107, * etc.) */

/** * function: clear * desc: makes the calling list empty (but the list still * exists). */ void clear() { Node *p = front; Node *pnext;

while (p != nullptr) { pnext = p->next; delete p; p = pnext; } front = back = nullptr; }

/** TODO

* function: merge_with

*

* description: assumes both list a and b are in

* sorted (non-descending) order and merges them

* into a single sorted list with the same

* elements.

*

* This single sorted list is stored in a while

* b becomes empty.

*

* if either of given lists are not sorted,

* we blame the caller and the behavior is

* implementation dependent -- i.e., don't worry

* about it!

*

* Condition in which both parameters are the same

* list (not merely "equal"), the function simply

* does nothing and returns. This can be tested

* with simple pointer comparison.

*

* Example:

*

* a: [2 3 4 9 10 30]

* b: [5 8 8 11 20 40]

*

* after call a.merge_with(b):

*

* a: [2 3 4 5 8 8 9 10 11 20 30 40]

* b: []

*

*

* REQUIREMENTS:

*

* Runtime Must be linear in the length of the

* resulting merged list (or using variables above,

* O(a.length()+b.length()).

*

* should not allocate ANY new list

* nodes -- it should just re-link existing

* nodes.

*/

void merge_with(List &other)

{

}

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!