public class Assignment_6 {
public static Node computeTheMergeNode(LinkedList lst1, LinkedList lst2){
// Implement this function
return (null);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Random random = new Random();
int szList1UntilMerge = random.nextInt(100) + 1;
int szList2UntilMerge = random.nextInt(100) + 1;
int szListAfterMerge = random.nextInt(100) + 1;
LinkedList list1 = new LinkedList();
LinkedList list2 = new LinkedList();
for (int i=0;i<szList1UntilMerge;i++){
int value = random.nextInt(1000);
list1.insert(value);
}
if (list1.count() != szList1UntilMerge){
System.out.print("There is an error in the insert function!");
}
Node mergeNode = list1.insert(2000+random.nextInt(100));
for (int i=0;i<szListAfterMerge;i++){
int value = mergeNode.mValue + 1 + random.nextInt(1000);
list1.insert(value);
}
if (list1.count() != szList1UntilMerge+szListAfterMerge+1){
System.out.print("There is an error in the insert function!");
}
for (int i=0;i<szList2UntilMerge;i++){
int value = random.nextInt(1000);
list2.insert(value);
}
if (list2.count() != szList2UntilMerge){
System.out.print("There is an error in the insert function!");
}
Node curr = list2.mHead;
Node prev = null;
while (curr != null){
prev = curr;
curr = curr.mNext;
}
prev.mNext = mergeNode;
if (list2.count() != szList2UntilMerge+szListAfterMerge+1){
System.out.print("There is an error in the merge function!");
}
System.out.println("List 1:");
list1.print();
System.out.println("List 2:");
list2.print();
Node mergeNode2 = computeTheMergeNode(list1, list2);
if (mergeNode == mergeNode2){
System.out.println("MergeNode correctly detected");
System.out.println(mergeNode2.mValue);
}
else{
System.out.println("Please check your algorithm!");
}
}
}
The teacher sent this announcement as well:
Please update the code as follows:
int szList1UntilMerge = random.nextInt(100) + 1;
int szList2UntilMerge = random.nextInt(100) + 1;
int szListAfterMerge = random.nextInt(100) + 1;