java array simulation stack

Introduction of l stack

1) The stack is a sequential list of FILO-First In Last Out.

2) stack is a special linear table that restricts the insertion and deletion of elements in linear tables only at the same end of the linear table. One end that allows insertion and deletion is called the top of the stack, and the other end is the fixed end, called the bottom of the stack.

3) According to the definition of stack, the elements first put in the stack are at the bottom of the stack, the elements last put in the top of the stack, and the deletion elements are just the opposite. The elements last put in are deleted first, and the elements first put in are deleted finally.

Stack-in and Stack-out Concept Map

 

Idea analysis of implementation stack:

1. Analog stacks with arrays

2. Define a top (like a pointer) to represent the top of the stack, initialized to -1

3. Stack operation, when data is added to the stack, top+; stack[top] = data; stack [] array simulation stack, used to store data

4. Out-of-stack operation, int val = stack[top]; top--; return val

//Define an ArrayStack Representation Stack
class ArrayStack{

    private int maxSize;//Stack size
    private int[] stack;// Array simulation stack data is placed in that array
    private int top = -1;// Represents stack top initialization - 1
    //constructor
    public ArrayStack (int maxSize){
        this.maxSize = maxSize;
        stack = new int[maxSize];
    }
    //Stack full
    public boolean isFull(){
        return top == maxSize - 1 ;
    }
    //Stack space
    public boolean isEmpty(){
        return top == -1;
    }
    //Push
    public void push(int value){
        if(isFull()){
            System.out.println("Stack full");
            return;
        }
        top++;
        stack[top] = value;
    }
    //Stack out
    public int pop(){
        if(isEmpty()){
            throw new RuntimeException("Stack empty, no data.....");
        }
        int value = stack[top];
        top--;
        return value;
    }
    //Traversal stack
    public void list(){
        if(isEmpty()){
            System.out.println("Stack empty, no data.....");
            return;
        }
        //Data needs to be displayed from the top of the stack
        for (int i = top ; i >= 0 ; i--){
            System.out.printf("stack[%d]=%d\n", i, stack[i]);
        }
    }
}
public class ArrayStackDemo {

    public static void main(String[] args) {
        ArrayStack stack = new ArrayStack(4);//Create stack
        String key = "";
        boolean loop = true;//Control whether to Exit menu
        Scanner scanner = new Scanner(System.in);

        while(loop){
            System.out.println("show: Represents the display stack");
            System.out.println("exit: Represents an exit procedure");
            System.out.println("push: Presentation stack");
            System.out.println("pop: Presentation stack");
            System.out.println("Please enter your choice");
            key = scanner.next();
            switch (key){
                case "show":
                    stack.list();
                    break;
                case  "push":
                    System.out.println("Please enter a number");
                    int value = scanner.nextInt();
                    stack.push(value);
                    break;
                case "pop":
                    try{
                        int res = stack.pop();
                        System.out.printf("The number of outgoing stacks is %d\n",res);
                    }catch (Exception e){
                        System.out.println(e.getMessage());
                    }
                    break;
                case "exit":
                    scanner.close();
                    loop = false;
                    break;
            }
        }
    }

}

Operation results:

Posted on Sat, 12 Oct 2019 07:47:36 -0700 by DarkReaper