What is a queue?

As mentioned earlier data structure Similarly, the data in the queue is linearly arranged.Although with Stack Some are similar, but adding and deleting data in a queue is done on both ends, and it's easier to think of it as being in a queue, just like the name of the queue.In a queue, processing always starts at the first place and goes backwards, whereas newcomers can only come at the end of the queue.

What is the queue?

As above is the concept diagram of the queue, there is now only data Blue in the queue.When data is added to a queue, it is added to the top.

Then, the data Green is added to the queue.Adding data to a queue is called queuing.

Then Data Red joined the team.

When removing (deleting) data from a queue, it starts with the lowest data, which is the first data to join the queue, Blue.Deleting data from a queue is called queuing.

If you do a queue operation again, Green will be taken out.

The most advanced data, such as queues, is first retrieved, the first-in-first-out structure, which we call First In First Out, or FIFO for short.

Similar to stacks, there are limitations on where data can be manipulated in queues.In a stack, data is added and deleted at the same end, while in a queue, data is added and deleted at both ends.Queues also do not have direct access to data in the middle; they must prioritize the target data through an out-of-queue operation before they can be accessed.

After introducing the basic knowledge of queues, let's take an example, such as queue buying, which is common in life.

How do you understand queues?

The concept of queue is very understandable. You can think of it as queuing to buy tickets first. Later people can only stop at the end. No queuing is allowed. First-in-first-out is a typical queue.

In the previous article, stacks only support two basic operations: stacking and stacking.Queues are very similar to stacks, and support a limited number of operations. The most basic operations are two: queuing, placing a data at the end of the queue, and queuing, taking an element from the head of the queue.

So queues, like stacks, are a linear table data structure with limited operations. The concept of queues is well understood and the basic operations are easy to master.As a very basic data structure, queues are also widely used, especially some queues with some additional characteristics, such as circular queues, blocked queues, and concurrent queues.

Queue implementation

Looking at this, I believe you have a preliminary understanding of the queue, which mainly consists of two operations, enlistment and exit.Understanding is not enough. Let's start with implementing a queue. Let's see how to implement a queue with code.

Like stacks, queues can be implemented using arrays or chained lists.A stack implemented with an array is called a sequential stack, and a stack implemented with a chain table is called a chain stack.Similarly, a queue implemented with an array is called a sequential queue, and a queue implemented with a chain table is called a chain queue.

First, let's look at what a queue implemented with an array looks like, as shown in the following figure:

Then I'll use the Java language to implement the next sequential queue, code as follows:

/**
 * Sequential queue based on array implementation
 *
 * @author wupx
 * @date 2020/02/13
 */
public class ArrayQueue {

    private String[] items;
    /**
     * Array size
     */
    private int n = 0;
    /**
     * Head Subscript
     */
    private int head = 0;
    /**
     * End Subscript
     */
    private int tail = 0;

    /**
     * Request an array of capacity size
     *
     * @param capacity
     */
    public ArrayQueue(int capacity) {
        items = new String[capacity];
        n = capacity;
    }

    /**
     * Entry
     *
     * @param item
     * @return
     */
    public boolean enqueue(String item) {
        // If tail == n, the queue is full
        if (tail == n) {
            return false;
        }
        items[tail] = item;
        ++tail;
        return true;
    }

    /**
     * Queue
     *
     * @return
     */
    public String dequeue() {
        // If head == tail indicates that the queue is empty
        if (head == tail) {
            return null;
        }
        String ret = items[head];
        ++head;
        return ret;
    }
}

Another is a chain queue, which is implemented as follows:

Then use the chain table to implement the queue, the code is as follows:

/**
 * Chained Queue Based on Chain List
 *
 * @author wupx
 * @date 2020/02/13
 */
public class LinkedListQueue {
    /**
     * Team Head
     */
    private Node head = null;
    /**
     * End of Team
     */
    private Node tail = null;

    /**
     * Entry
     *
     * @param value
     */
    public void enqueue(String value) {
        if (tail == null) {
            Node newNode = new Node(value, null);
            head = newNode;
            tail = newNode;
        } else {
            tail.next = new Node(value, null);
            tail = tail.next;
        }
    }

    /**
     * Queue
     *
     * @return
     */
    public String dequeue() {
        if (head == null) {
            return null;
        }

        String value = head.data;
        head = head.next;
        if (head == null) {
            tail = null;
        }
        return value;
    }

    public void printAll() {
        Node p = head;
        while (p != null) {
            System.out.print(p.data + " ");
            p = p.next;
        }
        System.out.println();
    }

    private static class Node {
        private String data;
        private Node next;

        public Node(String data, Node next) {
            this.data = data;
            this.next = next;
        }

        public String getData() {
            return data;
        }
    }
}

So far, we have implemented queues based on arrays and chained lists, which you can implement by yourself.

Queues have many extensions, such as circular queues, blocked queues, concurrent queues, and so on, which will be described in future articles.

summary

This article mainly talks about the following Stack Queues, a very similar data structure, are also a linear logical structure.

Queues follow the principle of first in, first out (FIFO). The two main operations are queue entry and queue exit.Queues can be implemented using arrays or chained lists.An array is called a sequential queue, and a chain table is called a chain queue.

Reference resources

My First Algorithmic Book

Tags: Java

Posted on Wed, 12 Feb 2020 12:55:31 -0500 by verycleanteeth