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

1. The provided code reads two sequences of numbers. In this task, you are asked

ID: 3550751 • Letter: 1

Question

1. The provided code reads two sequences of numbers. In this task, you are asked to write a function to insert these numbers into two separate doubly linked lists so that the data are in ascending order. The function should have the following form. Note in the current implementation, the numbers are not inserted in ascending order.



template <typename T>

void insertOrder(dnode<T> *header, const T& item)

{

insert(header, item);

}



2. Follow the instructions under question number 36 on page 500 of the textbook and implement the following function:

template <typename T>


void merge(dnode<T> *list1, dnode<T> *list2)

{

}
-------------------
//d_dnode.h

#ifndef DOUBLY_LINKED_NODE_CLASS

#define DOUBLY_LINKED_NODE_CLASS


template <typename T>

class dnode

{

public:

// the members of a dnode object are used for operations within a

// doubly linked list; access is simplified by making them public


      T nodeValue; // data value of the node

dnode<T> *prev; // previous node in the list

      dnode<T> *next; // next node in the list


// default constructor. creates object with value T(), the

// default value of type T. set the node pointers to point at

// the node itself

      dnode()

{

next = this; // the next node is the current node

prev = this; // the previous node is the current node

}


      // constructor with an argument to initialize nodeValue.

// set the node pointers to point at the node itself

      dnode(const T& value): nodeValue(value)

{

next = this; // the next node is the current node

prev = this; // the previous node is the current node

}

};


#endif // DOUBLY_LINKED_NODE_CLASS

Explanation / Answer

1. I could complete only the first part. Please check and let me know if its working properly. Then i will complete the second part. Thank you.


template <typename T>

void insertOrder(dnode<T> *header, const T& item)

{

if((*header).nodeValue > item){

dnode<T> * item_node = new dnode(item);

item_node->next = header;

header->prev->next = item_node;

*header = item_node;

}

else{

dnode<T> * curr = header->next;

while(curr != header){

if(curr->nodeValue > item){

dnode<T> * item_node = new dnode(item);

item_node->next = curr;

curr->prev->next = item_node;

return;

}

curr = curr->next;

}

dnode<T> * item_node = new dnode(item);

item_node->next = curr;

curr->prev->next = item_node;

return;

}

}