我必须在java中实现一些堆栈的函数,并选择一个练习来解决,例如,在堆栈的末尾插入一个新元素而不会中断顺序。我怎样才能做到这一点?在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
[你有什么尝试?](http://www.whathaveyoutried.com/)另外,“堆栈结束”的意思是推?或者把它作为第一个元素?因为这不是一个堆栈应该如何运作的。 –
你有什么尝试?你对这个*如何工作有什么想法?你卡在哪里? – Polygnome
这是功课吗? – aglassman