Stack
Stack
public FixedSizeStack(int n) {
stackData = new int[n];
top = -1;
}
@Override
public void push(int element) {
if (isFull()) {
throw new IllegalStateException("Stack is full. Cannot push element.");
}
stackData[++top] = element;
}
@Override
public int pop() {
if (isEmpty()) {
throw new IllegalStateException("Stack is empty. Cannot pop element.");
}
return stackData[top--];
}
@Override
public int peek() {
if (isEmpty()) {
throw new IllegalStateException("Stack is empty. Cannot peek
element.");
}
return stackData[top];
}
@Override
public boolean isEmpty() {
return top == -1;
}
@Override
public boolean isFull() {
return top == (stackData.length - 1);
}
}
//