Stack and queue implementations (PHP)

Source: Internet
Author: User

Recently often asked the stack and queue algorithm problem, previously on the list, the binary tree has the understanding, stacks and queues feel a bit confused, and later looked at the discovery is to use the array simulation FIFO and advanced after the situation.

It's better to ask this than to ask the list of the moment.

The following return to the point: First look at the stack

<?PHPclassstack{ Public Static $dataInfo=Array();  Public Static $theMax= 10;  Public Static $length=0; //  Public Static $stackInfo;  Public functionAddData ($data){         if(Self::$length>= Self::$theMax){             return false; }Else{           Array_push(Self::$dataInfo,$data); Self::$length++; }              }      Public functionOutdata () {if(Self::$length<=0 ){                          return false; }Else{            $data=Array_pop(Self::$dataInfo); Self::$length--; return $data; }                       }           Public functionGetAll () {foreach(Self::$dataInfo  as $value) {             //Code:             Print_r($value); }     }      }$a=NewStack ();$a->adddata (1);$a->adddata (3);$a->adddata (5);$a-getAll ();Print_r($a-outdata ());?>

Similarly, the queue is the same, but you can change Array_pop () to Array_shift ().

Have any questions please feel free to ask questions.

Stack and queue implementations (PHP)

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.