public class LinkedStack<Item> extends java.lang.Object implements java.lang.Iterable<Item>
This implementation uses a singly-linked list with a non-static nested class for linked-list nodes. See the Stack class for a version that uses a static nested class. The push, pop, peek, size, and is-empty operations all take constant time in the worst case.
For additional documentation, see algs4.cs.princeton.edu/13stacks/ or Section 1.3, page 120, of "Algorithms, 4th Edition", by Robert Sedgewick and Kevin Wayne.
Constructor and Description |
---|
LinkedStack()
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 this stack in LIFO order.
|
static void |
main(java.lang.String[] args)
Unit test of this LinkedStack data type.
|
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 this 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)