Package algs13
Class Stack<T>
java.lang.Object
algs13.Stack<T>
- All Implemented Interfaces:
Iterable<T>
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 1.3 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionboolean
isEmpty()
Is the stack empty?iterator()
Return an iterator to the stack that iterates through the items in LIFO order.static void
A test client.static void
static void
peek()
Return the item most recently added to the stack.pop()
Delete and return the item most recently added to the stack.void
Add the item to the stack.int
size()
Return the number of items in the stack.toString()
Return string representation.Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
Stack
public Stack()Create an empty stack.
-
-
Method Details
-
isEmpty
Is the stack empty? -
size
Return the number of items in the stack. -
push
Add the item to the stack. -
pop
Delete and return the item most recently added to the stack.- Throws:
NoSuchElementException
- if stack is empty.
-
peek
Return the item most recently added to the stack.- Throws:
NoSuchElementException
- if stack is empty.
-
toString
Return string representation. -
iterator
Return an iterator to the stack that iterates through the items in LIFO order. -
main
A test client. -
main1
-
main2
-