structure
Class AbstractStack
java.lang.Object
|
+--structure.AbstractStructure
|
+--structure.AbstractLinear
|
+--structure.AbstractStack
- All Implemented Interfaces:
- Linear, Stack, Structure
- Direct Known Subclasses:
- StackArray, StackList, StackVector
- public abstract class AbstractStack
- extends AbstractLinear
- implements Stack
An abstract structure implementing features common to all
Last-In, First-Out structures in this package.
Stacks are typically used to store the state of a recursively
solved problem.
The structure package provides several extensions of the AbstractStack class,
each of which has its particular strengths and weaknesses.
Example usage:
To reverse a string using a stack, we would use the following:
public static void main(String[] arguments)
{
if(arguments.length > 0){
AbstractStack
reverseStack = new StackList()
;
String s = arguments[0];
for(int i=0; i < s.length(); i++){
reverseStack.push(new Character(s.charAt(i)))
;
}
while(!reverseStack.AbstractLinear.empty()
){
System.out.print(reverseStack.pop()
);
}
System.out.println();
}
}
- See Also:
Stack
,
StackVector
,
StackList
,
StackArray
Method Summary |
Object |
getFirst()
Deprecated. Please use method get, rather than getFirst! |
Object |
peek()
Fetch a reference to the top element of the stack. |
Object |
pop()
Remove an element from the top of the stack. |
void |
push(Object item)
Add an element from the top of the stack. |
Methods inherited from class java.lang.Object |
, clone, equals, finalize, getClass, notify, notifyAll, registerNatives, toString, wait, wait, wait |
AbstractStack
public AbstractStack()
push
public void push(Object item)
- Add an element from the top of the stack.
- Specified by:
push
in interface Stack
- Parameters:
item
- The element to be added to the stack top.- Postcondition:
- item is added to stack
will be popped next if no intervening add
pop
public Object pop()
- Remove an element from the top of the stack.
- Specified by:
pop
in interface Stack
- Precondition:
- stack is not empty
- Postcondition:
- most recently added item is removed and returned
- Returns:
- The item removed from the top of the stack.
getFirst
public Object getFirst()
- Deprecated. Please use method get, rather than getFirst!
- Fetch a reference to the top element of the stack.
- Specified by:
getFirst
in interface Stack
- Precondition:
- stack is not empty
- Postcondition:
- top value (next to be popped) is returned
- Returns:
- A reference to the top element of the stack.
peek
public Object peek()
- Fetch a reference to the top element of the stack.
Provided for compatibility with java.util.Stack.
- Specified by:
peek
in interface Stack
- Precondition:
- stack is not empty
- Postcondition:
- top value (next to be popped) is returned
- Returns:
- A reference to the top element of the stack.