|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object edu.cmu.cs.pattis.cs151xx.orderedCollections.AbstractOrderedCollection<E> edu.cmu.cs.pattis.cs151xx.orderedCollections.AbstractStack<E> edu.cmu.cs.pattis.cs151xx.orderedCollections.LinkedStack<E>
public class LinkedStack<E>
This class implements the OrderedCollection
interface
with LIFO behavior: last in/first out.
It is backed by a linked list.
It makes a guarantee that the iteration order is LIFO.
This class permits the null
element.
This class offers constant time performance for the basic operations
(add
, remove
, peek
, and
size
).
Iterating over this set requires time proportional to the size of the
stack.
Note that this implementation is not synchronized. If multiple threads access a stack concurrently, and at least one of the threads modifies the stack, it must be synchronized externally. This is typically accomplished by synchronizing on some object that naturally encapsulates the stack.
The iterators returned by this class's iterator method are fail-fast:
if the stack is modified at any time after the iterator is created,
in any way, the Iterator throws a
ConcurrentModificationException
.
Thus, in the face of concurrent modification, the iterator fails
quickly and cleanly, rather than risking arbitrary,
non-deterministic behavior at an undetermined time in the future.
Constructor Summary | |
---|---|
LinkedStack()
Constructs a new, empty stack. |
|
LinkedStack(java.util.Collection<E> c)
Constructs a new stack containing the elements in the specified collection. |
|
LinkedStack(E[] o)
Constructs a new stack containing the elements in the specified array. |
|
LinkedStack(OrderedCollection<E> o)
Constructs a new stack containing the elements in the specified ordered collection. |
Method Summary | |
---|---|
boolean |
add(E o)
Ensures that this stack contains the specified element. |
void |
clear()
Removes all of the elements from this stack. |
java.util.Iterator<E> |
iterator()
Returns an iterator over the elements in this stack. |
E |
peek()
Returns (without removing) the next elements in this stack, according to LIFO behavior. |
E |
remove()
Returns/removes the next element in this stack, according to LIFO behavior. |
java.lang.String |
toString()
Returns a string representation of this stack. |
Methods inherited from class edu.cmu.cs.pattis.cs151xx.orderedCollections.AbstractStack |
---|
equals |
Methods inherited from class edu.cmu.cs.pattis.cs151xx.orderedCollections.AbstractOrderedCollection |
---|
addAll, hashCode, isEmpty, size, toArray, toArray, toCollection |
Methods inherited from class java.lang.Object |
---|
getClass, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
public LinkedStack()
public LinkedStack(OrderedCollection<E> o)
o
- - the ordered collection whose elements are to be placed
into this stack.public LinkedStack(java.util.Collection<E> c)
c
- - the collection whose elements are to be placed into this
stack.public LinkedStack(E[] o)
o
- - the array whose elements are to be placed into this
stack.Method Detail |
---|
public void clear()
AbstractOrderedCollection
and is more efficient.
clear
in interface OrderedCollection<E>
clear
in class AbstractOrderedCollection<E>
public boolean add(E o)
true
if this collection changed as a result of the
call.
This always happens because stacks allow duplicates.
add
in interface OrderedCollection<E>
add
in class AbstractOrderedCollection<E>
o
- - the element to store in the stack
true
if this stack changed as a
result of the call.public E remove()
remove
in interface OrderedCollection<E>
remove
in class AbstractOrderedCollection<E>
java.util.NoSuchElementException
- if the stack is emptypublic E peek()
peek
in interface OrderedCollection<E>
peek
in class AbstractOrderedCollection<E>
java.util.NoSuchElementException
- if the stack is emptypublic java.util.Iterator<E> iterator()
iterator
over the elements in this stack.
The order is guaranteed to be exhibit LIFO behavior.
iterator
in interface OrderedCollection<E>
iterator
in interface java.lang.Iterable<E>
iterator
in class AbstractOrderedCollection<E>
iterator
over the elements in this stackpublic java.lang.String toString()
This implementation creates an empty string buffer, appends a left square bracket, and iterates over the collection appending the string representation of each element in turn. After appending each element except the last, the string ", " is appended. Finally a right bracket is appended. A string is obtained from the string buffer, and returned.
toString
in class AbstractOrderedCollection<E>
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |