interface Stack {
public void push(int elt);
public int pop();
public boolean isEmpty();
}
public class ArrayStack implements Stack{
private final int MAX_ELEMENTS = 10;
private int[] stack;
private int index;
public ArrayStack() {
stack = new int[MAX_ELEMENTS];
index = -1;
}
public void push(int elt) {
if (index != stack.length - 1) {
index++; //1
stack[index] = elt; //2
} else {
throw new IllegalStateException("stack overflow");
}
}
public int pop() {
if (index != -1) {
return stack[index--];
} else {
throw new IllegalStateException("stack underflow");
}
}
public boolean isEmpty() { return index == -1; }
}
public void push(int elt);
public int pop();
public boolean isEmpty();
}
public class ArrayStack implements Stack{
private final int MAX_ELEMENTS = 10;
private int[] stack;
private int index;
public ArrayStack() {
stack = new int[MAX_ELEMENTS];
index = -1;
}
public void push(int elt) {
if (index != stack.length - 1) {
index++; //1
stack[index] = elt; //2
} else {
throw new IllegalStateException("stack overflow");
}
}
public int pop() {
if (index != -1) {
return stack[index--];
} else {
throw new IllegalStateException("stack underflow");
}
}
public boolean isEmpty() { return index == -1; }
}
No comments:
Post a Comment