我必须在java中实现一些堆栈的功能,并选择一个练习来解决,例如,在堆栈末尾插入一个新元素而不破坏顺序。我怎样才能做到这一点?
// stack.java
// demonstrates stacks
// to run this program: C>java StackApp
////////////////////////////////////////////////////////////////
class Stack
{
private int maxSize; // size of stack array
private long[] stackArray;
private int top; // top of stack
//--------------------------------------------------------------
public Stack(int s) // constructor
{
maxSize = s; // set array size
stackArray = new long[maxSize]; // create array
top = -1; // no items yet
}
//--------------------------------------------------------------
public void makeEmpty( ) {
top = -1;
}
public void push(long j) // put item on top of stack
{
stackArray[++top] = j; // increment top, insert item
}
//--------------------------------------------------------------
public long pop() // take item from top of stack
{ return stackArray[top--]; // access item, decrement top
}
//--------------------------------------------------------------
public long peek() // peek at top of stack
{ return stackArray[top];
}
//--------------------------------------------------------------
public boolean isEmpty() // true if stack is empty
{return (top == -1);
}
//--------------------------------------------------------------
public boolean isFull() // true if stack is full
{return (top == maxSize-1);
}
}
//--------------------------------------------------------------
// end class StackX