review 栈 --java实现
2019獨角獸企業重金招聘Python工程師標準>>>
棧 stack是限制僅在表的一端進行插入和刪除操作的線性表。其特點為先進后出FILO;
Demo代碼如下:
class StackX {
private int maxSize;
private long[] stackArray;
private int top;
public StackX(int s) {
// Initial stack
maxSize = s;
stackArray = new long[maxSize];
// top
top = -1;
}
/**
* push elements to stack
*/
public void push(long j) {
// top指針向上移動一位
stackArray[++top] = j;
}
public long peek() {
????return stackArray[top];
}
public long pop() {
????return stackArray[top--];
}
public boolean isEmpty() {
????return top == -1;
}
public boolean isFull() {
????return top == maxSize - 1;
}
}
public class StackApp {
public static void main(String[] args) {
????StackX theStack = new StackX(10);
????// 向棧中壓入元素
????theStack.push(10);
????theStack.push(20);
????theStack.push(30);
????theStack.push(40);
????theStack.push(50);
System.out.println("獲取棧中元素--彈出:");
????while (!theStack.isEmpty()) {
????????long value = theStack.pop();
????????System.out.print(value + " ?");
????}
?}
}
轉載于:https://my.oschina.net/u/938966/blog/108623
總結
以上是生活随笔為你收集整理的review 栈 --java实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: iOS网络编程之同步、异步、请求队列
- 下一篇: Instance and Media R