This is a stack implementation using an array.
// Stack implementation with arrays
public class CharStack {
private char [] items;
private int top;
// a constructor, need for every class
public CharStack() {
items = new char [1000];
top = -1;
}
public char pop() {
if(top <= -1) {
System.out.println("Stack underflow");
System.exit(0);
}
// if we got here, the stack is not empty
top--;
return items[top + 1];
}
public void push(char c) {
if (top >= items.length - 1) {
System.out.println("Stack overflow");
System.exit(0);
}
// if we got here, the stack is not full
top++;
items[top]=c;
}
public boolean isEmpty() {
return top == -1;
}
}
This is an example from CSci 2101 course.