Introduction to the stack and implementation of the code

Stack

1. Concept

  • Stack : A special linear table that only allows inserting and deleting elements at a fixed end. One end of the data insertion and deletion operations is called the top of the stack, and the other end is called the bottom of the stack. The data elements in the stack comply with the LIFO (Last In First Out) principle.
  • Pushing the stack : The inserting operation of the stack is called pushing/pushing/pushing, and the data is put on the top of the stack.
  • Pop : The delete operation of the stack is called pop. The output data is at the top of the stack.
    Insert picture description here

2. The specific implementation of the stack

2.1 Stacking
  • Ideas:
  1. Before entering the stack, we must first determine whether the stack is full, and only when the stack is not full can new elements continue to be pushed onto the stack;
  2. Because it is in the form of a sequence table, just add a new element at the end of the array;
  3. Then add 1 to the length of the sequence table;
//入栈
    public void push(T item) {
    
    
        //满
        if(isFull()) {
    
    
            return;
        }
        this.elem[this.usedSize] = item;
        this.usedSize++;
    }
2.2 Pop
  • Ideas:
  1. Before popping the stack, you must determine whether there are any elements in the stack. Only with the element, you can pop the element;
  2. Because the stack is first-in-last-out, you only need to pop the element at the last usedSize-1 position each time;
  3. Then the size is reduced by 1;
//出栈
    public T pop() {
    
    
        //空
        if(isEmpty()) {
    
    
            //return -1;
            throw new RuntimeException("栈为空!");
        }
        T ret = this.elem[this.usedSize-1];
        this.usedSize--;
        this.elem[usedSize] = null;
        return ret;
    }
2.3 Get the top element of the stack
  • Ideas:
  1. Also first determine whether it is empty;
  2. It is not empty, as long as it returns the element at the position of usedSize-1 (because the array index starts from 0);
//获取栈顶元素,但是不删除
    public T peek(){
    
    
        //空
        if(isEmpty()) {
    
    
            //return -1;
            throw new RuntimeException("栈为空!");
        }
        return this.elem[this.usedSize-1];
    }
2.4 Determine whether it is empty
  • Idea: Just look at whether usedSize is 0;
//判断是否为空
    public boolean isEmpty() {
    
    
        if(this.usedSize == 0) {
    
    
            return true;
        }
        return false;
        //return this.usedSize == 0;
    }
2.5 Determine whether it is full
  • Idea: Just judge whether usedSize is equal to the size of the array you defined;
//判断是否为满
    public boolean isFull() {
    
    
        return this.usedSize == this.elem.length;
    }
}

3. Overall code

import java.util.Objects;
import java.util.Stack;

//顺序(顺序表实现)栈
class MyStack<T> {
    
    
    public T[] elem;//数组
    public int usedSize;//top

    public MyStack() {
    
    
        this.elem = (T[])new Objects[5];
    }
    //入栈
    public void push(T item) {
    
    
        //满
        if(isFull()) {
    
    
            return;
        }
        this.elem[this.usedSize] = item;
        this.usedSize++;
    }
    //出栈
    public T pop() {
    
    
        //空
        if(isEmpty()) {
    
    
            //return -1;
            throw new RuntimeException("栈为空!");
        }
        T ret = this.elem[this.usedSize-1];
        this.usedSize--;
        this.elem[usedSize] = null;
        return ret;
    }
    //获取栈顶元素,但是不删除
    public T peek(){
    
    
        //空
        if(isEmpty()) {
    
    
            //return -1;
            throw new RuntimeException("栈为空!");
        }
        return this.elem[this.usedSize-1];
    }
    //判断是否为空
    public boolean isEmpty() {
    
    
        if(this.usedSize == 0) {
    
    
            return true;
        }
        return false;
        //return this.usedSize == 0;
    }
    //判断是否为满
    public boolean isFull() {
    
    
        return this.usedSize == this.elem.length;
    }
}


public class TestDemo2 {
    
    

    public static void main(String[] args) {
    
    
        MyStack<Integer> myStack = new MyStack<>();
        myStack.push(1);//入栈
        myStack.push(12);
        myStack.push(3);

        System.out.println(myStack.pop());//3,出栈
        System.out.println(myStack.peek());//12,获取栈顶元素

    }
}

Guess you like

Origin blog.csdn.net/qq_45665172/article/details/110206747
Recommended