Just getting this out the way so we can get down to whats what. Yes I am a student. My question is relate to the curriculum I am currently learning. This WAS a homework assignment but the current code is far from what I turned in, hence now only being a side project of my own now. I was instructed (for homework) to merge two lists without creating new objects; thus I was required to implement the two finger method. My homework was the merge method. Which means the main method was not part of the homework.
Now, my question focuses on the main method. Correct me if I am wrong: Considering that a node linked-list connects objects by an "invisible thread"; being that first we have an object then the next pointer pointing to either null (specifying the end) or the next item of the linked-list. And that its creation follows stack principles F.I.L.O. And lets say that I wanted to create a list of numbers in acceding order. This can be done in two ways.
1) *how I learned in class* have a first pointer created with the very first object, and a temp pointer that will go through the process of the list creation.
2) *another way that occurred to me* have an array of integers in ascending order, create the pointer List with the very last element in the array and the 'next' point to null. Then, descend through the array's index and store each element in the linked-list of nodes.
in the main method I used #1, please understand I just put all this with the hopes of being corrected so I can improve my understanding
Also, I am aware that those two actions are preferably preformed within a loop but this is not the behavior I am mainly questioning
**********The problem.
-because the programs merge method can deals with equal size and unparalleled size of linked-lists, there are two cases to be tested.
-to avoid the tedious task of clicking the run button every time in order to test the probable out comes, I put ,(mostly everything), in the main method within a do-while loop so that at the end I am given the option to continue or not. Hence not needed to press run every time I want to test each case.
-The initial run: Everything seems to run fine. I input the size of both linked-lists to be of paralleled size and I print out the two lists first then the merged list. This is to show the merged list is a product of the two.
-I am prompt to continue: choose to continue to test the unparalleled size of linked-lists. Everything goes smoothly until the program calls on the merge method. It seems I encounter an infinite loop.
*my first thought is that the unparalleled size has something to do with it. I re-run the program but this time start with the unparalleled size. There is no problem, the program performs as it should.
-I am prompt to continue: I choose to continue to then see what is the problem. I input the size of each linked-list to be parallel. I encounter the same problem.
*******My attempts to solve
-because I have the initial two lists to print out first, I see the problem doesn't happen there. I place printLines to see where exactly my problem starts. It then narrows down to when the merge method is called. I then continue with the printLines within the merge method, and see that within the loop that merges the two linked-lists I encounter an infinite loop. There is a printLine there that shows me the values of each list as it's being merged and see that the incrementation of the linked-list's index is skipped. It would be understandable if it is because the pointer next is indicating to null which then the index is not incremented. But that is not it. If you see, the values are stuck around the beginning of the list.
*I would continue with my other attempts but my head is going all over the place with this explanation.
*The code of the merge method and the main method, including of the separate node class will be posted. This is so the project can be seen as a whole. Please feel free to "rip me a new one" if you have to. Any clarification (if needed) regarding the behavior of anything written above is appreciated.
Node Class
package ClassWork3;
public class NodeOfInt {
public int data;
public NodeOfInt next;
public NodeOfInt(int dat, NodeOfInt nex) {
this.data = dat;
this.next = nex;
}
}
Main method and merge
package ClassWork3;
import ClassWork3.NodeOfInt;
import java.util.Scanner;
public class HomeWork3 {
public static void main(String[] args) {
NodeOfInt L1 = new NodeOfInt(0 ,null);
NodeOfInt L1pointer = L1;
NodeOfInt L2 = new NodeOfInt(1 ,null);
NodeOfInt L2pointer = L2;
Scanner input = new Scanner(System.in);
int size;
System.out.println("******************************"
+ "\n* THIS PROGRAM'S MAIN METHOD *"
+ "\n* TESTS THE MERGE METHOD FOR *"
+ "\n* TWO ARRAYS WHOSE *"
+ "\n* COMBINATION IS MxN OR NxN *"
+ "\n******************************"
);
do {
System.out.println("\n##############################");
System.out.print("Please enter a size for list1: ");
size = input.nextInt();
for(int i = 1; i < size; i++){
int num = i * 2;
NodeOfInt temp1 = new NodeOfInt(num ,null);
L1pointer.next = temp1;
L1pointer = L1pointer.next;
}
System.out.print("Please enter a size for list2: ");
size = input.nextInt();
for(int i = 1; i < size; i++){
int num = (i * 3) - 1;
NodeOfInt temp2 = new NodeOfInt(num ,null);
L2pointer.next = temp2;
L2pointer = L2pointer.next;
}
System.out.println("\n********>List 1\n------------------------------");
NodeOfInt temp1 = L1;
size = 1;
while(temp1 != null){
System.out.printf("%-2d|", temp1.data);
if(size % 10 == 0)
System.out.println();
temp1 = temp1.next;
size++;
}
System.out.println();
System.out.println("\n********>List 2\n------------------------------");
NodeOfInt temp2 = L2;
size = 1;
while(temp2 != null){
System.out.printf("%-2d|", temp2.data);
if(size % 10 == 0)
System.out.println();
temp2 = temp2.next;
size++;
}
System.out.println();
NodeOfInt Lmerged = merge(L1, L2);
NodeOfInt temp3 = Lmerged;
System.out.println("\n********>Merged list\n------------------------------");
size = 1;
while(temp3 != null){
System.out.printf("%-2d|", temp3.data);
if(size % 10 == 0)
System.out.println();
temp3 = temp3.next;
size++;
}
System.out.print("\nDo you wish to to continue[y/n]: ");
}while(input.next().toLowerCase().charAt(0) == 'y');
}
/**
* The total of nodes created in this method is
* L1's size + L2's size.
* temp and MergedL are merely pointers
* @param L1
* @param L2
* @return MergedL
*/
static NodeOfInt merge(NodeOfInt L1, NodeOfInt L2) {
System.out.println("#%$%#");
/**
*Create two pointers
*MergedL is the pointer to be returned
*temp is the pointer to create the merged list on
*/
NodeOfInt MergedL, temp;
/**
* This if statement checks which first
* node of L1 and L2 is smaller
*/
if(L1.data < L2.data) {
MergedL = L1;
temp = MergedL;
L1 = L1.next;
}
/**
* Here if L1.data and L2.data
* are equal or L1 is greater
* The result will be the same.
*/
else{
MergedL = L2;
temp = MergedL;
L2 = L2.next;
}
/**
* The while loop will have the same implementation
* as the above if statement but here he exercise
* the possibility of
* *L1's size being smaller/larger than L2's size*
* or vice versa.
*/
while(L1 != null || L2 != null) {
/**
* This only executes if L1 and L2 both
* have a item.
*/
if(L1 != null && L2 != null) {
//System.out.println(L1.data + "|L1|L2|" + L2.data);
if (L1.data < L2.data) {
temp.next = L1;
L1 = L1.next;
temp = temp.next;
}
else {
temp.next = L2;
L2 = L2.next;
temp = temp.next;
}
}
/**
* This else runs on the possibility if
* either L1 is null or L2 is null
*/
else {
/**
* L2 is null
* L1 still has an item
*/
if(L2 == null && L1 != null) {
//System.out.println(L1.data + "|L1|");
temp.next = L1;
L1 = L1.next;
temp = temp.next;
}
/**
* L1 is null
* L2 still has an item
*/
if(L1 == null && L2 != null) {
//System.out.println("|L2|" + L2.data);
temp.next = L2;
L2 = L2.next;
temp = temp.next;
}
/**
* This if is so both are null.
*/
else{
//System.out.println("|*^*|");
break;
}
}
}
/**
* Because we used temp to merge the two lists
* MergedL remains untouched, still pointing to
* the first node.
*/
return MergedL;
}
}

New Topic/Question
Reply



MultiQuote





|