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

C++ Programming. Present a function to merge two linked lists and produce one li

ID: 3689608 • Letter: C

Question

C++ Programming.

Present a function to merge two linked lists and produce one list. Given two lists, yourfunction must merge them in such a way that it interleaves one element from each list alternatively to produce the final list. For example, considera linked list such as 1 rightarrow 2 rightarrow 3 rightarrow 4 rightarrow 10 rightarrow 15 rightarrow NULL with a pointer "firstList" pointing to its first element (1), and another list such as 8 rightarrow 7 rightarrow 6 rightarrow 5 rightarrow NULL with a pointer "secondList" pointingto its first element (8), yourfunction produces 1 rightarrow 8 rightarrow 2 rightarrow 7 rightarrow 3 rightarrow 6 rightarrow 4 rightarrow 5 rightarrow 10 rightarrow 15 rightarrow NULL, with the "firstPointer" now pointingto the first element of this new list (1). The idea is to merge both lists by takingone elementfrom each at any time; in case you exhaustone list, you simply append elements from the other list until you have considered all elements. In other words, implement void mergeUnkedList(chunk *firstList, chunk* secondList)

Explanation / Answer

Code:

void mergeLinkedList(chunk *firstList, chunk *secondList){
   chunk *node1=firstList;
   chunk *node2=secondList;
   while(true){
       if(node2==NULL){
           break;
       }
       else{
           chunk *n=node2->next;
           node2->next=node1->next;
           node1->next=node2;
           if(node1->next->next==NULL){
               node1->next->next=n;
               break;
           }
           else{
               node1=node1->next->next;
               node2=n;
           }
       }
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote