ObjectStack<Item>
public class Stack<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 |
|---|
public Stack()
| Method Detail |
|---|
public boolean isEmpty()
public int size()
public void push(Item item)
public Item pop()
public Item peek()
public String toString()
toString in class Objectpublic java.util.Iterator<Item> iterator()
iterator in interface Iterable<Item>public static void main(String[] args)