Java Road to God: The third post-data structure and algorithm queue

Data structure and algorithm-queue

I lost two hairs today. I touched them. Don’t touch them. They are precious! !

What is a queue?

1) Queue is an ordered list, which can be realized by array or linked list

2) Follow the principle of  first in, first out  . That is: the data stored in the queue first must be taken out first. What's deposited later will be taken out later

3) Schematic diagram: (using array simulation queue diagram)

queue

Rear represents the end of the queue, front represents the head of the queue, and the front value is equal to -1, which represents the first bit of the queue data (using an array to simulate a queue, the first subscript of the array is 0, and -1 is the first data The value of rear is -1, here I think it is to be consistent with front, so rear == frontthat the array is empty!


The idea of ​​using arrays to simulate queues

The queue itself is an ordered list. If the structure of the array is used to store the data of the queue, the declaration of the queue array is as shown in the figure below, where max Size is the maximum capacity of the queue, because the output and input of the queue are processed from the front and the back respectively. Therefore, two variables front and rear are required to record the subscripts of the front and back ends of the queue respectively. The front will change with the data output, and the rear will change with the data input, or this picture~

queue

** When we store data in the queue, it is called "addQueue". The processing of addQueue requires two steps:

  1. Move the tail pointer back:, rear+1when the front == rear queue is empty (PS: array head and array tail together, can it not be empty)
  2. If the tail pointer rear is less than the maximum subscript maxSize-1 of the queue, the data will be stored in the array element pointed to by the rear, otherwise the data cannot be stored. reaI == maxSize-1 This means that the queue is full
  3. Not much to say, the code: **
package com.yishuai.queue;

public class ArrayQueueDemo {
    public static void main(String[] args) {
        //数据测试
        ArrayQueue arrayQueue = new ArrayQueue(3);
    }
}

class ArrayQueue {
    private int maxSize;//最大容量
    private int front;//队列头
    private int rear;//队列尾
    private int[] arr;//存放数据的数组,模拟队列

    //队列初始化
    public ArrayQueue(int maxSize) {
        this.maxSize = maxSize;
        arr = new int[maxSize];
        front = -1;//指向队列的首部,分析出front指向队列头的前一个位置
        rear = -1;//指向队列的尾部,指向队列尾部的最后一个数据
    }

    //判断队列是否满
    public boolean isFull() {
        return rear == maxSize - 1;
    }

    //判断队列是否空
    public boolean isEmpty() {
        return rear == front;
    }

    //将数据添加到队列
    public void addQueue(int data) {
        //如果队列已经满了,就不能再添加数据进入
        if (isFull()) {
            System.out.println("队列已经满了");
            return;
        }
        rear++; //数据增加,队列尾的下标增加,队列头不变
        arr[rear] = data;
    }

    //将数据从队列中取出
    public int getQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空,没有数据可取");
        }
        front++;
        return arr[front];
    }

    //展示出队列中的所有数据
    public void showQueue() {
        if (isEmpty()) {
            System.out.println("队列为空,没有数据哦~");
        }
        for (int data : arr) {
            System.out.print(data + "\t");
        }
    }
}

New problem

Because the queue at this time is a chain, which means that when the current data is taken out, even if the queue is not full at this time, the tail of the queue is still as large as maxSize-1. When inserting, it still shows: the queue is full~, if we want to solve this problem, we have to use:  circular queue

Circular queue

There are two ways (it pitted me for a few hours):

The first:

Statement: font is the first data in the head, rear is the next position of the last data in the tail

  1. At font == rearthat time, the queue is empty (is this okay?)
  2. Initial:, the font == rear == 0queue is empty at this time, when the data enters, the rear starts to go backward, which is +1
  3. Suppose maxSize=5four data are put in, that is, after the rear has gone four steps, rear=4the array has five positions, but there are only four data in it, and the last empty data has not been put in. At this time, if the rear is again Go one step forward,, rear=5but the subscript of the array starts from 0, that is to say, (0,1,2,3,4) here are five cells, to the top
  4. So when rear reaches 5, you can only go back. That is to say, rear=4afterwards, it is not rear=5, but rear=0, font is also equal to 0 at this time font == rear, is it exciting?
  5. When I said earlier font == rear, the queue was empty, but now font == rearwhen I say it, the queue is full again. Is something wrong? Do most people think the same way as me (maybe I am narcissistic), you are right, both are right, so, simply relying on font and rear can no longer meet the conditions of judgment, my method It was added, then a count counted from 0, add a data count++, a data fetch, count-- . The count will change between [0, 5]. When font == rearand count=0when the queue is empty, when font == rearand count=5when, the queue is full. As for the number of valid data, just look at the value of count. This is the first method ( I feel better than the second one)! !

The second type (I discussed with my roommate for more than an hour, and made complaints)

PS: Here comes the point! ! !

One cell is empty in this method! ! ! If the maximum space is 5, only 4 pieces of data can be put in, and there is still a space for planting rice (continuous swearing!), it is too wasteful to not use up the space, I watched it 3 times and thought It shouldn’t be so stupid. I don’t run out of space. I have a mine at home. I thought I didn’t understand it. I did it over and over again. After discussing it with my roommate for a long time, I didn’t come to a conclusion. I finally turned the book and solved it ( Don’t sell books, don’t talk privately to me and ask ha), repeat, one space is empty! ! Always! !

This method means:
declare: font is the first data in the head, rear is the next position of the last data in the tail

  1. At font == rearthat time, the queue is empty (this is okay? Is it okay?)
  2. If maxSize = 5, when is it full? When the rear reaches 4, it is full, that is rear == 4, there are four data in it, and there is one space. Repeat: rice is planted (empty), so (rear+1)% maxSize == fontwhen it is full (% is the remainder, also called Modulus, / is quotient), what does rear+1 mean? Fill in the space for planting rice, and then take the modulus of maxSize. Why take the modulus? Because it may happen to be in the same circle as font, or it may have been a circle away from font, it is necessary to perform modulo operation.
  3. So how much valid data is there? You can directly look at the length of the queue, that is |rear-font| (take the absolute value, ask me why I go to the wall first, and then comment and I will reply to you), so it can be written like this (rear+maxSize-font)%maxSize, the
    update is over, my hair is lost again Two, ah ah ah! ! !

If you don't like a thumbs up before leaving, are you worthy of my hair, worthy of it?

Guess you like

Origin blog.csdn.net/javachengzi/article/details/108613005