Question: Please Convert this Java Code to C/C++ Code using NetBeans : Code : package chegg; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class MergingList {

Please Convert this Java Code to C/C++ Code using NetBeans :

Code :

package chegg;

import java.util.ArrayList; import java.util.Collections; import java.util.List;

public class MergingList {

public static void main(String[] args) {

//Creating List and adding the items

List l1 = new ArrayList<>(); l1.add(3); l1.add(4); l1.add(6); l1.add(7); l1.add(9); l1.add(30); l1.add(11); l1.add(15); l1.add(17); l1.add(19); l1.add(22); l1.add(29);

List l2 = new ArrayList<>(); l2.add(22); l2.add(30); l2.add(35); l2.add(60); l2.add(10); l2.add(12); l2.add(15); l2.add(20);

//Sorting the above lists Collections.sort(l1); Collections.sort(l2);

// Program to merge both the sorted list //Creating the empty merged list List mergedList = new ArrayList<>();

//initializing the iterators int i = 0, j = 0;

while (i < l1.size() && j < l2.size()) {

// we will compare the elements of both list if the element of the i-th position in list //is greater than element of j-th position in the second list //then we will add the i-th element of the first list in the merged list //else we will put the the j-th element of the second list into the first list if (l1.get(i) < l2.get(j)) { mergedList.add(l1.get(i)); i++;

} else { mergedList.add(l2.get(j)); j++; } }

//now we will add the leftover items of the first list into the merged list while (i < l1.size()) { mergedList.add(l1.get(i)); i++; } //now we will add the leftover items of the second list into the merged list

while (j < l2.size()) { mergedList.add(l2.get(j)); j++; }

//Printing the merged list System.out.println(mergedList.toString()); }

}

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!