I am writing an N-queens problem using stacks in C. I am only doing boards of size 4 thru 8. I initially had my prog displaying the first solution for boards of those sizes, however, i needed it to display all solutions. When i finally got it to display all solutions, it would get to the first non-solution and instead of jumping out would print the non-solution over and over until i ^C to get out. So it is not doing that any more but now i am popping an empty stack somewhere in my program. I'm pretty sure which function it is and I think I need to add code, not change something i already have, but i have been racking my brain all night and getting nothing. Is there anyone who can help me. I think i am probalby missing one line of code here somewhere but can't figure out what it is. This is my fillBoard function (I thinkthat is where i'm missing something ):
Code:
void FillBoard(STACK *currSolution, STACK *lastSolution, int BoardSize)
{
int row;
int col;
int board[9][9] = {{0}}; /*0 no queens: 1 queen*/
ELEMENT *place;
place = (ELEMENT *)malloc(sizeof(ELEMENT));
if(emptyStack(lastSolution))
{
row = 0;
col = -1;
}
else
{
while(!emptyStack(lastSolution))
{
pop(lastSolution, place);
push(currSolution, *place);
board[place->row][place->col] = 1;
row = place->row;
col = place->col;
}
pop(currSolution, place);
board[row][col] = 0;
if( col == BoardSize )
{
pop(currSolution, place);
row = place->row;
col = place->col;
board[row][col] = 0;
}
}
while(row < BoardSize)
{
while(col < BoardSize && row < BoardSize)
{
col++;
if(!guarded(board, row, col, BoardSize))
{
board[row][col] = 1;
place->row = row;
place->col = col;
push(currSolution, *place);
row++;
col = -1;
}
while(col >= BoardSize - 1)
{
pop(currSolution,place);
row = place->row;
col = place->col;
board[row][col] = 0;
}
}
}
free(place);
return;
}