Easy use of stack and implementation of stack by hand

Posted by 2levelsabove on Sun, 30 Jan 2022 17:26:23 +0100

Concept and characteristics of stack

  • Stack is a special linear table, which only allows the insertion and deletion of elements at the fixed end. One end for data insertion and deletion is called the top of the stack, and the other end is called the bottom of the stack. The data elements in the stack follow the principle of Last In First Out LIFO (Last In First Out).
  • Stack pressing: the stack insertion operation is called stack entering / stack pressing / stack entering, and the input data is at the top of the stack.
  • Stack out: stack deletion is called stack out. The output data is also at the top of the stack.

As shown in the figure:

Here, 1 2 3 4 is the element pressed into the stack. The newly pressed element is placed at the top of the stack. For example, press the new element: 5. So this is it:

Out of the stack is also going out from the top of the stack.

Stack usage

The functions of stack include: pressing stack, getting out of stack, comparing stack top elements, judging empty, stack comparison, etc.

Stack pressing

push the stack:

    public static void main(String[] args) {
        java.util.Stack<Integer> stack = new java.util.Stack<>();
        stack.push(1);
        stack.push(2);
        stack.push(3);
        stack.push(4);
        System.out.println(stack);
    }

The operation results are as follows:

Out of stack

Pop the stack through pop method:

public static void main(String[] args) {
    java.util.Stack<Integer> stack = new java.util.Stack<>();
    stack.push(1);
    stack.push(2);
    stack.push(3);
    stack.push(4);
    System.out.println(stack);
    stack.pop();
    stack.pop();
    System.out.println(stack);
}

The operation results are as follows:

Get stack top element

By peek method:

public static void main(String[] args) {
    java.util.Stack<Integer> stack = new java.util.Stack<>();
    stack.push(1);
    stack.push(2);
    stack.push(3);
    stack.push(4);
    System.out.println(stack.peek());
    System.out.println(stack);
}

The operation results are as follows:

When using peek, you only get the top element of the stack.

Determine whether the stack is empty

Judge by isEmpty:

public static void main(String[] args) {
    java.util.Stack<Integer> stack = new java.util.Stack<>();
    stack.push(1);
    stack.push(2);
    stack.push(3);
    stack.push(4);
    System.out.println(stack.isEmpty());
}

Stack comparison

Compare equality by equals:

public static void main(String[] args) {
    java.util.Stack<Integer> stack = new java.util.Stack<>();
    java.util.Stack<Integer> stack1 = new java.util.Stack<>();
    stack.push(1);
    stack.push(2);
    stack1.push(1);
    stack1.push(2);
    System.out.println(stack.equals(stack1));
}

The operation results are as follows:

Hand to hand implementation stack

Because the stack is characterized by first in first out, last in first out. Moreover, the time complexity of entering and leaving the stack is O(1), so we choose the sequence table to realize the stack.

Write the sequence table

Write the initial part of the code first:

public int[] elem;
public int usedSize;
public MyStack() {
    this.elem = new int[5];
}

The contents of the sequence table are implemented here, that is, the stack implemented by ourselves.

Judge whether it is full

By directly comparing the size of the used space and the size of the array, you can know whether it is full or not:

public boolean isFull() {
    return this.usedSize == this.elem.length;
}

Stack pressing

Stack pressing is to put elements directly. When putting elements, first judge whether they are full or not. If they are full, expand the capacity. If they are not full, just put them directly in the usedSize position:

public  void push(int val) {
    if(isFull()) {
        this.elem = Arrays.copyOf(this.elem,2*this.elem.length);
    }
    this.elem[this.usedSize] = val;
    this.usedSize++;
}

Judge whether it is empty

Complete the judgment by judging whether usedSize is 0:

public boolean isEmpty() {
    return this.usedSize == 0;
}

Out of stack

When leaving the stack, first judge whether it is empty. If it is empty, throw the exception that the stack is empty. If not, first save the value of usedSize -1. Then usedSize --, and finally return the value of usedSize -1.

public int pop() {
    if ((isEmpty())) {
        throw new RuntimeException("Stack is empty!");
    }
    int oldVal = this.elem[this.usedSize-1];
    this.usedSize--;
    return  oldVal;
}

Compare stack top elements

The first step is to determine whether the stack is empty. If not, just return the value of usedSize -1.

public int peek() {
    if ((isEmpty())) {
        throw new RuntimeException("Stack is empty!");
    }
    return this.elem[this.usedSize-1];
}

Stack size

You can know the size of the stack by returning the size of usedSize.

public int size() {
    return this.usedSize;
}

test

Test the above functions:

public static void main(String[] args) {
    MyStack stack = new MyStack();
    stack.push(1);
    stack.push(2);
    stack.push(3);
    stack.push(4);
    stack.push(5);
    stack.push(6);
    stack.push(7);
    System.out.println(stack.size());
    stack.pop();
    stack.pop();
    System.out.println(stack.peek());
    System.out.println(stack.isEmpty());
}

The operation results are as follows:

Topics: Java data structure linked list stack