class StackX {
privateint maxSize; // size of stack array
privatedouble[] stackArray;
privateint top; // top of stack
//-------------------------------------------------------------
public StackX(int s) // constructor
{
maxSize = s; // set array size
stackArray = newdouble[maxSize]; // create array
top = -1; // no items yet
}
//-------------------------------------------------------------
publicvoid push(double j) // put item on top of stack
{
stackArray[++top] = j; // increment top, insert item
}
//-------------------------------------------------------------
publicdouble pop() // take item from top of stack
{
returnstackArray[top--]; // access item, decrement top
}
//-------------------------------------------------------------
publicdouble peek() // peek at top of stack
{
returnstackArray[top];
}
//-------------------------------------------------------------
publicboolean isEmpty() // true if stack is empty
{
// - 94 -
return (top == -1);
}
//-------------------------------------------------------------
publicboolean isFull() // true if stack is full
{
return (top == maxSize - 1);
}
//-------------------------------------------------------------
} // end class StackX
栈中,
maxSize:描述栈的大小
stackArray:描述栈的类型数组
top:描述栈的指针
push(double j):在栈里加数据的方法
pop():在栈里去数据的方法
peek():取出当前栈中指针指向的数据
isEmpty():判断栈中是不是还有数据
isFull():判读栈是不是满了