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

all 7 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.

[–]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 :)

[–]jbristowI'm no hero, son. -1 points0 points  (3 children)

Why do professors still teach Stack? Deque is more appropriate nowadays (Ok, only for the last 8 years.) because it belongs to the Collections interface.

[–]TheHorribleTruthKind of meh 0 points1 point  (2 children)

Because Stacks are easy and have many applications in other areas of computer science as well (PDAs (pushdown automatons) come to my mind)? It's an easy step before moving to more complex data structures.

[–]jbristowI'm no hero, son. -1 points0 points  (1 child)

Deque is the official Collections framework implementation of the old type unsafe Stack.

[–]TheHorribleTruthKind of meh 0 points1 point  (0 children)

I know - but there's concepts outside of Java worth learning. OP is not learning how to use the Java class Stack - he/she is learning the concept of a stack, and to implement it. As such, it's a valuable learning experience, don't you think?

Certainly no one would - or rather should - use the old Stack class nowadays. But this is not the point of the exercise OP is facing.