In the else statement of this main for loop, I am trying to add each newly created stacks into my queue and need to keep going on over the same queue until it's empty or I find the end word.
for (Stack<String> next:wordQ){
if(next.peek().toString().equals(start)){
for(String each:next){
ladder.add(each);
return ladder;
}
}
else {
LinkedList<String> temp2 = new LinkedList<String>();
temp2 = (LinkedList<String>) getWordsOneAway(next.peek().toString());
for ( String nextWord:temp2){
@SuppressWarnings("unchecked")
Stack<String> nextTempStack = (Stack<String>) next.clone();
nextTempStack.push(nextWord);
wordQ.add(nextTempStack);
}
buildLadder(start, next.peek().toString());
}
}
Tried using Iterator. Same issue.
Iterator<Stack<String>> myQIter = wordQ.iterator();
while ( myQIter.hasNext()){
Stack<String> tempStack = myQIter.next();
//System.out.println("This is peek: " +tempStack.peek());
if(tempStack.peek().toString().equals(start)){
for(String each:tempStack){
ladder.add(each);
return ladder;
}
}
else {
LinkedList<String> temp2 = new LinkedList<String>();
temp2 = (LinkedList<String>) getWordsOneAway(tempStack.peek().toString());
for ( String nextWord:temp2){
@SuppressWarnings("unchecked")
Stack<String> nextTempStack = (Stack<String>) tempStack.clone();
nextTempStack.push(nextWord);
wordQ.add(nextTempStack);
}
buildLadder(start, tempStack.peek().toString());
}
}
wordQ.add(nextTempStack); This is the issue