Skip to content
Advertisement

Is this a stack?

Is this code a stack?

How I can make it better?

This is my first one.

Can i do this using ArrayList?

public class Stack implements IADT {
private final int[] stackArray = new int[10];
private int top;
private int nr;
public Stack(){
    top = -1;
}
@Override
public String pop() {
    return Integer.toString(stackArray[top--]);
}
@Override
public String peek() {
    return Integer.toString(stackArray[top]);
}
@Override
public void push(String value) {
    //...
}

I didn’t added the isEmpty() method.

Advertisement

Answer

Yes, it is. But you can add checking for overflow, underflow.

And this will be better if you’ll try using collections.

2 People found this is helpful
Advertisement