Structure and algorithm (02): queue and stack structure

Time:2020-11-25

Source code:GitHub. Click here || Gitee. Click here

1、 Queue structure

1. Basic concepts

Queue is a kind of special linear table. Its particularity is that it only allows deletion at the front of the table, but inserts at the back end of the table. Like stack, queue is a kind of linear table with limited operation. The end of inserting is called the tail of the queue, and the end of deleting is called the head of the queue.

2. Feature description

Queue is a sequential list, which can be realized by array or linked list, following the principle of first in first out. That is, the first data in the queue will be taken out first; the data that enters the queue later should be taken out later; that is, FIFO principle.

Diagram of queuing

Structure and algorithm (02): queue and stack structure

Diagram of out of queue

Structure and algorithm (02): queue and stack structure

Through the above two diagrams, it is not difficult to find some characteristics of queue structure

  • The data that enters first goes out first;
  • The data enters from the end of the team and goes out from the head of the team;
  • Based on the array description, the subscript of the queue changes frequently;
  • The out queue algorithm can take modulus based on container size;

The core of queue structure is the algorithm to judge whether the container is empty and full, that is, the container is empty and can not be retrieved again, and the container is full and can not be stored again. The algorithm structure is widely used in the storage field.

3. Message queuing

Message queuing is based on the “first in, first out” strategy of data structure. The message is put into the queue in the way of queuing, and then the out of queue is consumed

Structure and algorithm (02): queue and stack structure

Sometimes a certain type of message consumption needs to be controlled in sequence, that is, the public ID in the message can be modulus processed, that is, a certain type of message can be placed in a queue.

4. API use cases

The LinkedList class implements the queue interface, so you can simulate the queue effect based on LinkedList.

import java.util.LinkedList;
import java.util.Queue;

public class M01_Queue {
    public static void main(String[] args) {
        //Queue up
        Queue<String> queue = new LinkedList<>();
        queue.add("head") ;
        queue.add("middle") ;
        queue.add("tail") ;
        //When the team lists the data, the size is constantly changing
        int queueSize = queue.size() ;
        int loop = 0 ;
        //According to the size of the queue, continue to queue
        while (loop < queueSize) {
            System.out.println(queue.poll());
            System.out.println(queue);
            loop ++ ;
        }
    }
}

2、 Stack structure

1. Basic concepts

Stack, also known as stack, is a kind of linear table with limited operation. A linear table that restricts insert and delete operations only at the end of the table. This end is called the top of the stack, and the other end is called the bottom of the stack. To insert a new element into a stack is also called pushing, pushing or pushing. It is to put the new element on top of the stack to make it a new top element; to delete an element from a stack is also called pop, which is to delete the element at the top of the stack and make its adjacent element become a new top element.

2. Feature description

A stack is an ordered list of first in and then out. Adding and deleting can only be performed at the top of the stack, and the other end is a fixed end, which is called the bottom of the stack.

Diagram of stack

Structure and algorithm (02): queue and stack structure

Diagram of stack out

Structure and algorithm (02): queue and stack structure

Through the above two diagrams, some characteristics of stack structure are as follows:

  • The operation of entering and leaving the stack should be done through the top of the stack;
  • The pointer at the bottom of the stack is not moved in and out of the stack;
  • Move the pointer at the top of the stack when entering and leaving the stack;

According to the definition of stack, the first element placed in the stack is at the bottom of the stack, and the last element is at the top of the stack. The opposite is true for deleting elements from the stack container. The last element is deleted first, and the first element is deleted last.

3. Recursive application

The common application of stack in Java programming: (1) subroutine call: before jumping to the subroutine, the address of the next instruction will be stored in the stack until the subroutine is executed, and then the address will be taken out and returned to the original program; (2) Processing recursive calls: similar to subroutine calls, in addition to storing the address of the next instruction, parameters, area variables and other data should also be stored in the stack.

4. API use cases

Stack stack API is a subclass of vector. It implements a standard LIFO stack. The stack only defines the default constructor to create an empty stack. The stack includes all the methods defined by vector and also defines some of its own methods.

import java.util.Stack;

public class M02_Stack {
    public static void main(String[] args) {
        //On the stack
        Stack<String> stack = new Stack<>() ;
        stack.push("First") ;
        stack.push("Second") ;
        stack.push("Third") ;
        int stackSize = stack.size() ;
        int loop = 0 ;
        //According to the size of the stack, the stack will be continuously put out
        while (loop < stackSize) {
            System.out.println(stack.pop());
            System.out.println(stack);
            loop ++ ;
        }
    }
}

3、 Source code address

GitHub · address
https://github.com/cicadasmile/model-arithmetic-parent
Gitee · address
https://gitee.com/cicadasmile/model-arithmetic-parent

Structure and algorithm (02): queue and stack structure

Recommended reading: data structure and algorithm

Serial number Article title
01 Algorithm and structure (01): sparse array and 2D array conversion
02 Algorithm application: RSA algorithm, encryption and decryption, signature verification process details
03 Algorithm application: recursive algorithm, processing business data under the tree structure