Java USES arrays to implement stack data structure instances

  • 2020-04-01 03:35:29
  • OfStack

The stack is one of the most important data structures in the Java language, and its implementation should include at least the following methods:

1. Pop () out operation to pop the top element of the stack.
2. Push (E E) operation
3. Peek () looks at the top element of the stack
4. Is the isEmpty() stack empty

In addition, to implement a stack, you should also consider several issues:

1. The initial size of the stack and how to add stack space after the stack is full
2. Synchronization is required when updating the stack

Simple example, using the array stack, the code is as follows:


public class Stack<E> {      //Java does not support generic arrays. If you need to use them, use the container provided by Java & NBSP; < br / >     private Object[] stack;      //The default initial size of the stack < br / >     private static final int INIT_SIZE = 2;      //Top index & cake; < br / >     private int index;      public Stack() { 
        stack = new Object[INIT_SIZE]; 
        index = -1; 
    }      /** 
     * A constructor  
     *  
     * @param initSize 
     *            The initial size of the stack  
     */
    public Stack(int initSize) { 
        if (initSize < 0) { 
            throw new IllegalArgumentException(); 
        } 
        stack = new Object[initSize]; 
        index = -1; 
    }      /** 
     * The stack,  
     *  
     * @return The stack object  
     */
    public synchronized E pop() { 
        if (!isEmpty()) { 
            E temp = peek(); 
            stack[index--] = null; 
            return temp; 
        } 
        return null; 
    }      /** 
     * Into the stack,  
     *  
     * @param obj 
     *            Objects waiting to be pushed  
     */
    public synchronized void push(E obj) { 
        if (isFull()) { 
            Object[] temp = stack; 
            //If the stack is full, double the space of the current stack is created. < br / >             stack = new Object[2 * stack.length]; 
            System.arraycopy(temp, 0, stack, 0, temp.length); 
        } 
        stack[++index] = obj; 
    }      /** 
     * View the top of the stack object  
     *  
     * @return The stack object  
     */
    public E peek() { 
        if (!isEmpty()) { 
            return (E) stack[index]; 
        } 
        return null; 
    }      /** 
     * See if the stack is empty  
     *  
     * @return Returns if the stack is empty true Otherwise return false 
     */
    public boolean isEmpty() { 
        return index == -1; 
    }      /** 
     * Check if the stack is full  
     *  
     * @return Returns if the stack is full true, Otherwise returns false 
     */
    public boolean isFull() { 
        return index >= stack.length - 1; 
    } 
}

Finally, the Java implementation of the Stack (java.util.stack) data structure, it is through the inheritance of the Vector class to achieve, generally we just use it directly.


Related articles: