public class ResizingArrayStack<Item> extends java.lang.Object implements java.lang.Iterable<Item>
This implementation uses a resizing array, which double when it is full and halves when it is one-quarter full. The push and pop operations take constant amortized time. The size, peek, and is-empty operations takes constant time in the worst case.
For additional documentation, see Section 1.3, p.120, of "Algorithms, 4th Edition", by Robert Sedgewick and Kevin Wayne.
Constructor and Description |
---|
ResizingArrayStack()
Initializes an empty stack.
|
Modifier and Type | Method and Description |
---|---|
boolean |
isEmpty()
Is this stack empty?
Returns true if this stack is empty; false otherwise.
|
java.util.Iterator<Item> |
iterator()
Returns an iterator that iterates through the items
of the stack in LIFO order.
|
static void |
main(java.lang.String[] args)
Unit test of this ResizingArrayStack class.
|
Item |
peek()
Returns (but does not remove) the item most recently added
to this stack.
|
Item |
pop()
Removes and returns the item most recently added to this stack.
|
void |
push(Item item)
Adds item to this stack.
|
int |
size()
Returns the number of items in the stack.
|
public boolean isEmpty()
public int size()
public void push(Item item)
public Item pop()
public Item peek()
public java.util.Iterator<Item> iterator()
iterator
in interface java.lang.Iterable<Item>
public static void main(java.lang.String[] args)