PHP implementation Stack and queue

Source: Internet
Author: User

<?phpclass queueop{/* * Team Tail * Return: Number of elements in queue after processing */Public function Tailenquque ($arr, $val) {Retu RN Array_push ($arr, $val);}        /* * End of Team * return: The last value, if the array is empty or not an array, returns NULL * Comment: For two-way queue only */Public function Taildequeue ($arr) {    Return Array_pop ($arr); }/* * Team First queue * Return: Number of elements queued after processing * Comment: For two-way queue only */Public function headenqueue ($arr, $val) {RET    Urn Array_unshift ($arr, $var); }/* Team first OUT team * return: The removed value, if the parameter is not an array or an array is empty, returns NULL */Public Function Headdequeue () {return Array_shift ($    ARR);    }/* Queue Length * return: Returns the length of the queue (number of elements) */Public Function queuelength ($arr) {return count ($arr); }/* * Get team first element * return: The value of the first element, or False if the queue is empty */Public Function Queuehead ($arr) {return Reset (    $arr); }/* Gets the tail element * return: The value of the last element and returns False if the queue is empty */Public Function Queuetail ($arr) {return End ($a    RR); }/* * Empty queue * RetuRN: no return value */Public Function Clearqueue ($arr) {unset ($arr);    }}<?phpclass phpstack{Public $top =-1;    Public $size = 5;        Public $stack = Array ();    Public function __construct ($size) {$this->size = $size;        Public function push ($item) {if ($this->isfull ()) {return;        } $this->top++;    $this->stack[$this->top]= $item;        Public Function Pop () {if ($this->isempty ()) {return;        } $ret = $this->stack[$this->top];        $this->top--;    return $ret;             Public Function Isfull () {if (($this->top+1) >= $this->size) {echo ' The stack is full! ';        return true;            }} Public Function IsEmpty () {if ($this->top==-1) {echo ' The stack is empty ';        return true; }} public Function Show () {for ($i = $this->top; $i >-1; $i-) {echo $i. Element is: ". $this->stack[$i]. "        <br/> "; }}}?>

  

PHP implementation Stack and 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.