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

In C++ t emplate <class Type> class orderedLinkedList: public linkedListType<Typ

ID: 674148 • Letter: I

Question

In C++

template <class Type>

class orderedLinkedList: public linkedListType<Type>
{
public:
   //insert function
   void insert(const Type& newItem);
};

template <class Type>
void orderedLinkedList<Type>::insert(const Type& newItem)
{
   nodeType<Type> *current; //pointer to traverse the list
   nodeType<Type> *trailCurrent; //pointer just before current
   nodeType<Type> *newNode; //pointer to create a node

   bool found;

   newNode = new nodeType<Type>; //create the node
   newNode->info = newItem;   //store newItem in the node
   newNode->link = NULL;      //set the link field of the node
   //to NULL

   if (first == NULL) //Case 1
   {
       first = newNode;
       last = newNode;
       count++;
   }
   else
   {
       current = first;
       found = false;

       nodeType<Type> *temp=current;

       //iterate over the list to check if the element is already exit in the list
       //thenn set found to true
       while (temp != NULL && !found) //search the list
       {
           if (temp->info == newItem)
           {
               found = true;
               cout<<"element is already found in the list"<<endl;
           }
           else
               temp=temp->link;

       }

       //Check if the element is not in the list
       //then insert the element to list
       if(!found)
       {
           trailCurrent = current;
           current = current->link;

           if (current == first)    
           {
               newNode->link = first;
               first = newNode;
               count++;
           }
           else                     
           {
               trailCurrent->link = newNode;
               newNode->link = current;

               if (current == NULL)
                   last = newNode;

               count++;
           }
       }

   }//end else
}//end insert

Explanation / Answer

In ,my function i am not creating new list....i am retuning merged final list...in main we can create new list and store back that merged list

OrderedLinkeList<Type> MergeLists(OrderedLinkeList<Type> &list1, OrderedLinkeList<Type> &list2) {
//checking for empty lists
if (list1 == null) return list2;
if (list2 == null) return list1;
//merging list with list2
if (list1.data < list2.data) {
list1.next = MergeLists(list1.next, list2);
return list1;
} else { //merging list2 with list1
list2.next = MergeLists(list2.next, list1);
return list2;
}
}