// Implements a stack of integers.
class IntStack {
// Stack node type.
private class StackNode {
StackNode(int d, StackNode n) {
data = d;
next = n;
}
public int data;
public StackNode next;
}
// Head of the stack
private StackNode head = null;
// Push onto the stack.
public void push(int i) {
head = new StackNode(i, head);
}
// Remove from the stack.
public int pop() {
if (head != null) {
// Get the head and remove it.
int retval = head.data;
head = head.next;
// Return the value.
return retval;
} else
return 0;
}
// Find out what's on the top.
public int top() {
if(head == null)
return 0;
else
return head.data;
}
// So, are we empty yet?
public boolean isEmpty() {
return head == null;
}
// Remove all memory from the stack.
public void clear() {
head = null;
}
// Technically, we should probably overload compareTo and clone to
// to just throw, which is the closest Java equivalent to forbidding
// comparing and copying. But it's not quite the same in the Java
// context, so not as useful, and I doubt a Java programmer would find it
// worthwhile.
}