search
Homephp教程PHP源码PHP two-way queue, double-ended queue code

<?php
/**
 * PHP实现双向队列,双端队列
 * 双端队列(deque,全名double-ended queue)是一种具有队列和栈性质的数据结构。
 * 双端队列中的元素可以从两端弹出,插入和删除操作限定在队列的两边进行。
 */
class Deque
{
    public $queue=array();
    /**
     * 构造函数初始化队列
     */
    public function __construct($queue=array())
    {
        if(is_array($queue))
        {
            $this->queue=$queue;
        }
    }
    /**
     * 获取第一个元素
     */
    public function front()
    {
        return reset($this->queue);
    }
    /**
     * 获取最后一个元素
     */
    public function back()
    {
        return end($this->queue);
    }
    /**
     * 判断是否为空
     */
    public function is_empty()
    {
       return empty($this->queue);
    }
    /**
     * 队列大小
     */
    public function size()
    {
       return count($this->queue);
    }
    /**
     * 插入到尾
     */
    public function push_back($val)
    {
        array_push($this->queue,$val);
    }
    /**
     * 插入到头
     */
    public function push_front($val)
    {
       array_unshift($this->queue,$val);
    }
    /**
     * 移除最后一个元素
     */
    public function pop_back()
    {
       return array_pop($this->queue);
    }
    /**
     * 移除第一个元素
     */
    public function pop_front()
    {
        return array_shift($this->queue);
    }
    /**
     * 清空队列
     */
    public function clear()
    {
        $this->queue=array();
    }
}
//初始化一个双向队列
$deque=new Deque(array(1,2,3,4,5));
echo $deque->size().PHP_EOL;
echo $deque->is_empty().PHP_EOL;
echo $deque->front().PHP_EOL;
echo $deque->back().PHP_EOL;
echo PHP_EOL;
//弹出元素测试
echo $deque->pop_back().PHP_EOL;
echo $deque->pop_front().PHP_EOL;
echo $deque->size().PHP_EOL;
echo PHP_EOL;
$deque->push_back(&#39;a&#39;).PHP_EOL;
$deque->push_front(0).PHP_EOL;
echo PHP_EOL;
//插入测试
echo $deque->front().PHP_EOL;
echo $deque->back().PHP_EOL;
echo $deque->size().PHP_EOL;
echo PHP_EOL;
//清空测试
$deque->clear();
echo $deque->is_empty();


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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software