1
  public void question(int col, int n, Node<Integer> part_soln) {
        if (col==0) stack.push(part_soln);
        else for (int row=1; row<=n; row++)
              { if (!exists(row,part_soln) && !unsafe(col,row,col+1,part_soln)) 
                  { Node<Integer> new_soln = new Node<Integer>(row,part_soln);
                    question(col-1,n,new_soln);
                  }
              }
    }  

i was inserting part_soln into the stack, but now i want to get the first part_soln instead of the stack, i will break the loop once i get the part_soln, i modify the question become

  public void question(int col, int n, Node<Integer> part_soln) {
        if (col==0) return part_soln;
        else for (int row=1; row<=n; row++)
              { if (!exists(row,part_soln) && !unsafe(col,row,col+1,part_soln)) 
                  { Node<Integer> new_soln = new Node<Integer>(row,part_soln);
                    question(col-1,n,new_soln);
                  }
              }
return null;
}

the problem occurs, i cant get the first element in the stack but keep getting "null" as an answer, any suggestion?

3
  • With what parameters do you call question? Isn't it supposed to return a Node<Integer> instead of void? Commented Oct 21, 2010 at 13:01
  • Do you want to do something with the return value when you call question in question(col-1,n,new_soln)? Commented Oct 21, 2010 at 13:04
  • Are you the same person as user478763? If so then that is six completely unanswerable questions you have asked here now, generating a total reputation of 4 (and probably going down). Commented Oct 21, 2010 at 13:56

1 Answer 1

2

In the second version of question(), you have only two return statements, and the second returns null whenever col != 0.

So you seem to have messed up your recursion scheme, because even though question() calls itself recursively within the loop, the return value is not used.

It would help to know what the method is supposed to do. But anyway, my attempt to fix it based on the information you provided (you want to find and return the first suitable solution) is

public Node<Integer> question(int col, int n, Node<Integer> part_soln) {
    if (col==0) 
        return part_soln;
    else for (int row=1; row<=n; row++) {
        if (!exists(row,part_soln) && !unsafe(col,row,col+1,part_soln)) {
            Node<Integer> new_soln = new Node<Integer>(row,part_soln);
            Node<Integer> ret = question(col-1,n,new_soln);
            if (ret != null)
                return ret;
        }
    }
    return null;
}

The difference is that I store the return value from the recursive call and if it is not null, return it immediately.

Sign up to request clarification or add additional context in comments.

1 Comment

@rsp, oops, good catch, thanks. I noticed and commented the problem in the OP then failed to correct it in my version. Now fixed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.