Java Generic
import java.util.*;
// Simple Java array-based stack template class.
public class Stack<T>
{
private Vector<T> data; // Should be T[] data;, but Java forbids it.
private int top_elt;
// Create an empty stack of a given size.
public Stack(int size)
{
data = new Vector<T>(size);
data.setSize(size);
top_elt = -1;
}
// Add an element to the stack. Note that you must send a pointer,
// and the stack actually stores the pointer.
public void push(T e)
{
data.set(++top_elt, e);
}
// Pop the stack and return the top item pointer. If the stack is
// empty, return the null pointer.
public T pop()
{
if(empty()) return null;
else return data.get(top_elt--);
}
// Return the top item pointer. If none, return the null pointer.
public T top()
{
if(empty()) return null;
return data.get(top_elt);
}
// Tell if the stack is empty.
public Boolean empty() { return top_elt == -1; }
}