數據結構和算法-006 堆棧

堆棧,都懂得。先進後出。直接看代碼吧,實現以下功能:

創建堆棧

壓入值

彈出值

查看棧頂值

壓入一組值

public class TheStack {
    
    private String [] stackArray;
    
    private int stackSize;
    
    private int topOfStack = -1;
    
    TheStack(int size){
        stackSize = size;    
        stackArray = new String[size];
        Arrays.fill(stackArray, "-1");
    }
    
    public void printStack(){
        
        StringBuffer sb = new StringBuffer("-");
        for (int i = 0; i<stackSize; i++){
            sb.append("-----");
        }
        
        String septalLine= sb.toString();
        
        System.out.println(septalLine);
        for (int i = 0; i<stackSize; i++){
            System.out.print("|  " + i + " ");
        }
        System.out.println("|");
        System.out.println(septalLine);
        for (int i = 0; i<stackSize; i++){
            if(stackArray[i].equals("-1"))
                System.out.print("|    ");
            else
             System.out.print("| " + stackArray[i] + " ");
        }
        System.out.println("|");
        System.out.println(septalLine);
    }
    
    public void push(String input){
        if(topOfStack+1<stackSize){
            topOfStack++;
            stackArray[topOfStack] = input;        
        } else System.out.println("The stack is full");
    }
    
    public String pop(){
        if (topOfStack>=0){
            stackArray[topOfStack]="-1";
            return stackArray[topOfStack--];
        } else {
            System.out.println("Stack is Empty");
            return "-1";
        }
    }
    
    public String peek(){
        if(topOfStack >=0){
            return stackArray[topOfStack];
        }else {
            System.out.println("Stack is Empty");
            return "-1";
        }
    }
    
    public void pushMany(String multipleValue){
        String [] manyValues = multipleValue.split(" ");
        for(int i =0; i<manyValues.length; i++){
            push(manyValues[i]);
        }
    }

    public static void main(String[] args) {
        System.out.println("Create a Stack");
        TheStack stack = new TheStack(10);
        stack.printStack();
        System.out.println();
        
        System.out.println("Push first value 10");
        stack.push("10");
        stack.printStack();
        System.out.println();
        
        System.out.println("Push Second value 11");
        stack.push("11");
        stack.printStack();
        System.out.println();
        
        System.out.println("Pop top value in the stack");
        System.out.println("The value popped up is: " + stack.pop());
        stack.printStack();
        System.out.println();
        
        System.out.println("Peek top value in the stack");
        System.out.println("The value is " + stack.peek());
        stack.printStack();
        System.out.println();

        System.out.println("Push a couple of values in the stack");
        stack.pushMany("12 14 54 56 43");
        stack.printStack();
        System.out.println();
    }

}

輸出結果

Create a Stack
---------------------------------------------------
|  0 |  1 |  2 |  3 |  4 |  5 |  6 |  7 |  8 |  9 |
---------------------------------------------------
|    |    |    |    |    |    |    |    |    |    |
---------------------------------------------------

Push first value 10
---------------------------------------------------
|  0 |  1 |  2 |  3 |  4 |  5 |  6 |  7 |  8 |  9 |
---------------------------------------------------
| 10 |    |    |    |    |    |    |    |    |    |
---------------------------------------------------

Push Second value 11
---------------------------------------------------
|  0 |  1 |  2 |  3 |  4 |  5 |  6 |  7 |  8 |  9 |
---------------------------------------------------
| 10 | 11 |    |    |    |    |    |    |    |    |
---------------------------------------------------

Pop top value in the stack
The value popped up is: -1
---------------------------------------------------
|  0 |  1 |  2 |  3 |  4 |  5 |  6 |  7 |  8 |  9 |
---------------------------------------------------
| 10 |    |    |    |    |    |    |    |    |    |
---------------------------------------------------

Peek top value in the stack
The value is 10
---------------------------------------------------
|  0 |  1 |  2 |  3 |  4 |  5 |  6 |  7 |  8 |  9 |
---------------------------------------------------
| 10 |    |    |    |    |    |    |    |    |    |
---------------------------------------------------

Push a couple of values in the stack
---------------------------------------------------
|  0 |  1 |  2 |  3 |  4 |  5 |  6 |  7 |  8 |  9 |
---------------------------------------------------
| 10 | 12 | 14 | 54 | 56 | 43 |    |    |    |    |
---------------------------------------------------



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章