Linear table structure-queue

Another kind of data structure that is very similar to the stack is the queue. Like the stack, the queue is a special linear table structure, except that the queue is inserted at one end and deleted at the other end, just like we usually queue. From The queue enters the queue and goes out at the head of the queue, so the queue's feature is first-in-first-out (FIFO), the end that allows insertion is called the tail of the queue, and the end that allows deletion is called the head of the queue.

A picture can vividly reflect the difference between the two:

queue

Like the stack, the queue can also be implemented through an array and a linked list. The array is called a sequential queue, and the linked list is called a chained queue. The stack only needs a pointer to the top of the stack, because it is only allowed to insert and delete at the top of the stack. But the queue needs two pointers, one to the head of the team and one to the tail of the team. Let's first look at the sequential queue code implemented through PHP arrays:

<?php
/**
 * 通过 PHP 数组实现的队列
 */
class SimpleQueue
{
    private $_queue = [];
    private $_size = 0;

    public function __construct($size = 10)
    {
        $this->_size = $size;
    }

    // 入队
    public function enqueue($value)
    {
        if (count($this->_queue) > $this->_size) {
            return false;
        }
        array_push($this->_queue, $value);
    }

    // 出队
    public function dequeue()
    {
        if (count($this->_queue) == 0) {
            return false;
        }
        return array_shift($this->_queue);
    }

    public function size()
    {
        return count($this->_queue);
    }
}

$queue = new SimpleQueue(5);
$queue->enqueue(1);
$queue->enqueue(3);
$queue->enqueue(5);
var_dump($queue->dequeue());  # 1
var_dump($queue->size());  # 2

A problem with sequential queues implemented by arrays is that with the insertion and deletion of queue elements, the tail of the queue and the head of the queue continue to move backwards, causing the tail of the queue to point to the end and failing to insert data. At this time, the queue head may still be If there is space left, as shown below:

Of course, we can move all the queue data forward by means of data movement, but this will add additional time complexity. If you frequently operate a queue with a large amount of data, obviously there is a serious loss of performance. The solution to this problem is Circular queue, that is, connecting the head and tail of the queue:

In this way, the previous problem will not occur. At this time, the condition for judging whether the queue is empty or not tail==head, but the condition for judging whether the queue is full becomes (tail+1) % maxsize == head, maxsize is the length of the array, and a space is wasted to avoid confusion about judging empty. The condition of the queue. Of course, if the queue is implemented through a linked list, there is obviously no such problem, because the linked list has no space limit.

The application of queues is also very extensive. For example, our common message queue is a typical application scenario of queues.

Guess you like

Origin www.cnblogs.com/stringarray/p/12702568.html