practice
Class Stack<T>

java.lang.Object
  extended by practice.Collection
      extended by practice.Stack<T>
Type Parameters:
T - type of the elements stored in this stack.

public class Stack<T>
extends Collection

This class represents a stack. A stack is a collection. The method to add elements is called push. The method to remove elements is called pop.

The element that is last added is first removed. Consider the following snippet.

 Stack stack = new Stack();
 stack.push(2);
 stack.push(5);
 System.out.println(stack.pop());
 System.out.println(stack.pop());
 
It produces the following output.
 5
 2
 


Constructor Summary
Stack()
          Creates an empty stack.
 
Method Summary
 boolean equals(Object object)
          Tests if this stack is the same as the given object.
 T pop()
          Removes the top element of this stack.
 void push(T element)
          Adds the given element to this stack.
 String toString()
          Returns a string representation of this stack.
 
Methods inherited from class practice.Collection
add, isEmpty, remove, size
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Stack

public Stack()
Creates an empty stack.

Method Detail

push

public void push(T element)
Adds the given element to this stack.

Parameters:
element - the element to be added.

pop

public T pop()
      throws Exception
Removes the top element of this stack. The element at the top is the last element that has been added.

Throws:
Exception - if this stack is empty.
Returns:
the top element of this stack.

equals

public boolean equals(Object object)
Tests if this stack is the same as the given object. Two stacks are the same if they contain the same elements in the same order.

Overrides:
equals in class Collection
Parameters:
object - an object.
Returns:
true if this stack is the same as the given object, false otherwise.

toString

public String toString()
Returns a string representation of this stack. This representation consists of the elements, from top to bottom, separated by a colon.

Overrides:
toString in class Object
Returns:
a string representation of this stack.