Class Stack<Item>

Object
  extended by Stack<Item>
All Implemented Interfaces:
Iterable<Item>

public class Stack<Item>
extends Object
implements Iterable<Item>

The Stack class represents a last-in-first-out (LIFO) stack of generic items. It supports the usual push and pop operations, along with methods for peeking at the top item, testing if the stack is empty, and iterating through the items in LIFO order.

All stack operations except iteration are constant time.

For additional documentation, see Section 4.3 of Introduction to Programming in Java: An Interdisciplinary Approach by Robert Sedgewick and Kevin Wayne.


Constructor Summary
Stack()
          Create an empty stack.
 
Method Summary
 boolean isEmpty()
          Is the stack empty?
 java.util.Iterator<Item> iterator()
          Return an iterator to the stack that iterates through the items in LIFO order.
static void main(String[] args)
          A test client.
 Item peek()
          Return the item most recently added to the stack.
 Item pop()
          Delete and return the item most recently added to the stack.
 void push(Item item)
          Add the item to the stack.
 int size()
          Return the number of items in the stack.
 String toString()
          Return string representation.
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Stack

public Stack()
Create an empty stack.

Method Detail

isEmpty

public boolean isEmpty()
Is the stack empty?


size

public int size()
Return the number of items in the stack.


push

public void push(Item item)
Add the item to the stack.


pop

public Item pop()
Delete and return the item most recently added to the stack. Throw an exception if no such item exists because the stack is empty.


peek

public Item peek()
Return the item most recently added to the stack. Throw an exception if no such item exists because the stack is empty.


toString

public String toString()
Return string representation.

Overrides:
toString in class Object

iterator

public java.util.Iterator<Item> iterator()
Return an iterator to the stack that iterates through the items in LIFO order.

Specified by:
iterator in interface Iterable<Item>

main

public static void main(String[] args)
A test client.