我必須在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