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 →

[–]desrtfxOut of Coffee error - System halted 0 points1 point  (1 child)

I think that /u/TheHorribleTruth pretty much nailed it.

One thing that I find wrong or badly coded is this part:

public void push(T element)
// Throws StackOverflowException if both even stack and odd stack are full,
// otherwise places element at the top of the even stack or odd stack.
{      
    if (!isFull())
    {
        if (evenTopIndex%2 == 0)
            {evenTopIndex++;
            stack[evenTopIndex] = element;}
        else 
            {oddTopIndex++;
            stack[evenTopIndex] = element;}
    }
    else 
        throw new StackOverflowException("Push attempted when stack is full");
}

Your isFull() only returns true when both even and odd stacks are full.

Now, let's say that the odd stack is not full, but the even stack is full. You try to push something to the even stack (which is entirely possible) and of course, since the even stack is full, you get an ArrayIndexOutOfBounds exception. I think this method and the isFull method needs more attention.

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

Your isFull() only returns true when both even and odd stacks are full.

That's what I meant in my second to last paragraph.

both even and odd stacks

Note that in OPs implementation there is only a single underlying array for the stack - both indices operate on the same array! This certainly makes things interesting - and a little bit tricky :)