Java Reference
In-Depth Information
protected
protected int
int depth = 0 ;
/* The actual stack */
protected
protected int
int [] stack = new
new int
int [ MAX_DEPTH ];
@Override
public
public boolean
boolean empty () {
return
return depth == 0 ;
}
@Override
public
public void
void push ( Integer n ) {
stack [ depth ++] = n ;
}
@Override
public
public Integer pop () {
return
return stack [-- depth ];
}
@Override
public
public Integer peek () {
return
return stack [ depth - 1 ];
}
}
While working on ToyStack2 , I extracted its interface into SimpleStack , which just lists the
operations. At the same time I added the empty() method for some compatibility with the
“standard” java.util.Stack API:
public
public interface
interface SimpleStack
SimpleStack < T > {
/** empty - return true if the stack is empty */
abstract
abstract boolean
boolean empty ();
/** push - add an element onto the stack */
abstract
abstract void
void push ( T n );
/** pop - return and remove the top element */
abstract
abstract T pop ();
/** peek - return the top element but don't remove it */
abstract
abstract T peek ();
}
Search WWH ::




Custom Search