Home  >  Article  >  Backend Development  >  How to loop queue in php array

How to loop queue in php array

PHPz
PHPzOriginal
2023-04-17 16:37:22623browse

PHP is a classic programming language and an open source interpreted scripting language. PHP can be embedded in HTML and is often used in the field of web development. It is one of the important tools for web application development. PHP has many powerful features, one of the important features is arrays. In PHP, an array is a container that can store multiple values, which can be of the same type or different types of data. In PHP, we can use a circular queue to traverse the elements in an array. This article will introduce how to use a circular queue to implement array traversal.

1. What is a circular queue?

Queue is a common data structure, which is a special linear table. In the queue, the insertion and deletion operations of data elements can only be performed at both ends of the queue. We call the head of the queue front and the tail of the queue rear. In the absence of queue space restrictions, the length of the queue can be increased arbitrarily. We call this a normal queue. A significant disadvantage of ordinary queues is that as the queue length continues to grow, the space in front of the queue array is likely to be wasted. This waste should be avoided even when the amount of data is small. Circular queues are a solution to this problem.

The circular queue is actually a circular sequence. The starting point of the array is adjacent to the end point of the array, and the pointer circulates along the array. The circular queue connects the front end (front) and the back end (rear) to form a ring. When the queue is full, the new elements coming in will overwrite the head element of the queue, thus realizing the recycling problem. This data structure solves the space waste problem of ordinary queues and makes full use of the space of array elements.

2. Implementation of circular queue

In PHP, we can use arrays to implement circular queues. The following is an example of the implementation of circular queue:

class CircleQueue {
    private $front; //队头指针
    private $rear; //队尾指针
    private $queueSize; //队列大小
    private $maxSize; //队列容量
    private $queue; //队列数组 

    public function __construct($maxSize){
        $this->maxSize = $maxSize;
        $this->front = 0;
        $this->rear = 0;
        $this->queueSize = 0;
        $this->queue = array();
    }
    
    public function enQueue($item){ //入队操作
        if($this->isFull()){
            return false;
        }else{
            $this->queue[$this->rear] = $item; //加入队列
            $this->rear = ($this->rear+1) % $this->maxSize; //队尾指针加1,如果超过了容量,就回到最开始(也就是第一个元素的位置)
            $this->queueSize++;
            return true;
        }
    }
    
    public function deQueue(){ //出队操作
        if($this->isEmpty()){
            return false;
        }else{
            $item = $this->queue[$this->front]; //取出队头元素
            $this->front = ($this->front+1) % $this->maxSize; //队头指针加1,如果超过了容量,就回到最开始(也就是第一个元素的位置)
            $this->queueSize--;
            return $item;
        }
    }
    
    public function isEmpty(){ //判断队列是否为空
        return $this->queueSize == 0;
    }
    
    public function isFull(){ //判断队列是否已满
        return $this->queueSize == $this->maxSize;
    }
    
    public function size(){ //获取队列大小
        return $this->queueSize;
    }
    
    public function getQueue(){ //获取队列数组
        return $this->queue;
    }
}

3. Use loops Queue traversing array

In PHP, we can use a circular queue to traverse the elements in an array. An array is composed of several elements, and a circular queue can put the array elements into the queue, and then traverse the queue to access the array elements. The following is a sample code that uses a circular queue to traverse an array:

$arr = array(1,2,3,4,5);
$queue = new CircleQueue(count($arr) + 1); //初始化队列,数组元素数量+1

//将数组元素入队列
foreach($arr as $value){
    $queue->enQueue($value);
}

//使用循环队列遍历数组元素
while(!$queue->isEmpty()){
    $item = $queue->deQueue();
    echo $item . ' ';
}

We first create an array, then create a circular queue, and put all the elements in the array into the queue. Finally, we use a circular queue to traverse the array elements and print out the value of each element, thus completing the array traversal.

4. Advantages and Disadvantages of Circular Queue

The circular queue has the following advantages:

1. Saves storage space, makes full use of the array space, and avoids waste of space;
2. Solve the storage problem of growing data volume;
3. In the implementation of circular queue, the time complexity of entering and dequeuing is O(1), and the time efficiency is high.

But circular queues also have some disadvantages:

1. The queue capacity is limited and the queue length is fixed. Once the amount of stored data exceeds the queue capacity, the data will be lost;
2. Every space must be used, otherwise the queue capacity will become smaller, which also limits the flexibility of the queue;
3. The data of the circular queue is relatively limited. For some complex data structures, the circular queue is not suitable .

5. Summary

This article introduces how PHP array circular queue traversal, and also introduces the definition, implementation, advantages and disadvantages of circular queues and other related knowledge. Circular queue is an important concept in data structure. It can be used to solve the problem of space waste in ordinary queues, improve the utilization of storage space, and improve the storage efficiency of data. In actual development, you can choose a circular queue or a normal queue as needed to adapt to different application scenarios.

The above is the detailed content of How to loop queue in php array. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn