© 1998-2002 McGraw-Hill

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(Object item)
          Add an element from the top of the stack.
 boolean empty()
          Returns true iff the stack is empty.
 Object get()
          Fetch a reference to the top element of the stack.
 Object getFirst()
          Fetch a reference to the top element of the stack.
 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 to top of stack.
 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(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.
Postcondition:
item is added to stack will be popped next if no intervening add
See Also:
push(java.lang.Object)

push

public void push(Object item)
Add an element to top of stack.
Parameters:
item - The value to be added to the top of the stack.
Postcondition:
item is added to stack will be popped next if no intervening push

remove

public Object remove()
Remove an element from the top of the stack.
Specified by:
remove in interface Linear
Precondition:
stack is not empty
Postcondition:
most recently added item is removed and returned
Returns:
The item removed from the top of the stack.
See Also:
pop()

pop

public Object pop()
Remove an element from the top of the stack.
Precondition:
stack is not empty
Postcondition:
most recently pushed item is removed and returned
Returns:
A reference to the removed element.

get

public Object get()
Fetch a reference to the top element of the stack.
Specified by:
get in interface Linear
Precondition:
stack is not empty
Postcondition:
top value (next to be popped) is returned
Returns:
A reference to the top element of the stack.

getFirst

public Object getFirst()
Fetch a reference to the top element of the 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.
Precondition:
stack is not empty
Postcondition:
top value (next to be popped) is returned
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
Postcondition:
returns true if and only if the stack is empty
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
Postcondition:
returns the number of elements in the stack
Returns:
number of elements in stack.

© 1998-2002 McGraw-Hill