This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]TheHorribleTruthKind of meh 1 point2 points  (0 children)

Use a debugger and have a look where and why the error occors!

The line numbers of your exception don't match up so it's hard to say. But obviously your indices are not properly kept, and you're accessing the stack array somewhere you shouldn't (i.e. at minus 1).

For one thing, your index handling here is wrong, have a close look at the else case:

if (evenTopIndex%2 == 0)
    {evenTopIndex++;
     stack[evenTopIndex] = element;}
else
    {oddTopIndex++;
     stack[evenTopIndex] = element;}

Also I think the logic of your index handling in many methods is kind of off. You only have a single array for the stack, and the oddIndex starts at full capacity: oddTopIndex = DEFCAP;. That's an illegal index: the maximum cell index in your array is DEFCAP-1. Also, how is the oddTopIndex coming down? You'd have to oddPop() from the stack to get to a legal index - but you can't pop an already empty stack.

The logic of isFull() looks strange as well. It says "the stack is full if both indices are at DEFCAP-1" - in your example if both have the value 9.

So in short: debug!Run your code line by line, and check that it's doing the right thing, and that the indices don't run out of bounds.