/************************************************************************* * This is a stack implementation that extends the LinkedList class * *************************************************************************/ import java.util.*; public class StackByInheritance extends LinkedList { public StackByInheritance() { super(); } /** * Inserts an item onto the top of the stack. */ public void push(T x) { addFirst(x); } /** * Removes and returns the item at the top of this stack. */ public T pop( ) throws StackException { if(isEmpty()) throw new StackException("Stack is empty"); else return remove(); } /** * Returns the top item without its removal */ public T peek() throws StackException { return get(0); } public static void main(String[] args) { StackByInheritance stk = new StackByInheritance(); stk.push(1); stk.push(5); stk.push(10); System.out.println(stk); System.out.println( stk.peek() ); stk.pop(); System.out.println( stk.peek() ); } } /** StackException **/ class StackException extends RuntimeException { public StackException(String name) { super(name); } public StackException() { super(); } }