|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.ObjectFlexOr.container.Stack
public class Stack
The stack implements container which has the basic add and remove methods that a stack needs. Storage can be implemented by any Sequence type with the default being a singly linked list. The choice of storage model is made at stack creation time. The front of the sequence is the top of the stack.
Note that using Vector (not possible until Vector implements Sequence) gives O(n) gives O(n) performance for remove() (pop) as the entire contents of the Vector is shifted. For a Vector it would be better to use the tail end but this would give poor performance for a singly linked list implement for pop as it would be O(n) due needing to find the predecessor. For an array implementation of stack storage it would be best to use a CircularArray -- element[0] follows element[size-1] creating a ring-like structure.
SLList
,
Sequence
,
Container
Constructor Summary | |
---|---|
Stack()
The default constructor selects a singly linked list as the storage container. |
|
Stack(Sequence container)
The constructor permits the user to specify the type of storage container. |
Method Summary | |
---|---|
void |
add(java.lang.Object obj)
Adds obj to the top of the stack. |
java.lang.Object |
clone()
Return a shallow copy of the stack. |
boolean |
contains(java.lang.Object obj)
Determines whether an element is in the stack. |
java.util.Enumeration<java.lang.Object> |
elements()
Returns an enumerator over the whole stack. |
boolean |
equals(java.lang.Object obj)
Check for equality of contents of two stacks. |
boolean |
isEmpty()
Check if the stack is empty. |
boolean |
isFull()
Check if the stack is full. |
java.lang.Object |
remove()
Return the top element in the stack. |
void |
removeAll()
Empty the stack. |
int |
size()
Returns the number of elements in the stack. |
java.lang.String |
toString()
Return a string representation of the contents. |
Methods inherited from class java.lang.Object |
---|
getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
public Stack()
public Stack(Sequence container)
The input container is cloned to minimize side effects of sharing.
Requires: container is empty Ensures: the stack uses the input storage type.
NotEmptyException
- thrown when expecting an empty container but getting
a non empty container.Method Detail |
---|
public boolean contains(java.lang.Object obj)
contains
in interface Container
public java.util.Enumeration<java.lang.Object> elements()
elements
in interface Container
NoSuchElementException
- when nextElement is called with an empty
enumeration sequence.public boolean isEmpty()
isEmpty
in interface Container
public boolean isFull()
isFull
in interface Container
public int size()
size
in interface Container
public void add(java.lang.Object obj)
obj
to the top of the stack.
Requires: True Ensures: sequence = obj ^ old sequence
add
in interface Container
public void removeAll()
Requires: True Ensures: sequence = null // the empty sequence.
removeAll
in interface Container
public java.lang.Object remove()
Requires: isEmpty = false Ensures: old sequence = obj ^ sequence
remove
in interface Container
public java.lang.Object clone()
Requires: True Ensures: result = copy of stack
clone
in interface Container
clone
in class java.lang.Object
public java.lang.String toString()
Requires: True Ensures: result = string representation of contents
toString
in interface Container
toString
in class java.lang.Object
public boolean equals(java.lang.Object obj)
Requires: True Ensures: result = true => contents are equal result = false => contents are not equal
equals
in interface Container
equals
in class java.lang.Object
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |