Java Reference
In-Depth Information
1
/**
2
* Construct the stack.
3
*/
4
public ArrayStack( )
5
{
6
theArray = (AnyType []) new Object[ DEFAULT_CAPACITY ];
7
topOfStack = -1;
8
}
figure 16.3
The zero-parameter
constructor for the
ArrayStack
class
1
/**
2
* Test if the stack is logically empty.
3
* @return true if empty, false otherwise.
4
*/
5
public boolean isEmpty( )
6
{
7
return topOfStack == -1;
8
}
9
10
/**
11
* Make the stack logically empty.
12
*/
13
public void makeEmpty( )
14
{
15
topOfStack = -1;
16
}
figure 16.4
The
isEmpty
and
makeEmpty
routines for
the
ArrayStack
class
1
/**
2
* Insert a new item into the stack.
3
* @param x the item to insert.
4
*/
5
public void push( AnyType x )
6
{
7
if( topOfStack + 1 == theArray.length )
8
doubleArray( );
9
theArray[ ++topOfStack ] = x;
10
}
figure 16.5
The
push
method for
the
ArrayStack
class
A real-life example of amortization is payment of income taxes. Rather
than pay your entire bill on April 15, the government requires that you pay
most of your taxes through withholding. The total tax bill is always the same;
Search WWH ::
Custom Search