Java Int Stack Class
// 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;
}
}