PHP bidirectional queue, double-ended queue code

Source: Internet
Author: User

<?PHP/** * User:jifei * date:2013-07-30 * time:23:12*//** * PHP implements two-way queue, double-ended queue * Double-ended queue (deque, full name double-ended queue) is a data structure with queue and stack properties. * Elements in a double-ended queue can be ejected from both ends, and insert and delete operations are scoped to both sides of the queue. */classdeque{ Public $queue=Array(); /** * Constructor Initialization queue*/     Public function__construct ($queue=Array())    {        if(Is_array($queue))        {            $this->queue=$queue; }    }    /** * Get the first element*/     Public functionFront () {return Reset($this-queue); }    /** * Get last element*/     Public functionBack () {return End($this-queue); }    /** * To determine if it is empty*/     Public functionIs_empty () {return Empty($this-queue); }    /** * Queue size*/     Public functionsize () {return Count($this-queue); }    /** * Insert to Tail*/     Public functionPush_back ($val)    {        Array_push($this->queue,$val); }    /** * Insert to head*/     Public functionPush_front ($val)    {       Array_unshift($this->queue,$val); }    /** * Remove the last element*/     Public functionPop_back () {return Array_pop($this-queue); }    /** * Remove the first element*/     Public functionPop_front () {return Array_shift($this-queue); }    /** * Empty queue*/     Public functionClear () {$this->queue=Array(); }}//initialize a two-way queue$deque=NewDeque (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;//pop-up element testEcho $deque->pop_back ().Php_eol;Echo $deque->pop_front ().Php_eol;Echo $deque->size ().Php_eol;Echo Php_eol;$deque->push_back (' a ').Php_eol;$deque->push_front (0).Php_eol;Echo Php_eol;//Insert TestEcho $deque->front ().Php_eol;Echo $deque->back ().Php_eol;Echo $deque->size ().Php_eol;Echo Php_eol;//Empty Test$deque-Clear ();Echo $deque->is_empty ();

PHP bidirectional queue, double-ended queue code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.