What data structure is a queue?
Queue is a linear data structure. The queue only allows deletion operations at the front end of the table, and insertion operations at the back end of the table. Like the stack, the queue is a linear table with restricted operations; the end where the insertion operation is performed is called the end of the queue, and the deletion operation is performed The end is called the head of the team.
#The operating environment of this article: windows10 system, thinkpad t480 computer.
Queue is a linear data structure.
The queue is a special linear table. The special thing is that it only allows deletion operations at the front end of the table (front), and insertion operations at the back end (rear) of the table. Just like the stack, A queue is a linear list with restricted operations. The end that performs the insertion operation is called the tail of the queue, and the end that performs the deletion operation is called the head of the queue. When there are no elements in the queue, it is called an empty queue.
The data elements of the queue are also called queue elements. Inserting a queue element into the queue is called enqueuing, and deleting a queue element from the queue is called dequeuing. Because the queue only allows insertion at one end and deletion at the other end, only the element that enters the queue earliest can be deleted from the queue first, so the queue is also called a first-in-first-out (FIFO—first in first out) linear list.
Sequential queue
To establish a sequential queue structure, you must statically allocate or dynamically apply for a continuous storage space, and set two pointers for management. One is the head pointer front, which points to the head element; the other is the tail pointer rear, which points to the storage location of the next element in the queue, as shown in the figure
Every time an element is inserted at the end of the queue, rear increases by 1; every time an element is deleted at the head of the queue, front increases by 1. As the insertion and deletion operations proceed, the number of queue elements continues to change, and the storage space occupied by the queue also moves in the continuous space allocated for the queue structure. When front=rear, there are no elements in the queue, which is called an empty queue. When rear increases beyond the continuous space that points to the allocation, the queue can no longer insert new elements, but there is often a large amount of available space that is not occupied at this time. These spaces are storage units that have been occupied by queue elements that have been dequeued.
Overflow phenomenon in the sequential queue:
(1) "Underflow" phenomenon: When the queue is empty, the overflow phenomenon caused by the queue operation. "Underflow" is a normal phenomenon and is often used as a condition for program control transfer.
(2) "True overflow" phenomenon: When the queue is full, a push operation on the stack will cause space overflow. "True overflow" is an error condition and should be avoided.
(3) "False overflow" phenomenon: Since the head and tail pointers only increase but do not decrease during the enqueue and dequeue operations, the space of the deleted element can never be reused. When the actual number of elements in the queue is far smaller than the size of the vector space, the queue operation may not be possible because the tail pointer has exceeded the upper bound of the vector space. This phenomenon is called "false overflow".
Circular queue
When actually using the queue, in order to make the queue space reusable, the method of using the queue is often slightly improved: regardless of insertion or deletion, once When the rear pointer is incremented by 1 or the front pointer is incremented by 1 and exceeds the allocated queue space, let it point to the starting position of this continuous space. If you really change from MaxSize-1 by 1 to 0, you can use the remainder operations rear%MaxSize and front%MaxSize to achieve it. This actually imagines the queue space as a circular space, and the storage units in the circular space are used cyclically. The queue managed in this way is also called a circular queue. In addition to some simple applications, the really practical queue is the circular queue.
In a circular queue, when the queue is empty, there is front=rear, and when all the queue space is full, there is also front=rear. In order to distinguish between the two situations, it is stipulated that the circular queue can only have MaxSize-1 queue elements at most. When there is only one empty storage unit left in the circular queue, the queue is full. Therefore, the condition for the queue to be empty is front=rear, and the condition for the queue to be full is front=(rear 1)%MaxSize. The situation of empty and full queues is as shown in the figure:
Recommendation: " Programming Video"
The above is the detailed content of What data structure is a queue?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

deque in Python is a low-level, highly optimized deque useful for implementing elegant and efficient Pythonic queues and stacks, which are the most common list-based data types in computing. In this article, Mr. Yun Duo will learn the following together with you: Start using deque to effectively pop up and append elements. Access any element in deque. Use deque to build an efficient queue. Start using deque to append elements to the right end of a Python list and pop up elements. The operations are generally very Efficient. If time complexity is expressed in Big O, then we can say that they are O(1). And when Python needs to reallocate memory to increase the underlying list to accept new elements, these

As web applications continue to develop, we need to handle a large number of tasks to maintain the stability and availability of the application. Using a queuing system is one solution. ThinkPHP6 provides a built-in queue system to manage tasks. However, handling a large number of tasks requires better queue management, which can be achieved using Supervisor. This article will introduce how to use Supervisor to manage ThinkPHP6 queues. Before that, we need to understand some basic concepts: queue system queue system is

Application summary of queue technology in message delay and message retry in PHP and MySQL: With the continuous development of web applications, the demand for high concurrency processing and system reliability is getting higher and higher. As a solution, queue technology is widely used in PHP and MySQL to implement message delay and message retry functions. This article will introduce the application of queue technology in PHP and MySQL, including the basic principles of queues, methods of using queues to implement message delay, and methods of using queues to implement message retries, and give

Performance Analysis and Optimization Strategy of JavaQueue Queue Summary: Queue (Queue) is one of the commonly used data structures in Java and is widely used in various scenarios. This article will discuss the performance issues of JavaQueue queues from two aspects: performance analysis and optimization strategies, and give specific code examples. Introduction Queue is a first-in-first-out (FIFO) data structure that can be used to implement producer-consumer mode, thread pool task queue and other scenarios. Java provides a variety of queue implementations, such as Arr

Queue in Java is a linear data structure with multiple functions. A queue has two endpoints and it follows the first-in-first-out (FIFO) principle for inserting and deleting its elements. In this tutorial, we will learn about two important functions of queues in Java, which are add() and Offer(). What is a queue? Queue in Java is an interface that extends the util and collection packages. Elements are inserted in the backend and removed from the frontend. Queues in Java can be implemented using classes such as linked lists, DeQueue, and priority queues. A priority queue is an extended form of a normal queue, where each element has a priority. The add() method of the queue is used to insert elements into the queue. It will define the element (as

Implementation of queue task monitoring and task scheduling in PHP and MySQL Introduction In modern web application development, task queue is a very important technology. Through queues, we can queue some tasks that need to be executed in the background, and control the execution time and order of tasks through task scheduling. This article will introduce how to implement task monitoring and scheduling in PHP and MySQL, and provide specific code examples. 1. Working principle of queue Queue is a first-in-first-out (FIFO) data structure that can be used to

What is the principle and implementation of the PHP mail queue system? With the development of the Internet, email has become one of the indispensable communication methods in people's daily life and work. However, as the business grows and the number of users increases, sending emails directly may lead to server performance degradation, email delivery failure and other problems. To solve this problem, you can use a mail queue system to send and manage emails through a serial queue. The implementation principle of the mail queue system is as follows: when the mail is put into the queue, when it is necessary to send the mail, it is no longer directly

Implementation Methods of Queue Producer and Consumer Patterns in PHP and MySQL With the rapid development of Internet business, the need to handle a large number of tasks in the system has become more and more urgent. Queues are a common solution to handle tasks efficiently. The implementation of the queue's producer-consumer pattern (Producer-ConsumerPattern) in PHP and MySQL is a common solution. This article will introduce the specific implementation method and provide code examples. producer-consumer pattern