class MyStack<T> {
protected Object[] stack;
protected int size;
protected int tos = -1;
MyStack(T ...objs) {
this.size = 16;
this.stack = new Object[size];
for (T obj: objs) {
this.push(obj);
}
}
public void push(T obj) {
if (this.tos == this.size - 1) {
Object[] newStack = new Object[2 * this.size];
for (int i = 0; i < this.size; i++) {
newStack[i] = this.stack[i];
}
this.stack = newStack;
this.size = 2 * this.size;
}
this.tos++;
this.stack[this.tos] = (Object)obj;
}
public T pop() {
if (this.tos == -1) {
throw new ArrayIndexOutOfBoundsException("no elements");
}
T elem = (T)this.stack[this.tos];
this.tos--;
return elem;
}
public int size() {
return this.tos + 1;
}
}
class Main {
public static void main(String[] args) {
MyStack<Integer> stack = new MyStack<Integer>();
for (int i = 0; i < 50; i++) {
stack.push(i);
}
while (stack.size() > 0) {
System.out.println(stack.pop());
}
}
}
To embed this program on your website, copy the following code and paste it into your website's HTML: