/Users/petercappello/NetBeansProjects/56-2014/56-2014-Generics/src/Stack.java |
import java.util.LinkedList;
A stack of objects.
@author
@param <T>
public class Stack<T>
{
private final LinkedList<T> stack = new LinkedList<>();
Pop an element off the stack and return that element.
@return
public T pop() { return isEmpty() ? null : stack.pop(); }
public void push( T t ) { stack.push( t ); }
public boolean isEmpty() { return stack.isEmpty(); }
public static void main( String[] args )
{
Stack<Integer> myStack = new Stack<>();
myStack.push( -3 );
myStack.push( 73 );
myStack.push( 953 );
System.out.println("myStack.pop(): " + myStack.pop() );
System.out.println("myStack.pop(): " + myStack.pop() );
System.out.println("myStack.pop(): " + myStack.pop() );
System.out.println("myStack.pop(): " + myStack.pop() );
Stack myRawStack = new Stack();
myRawStack.push( 9 );
myRawStack.push( 9.0 );
myRawStack.push( true );
System.out.println("myRawStack.pop(): " + myRawStack.pop() );
System.out.println("myRawStack.pop(): " + myRawStack.pop() );
System.out.println("myRawStack.pop(): " + myRawStack.pop() );
System.out.println("myRawStack.pop(): " + myRawStack.pop() );
Stack<Number> myPolymorphicStack = new Stack<>();
myPolymorphicStack.push( -3 );
myPolymorphicStack.push( 73.0 );
myPolymorphicStack.push( new Float( 20 ) );
System.out.println("myPolymorphicStack.pop(): " + myPolymorphicStack.pop() );
System.out.println("myPolymorphicStack.pop(): " + myPolymorphicStack.pop() );
System.out.println("myPolymorphicStack.pop(): " + myPolymorphicStack.pop() );
System.out.println("myPolymorphicStack.pop(): " + myPolymorphicStack.pop() );
}
}