public class Stack<Item> extends java.lang.Object implements java.lang.Iterable<Item>
This implementation uses a singly-linked list with a static nested class
for linked-list nodes. For the version from the textbook, that uses a
non-static nested class, see LinkedStack
.
The push, pop, peek, size, and isEmpty operations all take constant time in the worst case.
For additional documentation, see Section 1.3, page 121, of "Algorithms", 4th Edition, by Robert Sedgewick and Kevin Wayne.
Constructor and Description |
---|
Stack()
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 to this stack that iterates through the items
in LIFO order.
|
static void |
main(java.lang.String[] args)
Unit test of this Stack 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.
|
java.lang.String |
toString()
Returns a string representation of this stack.
|
public boolean isEmpty()
public int size()
public void push(Item item)
public Item pop()
public Item peek()
public java.lang.String toString()
toString
in class java.lang.Object
public java.util.Iterator<Item> iterator()
iterator
in interface java.lang.Iterable<Item>
public static void main(java.lang.String[] args)