Reputation: 119
I am having some trouble with a for loop that doesn't appear to be stopping. When i call outPutGenerator the first time with only one item in the custom list it works perfectly. When i run it again with 9 items it just keeps running. Could you please point me in the right direction?
edit: I can get the test to run the second time, but nothing after that.
input:
1
5
3
3 2 4
1 5 2
3 6 4
1
6
2
5 6
6 7
Code:
public static void outputGenerator (CustomList [] list, PrintWriter printWriter) {
int size2;
int valueToPrint;
CustomList listToBuild;
int length;
length = list.length;
System.out.println("len: " + length);
//print out the results to a .txt file
try {
printWriter.println("Matrix read: ");
printWriter.println();
printWriter.println("------------------" +
"---------------------");
printWriter.println();
printWriter.flush();
for (int x = 0; x < length; x++){
System.out.println("test");
listToBuild = list[x];
size2 = listToBuild.sizeOfList();
System.out.println("size2 " + size2);
for (int y = 0; y < size2; y++) {
System.out.println("y: " + y);
valueToPrint = listToBuild.ValueOfNode(y);
printWriter.println(valueToPrint);
System.out.println("val" + valueToPrint);
printWriter.flush();
}
printWriter.println();
}
return;
}catch (Exception e) {
e.printStackTrace();
}
}
Custom Linked List Code:
public class CustomList {
private Node firstNode;
private Node end;
private Node header;
private int sizeOfMatrix;
private int sizeOfList;
//constructor to set all to blank
public CustomList() {
firstNode = null;
end = null;
header = null;
sizeOfMatrix = 0;
sizeOfList = 0;
}
public void addToList(int dataToSave) {
Node node = new Node (dataToSave);
if (firstNode == null) {
firstNode = node;
firstNode.next = end;
firstNode.before = header;
}
else if (end == null) {
end = node;
end.before = firstNode;
firstNode.next = end;
}
else
end.next = node;
node.before = end;
end = node;
sizeOfMatrix++;
}
public void setHeader (int dataToUse){
Node headerNode = new Node(dataToUse);
header = headerNode;
header.next = firstNode;
}
public void print() {
Node zNode = firstNode;
System.out.println("Test");
if(firstNode == null){
System.out.print("EMPTY");
return;
}
while (zNode != null) {
System.out.println(zNode);
zNode = zNode.next;
}
}
public int sizeOfList() {
Node zNode = firstNode;
sizeOfList = 0;
while( zNode != null) {
zNode = firstNode.next;
sizeOfList++;
}
return sizeOfList;
}
public int ValueOfNode( int column ) {
int counter = 0;
Node zNode = firstNode;
while (zNode != null) {
if (column == counter){
return zNode.numInMatrix();
}
else
zNode = firstNode.next;
counter++;
}
return -1;
}
Upvotes: 1
Views: 44
Reputation: 4135
When you are doing
size2 = listToBuild.sizeOfList();
sizeOfList() will always return the same value for each call.
I guess, you are not changing value of firstNode. Moreover, there are some changes I have made in your function.
public int sizeOfList() {
Node zNode = firstNode;
sizeOfList = 0;
while( zNode != null) {
zNode = zNode.next;
sizeOfList++;
}
return sizeOfList;
}
Upvotes: 1