Data Structure (2): Theory and Implementation of the stack

I. INTRODUCTION

A data stack is a first-after, after first-out data structure.

image

As shown in FIG. If, after the digital 10,15,6,9 into stack from the stack to fetch the data will be sequentially 9,6,15,10. Structure like stack of boxes in our lives, the first item will be placed in the bottom of the box, and finally into the data at the top, but also need to pick up the time to take items from the top.

Second, code implementation

1. Create MyStack class as a custom stack

public class MyStack {

}

2, required to declare properties

Using the underlying data storage array, may be used alternatively java generic type Object.

private Object[] arr;//存储数据
private int top;//栈顶的位置

Top of the stack: the entire top of the stack (the last stack) of elements in the array is the last element deposit.

Bottom of the stack: the bottom element in the entire stack (the first stack), which is stored in the first array element.

3, the stack constructor

public MyStack(){
    arr = new Object[10];
    top = -1;
}

/**
 * 参数为数组的初始长度
 * @param maxSize
 */
public MyStack(int maxSize){
    arr = new Object[maxSize];
    top = -1;
}

4, press-fitting the data to the stack

Operation is stored in the underlying data to the array, since the topvariable note the location of the top of the stack, so the topvalue is increased by 1 after the latest top of the stack is set.

 public void push(Object value) {
    arr[++top] = value;
}

5, the top of the stack of pop-up data

Obtain the data stack, and this stack data will be removed. The stack is pressed into the recording data to the variable position of the top of the stack topplus 1. Likewise, data necessary to remove the stack topis decremented by one.

public Object pop(){
    return arr[top--];
}

6, see the top of the stack data

public Object peek(){
    return arr[top];
}

7, it is determined whether the air

public boolean isEmpty(){
    return top == -1;
}

8, to determine whether there is full

topIs equal to the last element of the array is the location is full

public boolean isFull(){
    return top == arr.length-1;
}

9, complete code

public class MyStack {
    private Object[] arr;//存储数据
    private int top;//栈顶的位置

    public MyStack() {
        arr = new Object[10];
        top = -1;
    }

    /**
     * 参数为数组的初始长度
     *
     * @param maxSize
     */
    public MyStack(int maxSize) {
        arr = new Object[maxSize];
        top = -1;
    }

    public void push(Object value) {
        arr[++top] = value;
    }

    /**
     * 弹出栈顶的数据
     * @return
     */
    public Object pop(){
        return arr[top--];
    }

    public Object peek(){
        return arr[top];
    }

    public boolean isEmpty(){
        return top == -1;
    }

    public boolean isFull(){
        return top == arr.length-1;
    }
}

Third, verify

public static void main(String[] args) {
    MyStack stack = new MyStack(4);
    stack.push("a");
    stack.push("b");
    stack.push("c");
    stack.push("d");

    System.out.println("是否为空:" + stack.isEmpty());
    System.out.println("是否存满:" + stack.isFull());

    System.out.println("栈顶:"+stack.peek());
    System.out.println("栈顶:"+stack.peek());

    //弹出栈中所有数据
    while (!stack.isEmpty()){
        System.out.println(stack.pop());
    }

    System.out.println("是否为空:" + stack.isEmpty());
    System.out.println("是否存满:" + stack.isFull());
}

Guess you like

Origin www.cnblogs.com/AIThink/p/11332604.html