java data structure - 5 stack (Stack)

Five, stack (Stack)

A stack is a last-in-first-out data structure (LIFO: last in first out), which only allows access to the first data item in the stack: the last inserted data item. After this data item is removed, the second data item is visible, and so on.

Putting data into the stack is called push, and removing data is called pop. In addition, a peek method is usually provided to view the top element of the stack. This method can get the value of the top element of the stack. But it doesn't remove it.

java.util.Stack is an implementation of the stack provided by JDK. This implementation is based on arrays. Since the stack is very simple, we do not have to analyze the source code, and directly provide an implementation of the same itself according to the following methods. In addition, we A stack can also be implemented based on a linked list.

1. Implementation of an array-based stack

Regarding the implementation of the array-based stack, there is only one thing worth noting, it is LIFO, but when we use the array, we do not need to insert the element into the first position of the array every time, and then insert all the previous elements after the To move a position, just use a variable to remember the position of the last added element. When popping the stack, just return the number at this position.

Here is the code implementation:

public class SimpleArrayStack<V> {
    private Object[] array = null;
    private int size = 0;
    private static final int DEFAULR_INITIAL_SIZE = 10;
    private int capacity = 0;
    public SimpleArrayStack() {
        this(DEFAULR_INITIAL_SIZE );
    }
    public SimpleArrayStack(int initial_size) {
        super();
        this.capacity = initial_size;
        array = new Object[initial_size];
    }
    /**
     * 压栈
     */
    public void push(V v) {
        int index = size++;
        if (index > capacity) { // 扩容
            int new_capacity = capacity + capacity >> 1;
            if (new_capacity <= 0) {
                new_capacity = Integer.MAX_VALUE;
            }
            array = Arrays.copyOf(array, new_capacity);
        }
        array[index] = v;
    }
    /**
     * 弹栈
     */
    public V pop() {
        if (size < 1) {
            throw new IllegalStateException("栈内没有元素");
        }
        V result = (V)array[--size];
        array[size] = null;
        return result ;
    }
    /**
     * 获取栈顶值
     */
    public V peek() {
        if (size < 1) {
            throw new IllegalStateException("栈内没有元素");
        }
        return (V)array[size - 1];
    }
    /**
     * 获取栈大小
     */
    public int getSize() {
        return size ;
    }
}

2. Implementation of stack based on linked list

The Stack based on the linked list is particularly simple. We can use the SingleLinkList to implement it, which is actually a kind of delegation: a data structure can be written on the basis of another data structure .

Here is the code implementation:

public class SimpleLinkedListStack<V> {
    private SingleLinkList<V> list =new SingleLinkList<V>();
    public void push(V v){
        list.addFirst(v);
    }
    public V pop(){
       return list.removeFirst();
    }
    public V peek(){
        return list.getFirst();
    }
    public int getSize(){
        return list.getSize();
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325490097&siteId=291194637