structure
Interface Stack

All Superinterfaces:
Linear, Structure
All Known Implementing Classes:
AbstractStack, StackArray, StackList, StackVector

public interface Stack
extends Linear

An interface describing a Last-In, First-Out structure. Stacks are typically used to store the state of a recursively solved problem. The structure package provides several implementations of the Stack interface, 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){
	   Stack reverseStack = new StackList();
	   String s = arguments[0];
	    
	   for(int i=0; i < s.length(); i++){
	       reverseStack.push(new Character(s.charAt(i)));
	   }

	   while(!reverseStack.empty()){
	       System.out.print(reverseStack.pop());
	   }

	   System.out.println();
     }
 }
 


Method Summary
 void add(java.lang.Object item)
          Add an element from the top of the stack.
 boolean empty()
          Returns true iff the stack is empty.
 java.lang.Object get()
          Fetch a reference to the top element of the stack.
 java.lang.Object getFirst()
          Fetch a reference to the top element of the stack.
 java.lang.Object peek()
          Fetch a reference to the top element of the stack.
 java.lang.Object pop()
          Remove an element from the top of the stack.
 void push(java.lang.Object item)
          Add an element to top of stack.
 java.lang.Object remove()
          Remove an element from the top of the stack.
 int size()
          Returns the number of elements in the stack.
 
Methods inherited from interface structure.Structure
clear, contains, elements, isEmpty, iterator, remove, values
 

Method Detail

add

public void add(java.lang.Object item)
Add an element from the top of the stack.

Specified by:
add in interface Linear
Parameters:
item - The element to be added to the stack top.
See Also:
push(java.lang.Object)

push

public void push(java.lang.Object item)
Add an element to top of stack.

Parameters:
item - The value to be added to the top of the stack.

remove

public java.lang.Object remove()
Remove an element from the top of the stack.

Specified by:
remove in interface Linear
Returns:
The item removed from the top of the stack.
See Also:
pop()

pop

public java.lang.Object pop()
Remove an element from the top of the stack.

Returns:
A reference to the removed element.

get

public java.lang.Object get()
Fetch a reference to the top element of the stack.

Specified by:
get in interface Linear
Returns:
A reference to the top element of the stack.

getFirst

public java.lang.Object getFirst()
Fetch a reference to the top element of the stack.

Returns:
A reference to the top element of the stack.

peek

public java.lang.Object peek()
Fetch a reference to the top element of the stack.

Returns:
A reference to the top element of the stack.

empty

public boolean empty()
Returns true iff the stack is empty. Provided for compatibility with java.util.Vector.empty.

Specified by:
empty in interface Linear
Returns:
True iff the stack is empty.

size

public int size()
Returns the number of elements in the stack.

Specified by:
size in interface Linear
Returns:
number of elements in stack.