public class LinkedListStack implements Stack {
private class Node {
A item;
Node next;
Node(A item, Node next) {
this.item = item;
this.next = next;
}
}
Node head = null;
public boolean isEmpty() {
return head == null;
}
public void push(A x) {
head = new Node(x, head);
}
public A pop() throws Exception {
A result = head.item;
head = head.next;
return result;
}
public static void main(String[] args) {
LinkedListStack myStack = new LinkedListStack();
try {
myStack.push(10);
myStack.push(9);
System.out.println(myStack.pop());
myStack.push(4);
myStack.push(2);
System.out.println(myStack.pop());
System.out.println(myStack.pop());
System.out.println(myStack.pop());
System.out.println(myStack.isEmpty());
} catch (Exception e) {
}
}
}