How to implement a two-way queue code instance using PHP

Source: Internet
Author: User
Main theme: The function of the array

Array_pop Array_push

   the Array_pop array_pop () function deletes the last element in the array. Delete the trailing  Array_push the    Array_push () function adds one or more elements (into the stack) to the end of the array of the first parameter, and then returns the length of the new array.   the function equals multiple calls $array [] = $value. The tail plugs into a

Array_unshift Array_shift

The Array_shift ()       function deletes the first element in the array and returns the value of the deleted element. Delete Header  the first array_unshift ()      function is used to insert a new element into an array. The value of the new array is inserted at the beginning of the array. Insert Head First

Reset End

The Reset   reset () function points the internal pointer to the first element in the array and outputs it. The  End    End () function points the array's internal pointer to the last element and returns the value of the element, if successful.

Implementation code:

<?phpclass Deque {public $queue = array ();     /** (tail) Queue **/public Function addlast ($value) {return Array_push ($this->queue, $value);     }/** (tail) out of Team **/public Function Removelast () {return Array_pop ($this->queue);     }/** (head) Queue **/public Function AddFirst ($value) {return Array_unshift ($this->queue, $value);     }/** (head) out of the team **/public Function Removefirst () {return array_shift ($this->queue);    }/** Empty Queue **/public Function Makeempty () {unset ($this->queue);     }/** Gets the column header **/public function GetFirst () {return reset ($this->queue);    } &nbsp;     /** Gets the column footer **/public function GetLast () {return end ($this->queue);    }&nbsp;     /** gets the length **/public function GetLength () {return count ($this->queue); }    }

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.