import java.util.LinkedList;
class GenStack<E> {
private LinkedList<E> list = new LinkedList<E>();
publicvoid push(E item) {
list.addFirst(item);
}
public E pop() {
return list.poll();
}
public E peek() {
return list.peek();
}
publicboolean hasItems() {
return !list.isEmpty();
}
publicint size() {
return list.size();
}
}
publicclass GenStackTest {
publicstaticvoid main(String[] args) {
GenStack<String> gs = new GenStack<String>();
gs.push("One");
gs.push("Two");
gs.push("Three");
gs.push("Four");
System.out.println("There are " + gs.size() + " items in the stack.\n");
System.out.println("The top item is: " + gs.peek() + "\n");
System.out.println("There are still " + gs.size() + " items in the stack.\n");
System.out.println("Popping everything:");
while (gs.hasItems())
System.out.println(gs.pop());
System.out.println("There are now " + gs.size() + " items in the stack.\n");
System.out.println("The top item is: " + gs.peek() + "\n");
}
}