BOOK: Data Structures using c++ Second,Edition, Author: Malik. Chapter5, Questio
ID: 3528797 • Letter: B
Question
BOOK: Data Structures using c++ Second,Edition, Author: Malik. Chapter5, Question 6 QUESTION: Merge Linked Lists Add the following operation to the class orderedLinkedList: void mergeLists (orderedLinkedList &list1;, orderedLinkedList &list2;); // This function creates a new list by merging the // elements of list1 and list2. // Postcondition: first points to the merged list; list1 and list2 are empty Example: Consider the following statements: orderedLinkedList newList; orderedLinkedList list1; orderedLinkedList list2; Suppose list1 points to the list with the elements 2 6 7 and list2 points to the list with the elements 3 5 8. The statement: newList.mergeLists(list1, list2); Creates a new linked list with the elements in the order 2 3 5 6 7 8 and the object newList points to this list. Also, after the preceding statement executes, list1 and list2 are empty. Write the definition of the function template mergeLists to implement the operation mergeLists. Also write a program to test your function.Explanation / Answer
No No you dont have to code so much :) its simple Lets say there are two arrays List 1 and two so we first find their lengths...... and we make a new array of that length and we run a for loop of that length, and keep comparing the elements of LISt 1and two which one is greater goes to push and its corresponding index goes one down.... just see the code and use simple intuition... and its DONE!! MergeList( List 1 , List 2) { int l1 = ArraySize(List 1); int l2= ArraySize(List 2); int l3=l1 +l2 for(int i =0, j=0,k=0;iList2[k] { List3[i]=List2[k]; K=K++; } else List3[i]=List1[j]; j=j++; } if(jRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.