⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stackx.java

📁 6个经典数据结构JAVA解法
💻 JAVA
字号:
/**
 * 栈(stack):后进先出(LIFO)
 * 数据项入栈和出栈的时间复杂度都为常数O(1)。即栈操作所耗的时间不依赖于栈中数据项
 * 的个数。栈不需要比较和移动操作。
*/
class StackX {
    
    private int maxSize; // size of stack array
    private long[] stackArray;
    private int top; // top of stack
    
    /**
     * 构建器(初始化栈的参数)
     * @param int s
     */
    public StackX(int s) { // constructor
        maxSize = s; // set array size
        stackArray = new long[maxSize]; // create array
        top = -1; // no items yet
    }
    /**
     * 入栈
     * @param long j
     */
    public void push(long j) { // put item on top of stack
        stackArray[++top] = j; // increment top, insert item
        System.out.println("top == " + top);
    }
    /**
     * 出栈     
     */
    public long pop() { // take item from top of stack
        System.out.println("top ==== " + top);
        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);
    }
    /**
     * 主函数,测试。
     * @param String[] args
     */

    public static void main(String[] args) {
        StackX theStack = new StackX(10); // make new stack
        theStack.push(1); // push items onto stack
        theStack.push(2);
        theStack.push(3);
        theStack.push(4);
        while (!theStack.isEmpty()) { // until it's empty, delete item from stack
            long value = theStack.pop();
            System.out.print(value); // display it
            System.out.print(" ");
        } // end while
        System.out.println("");
    } // end main()
} // end class StackX

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -