PHP array implementation stack data structure

Source: Internet
Author: User

Tips: Today preview the four-legged cat PHP data structure, see this example, studied a bit, is a preliminary understanding of the stack structure.

Stack, the embodiment is LIFO, that is, LIFO. queue, which embodies FIFO.

Stack

Array_pop ()//tail out

Array_push ()//Lujin

Or

Array_shift ()//head in

Array_unshift ()//head out

Use case: Verify that a mathematical formula is correct, such as {2*3[x*y+5+m* (i-j)/3]+k* (4+ (t+9))}.

Analysis: For a calculation of the correct or not, is the body now, the matching of the various parentheses, the parentheses exactly match, the calculation is no problem, how to test a formula in the matching of parentheses, met a lot of people think of using the regular. I can't figure out how this is going to be written, how to implement nested relationships. This time the stack will come in handy. Look at the code below.

functionCheckmatch ($str){        if(!$str)return false; $arr=Str_split($str); $left=Array(‘{‘,‘[‘,‘(‘); $right=Array(‘}‘,‘]‘,‘)‘); $stack=Array(); Reset($arr);//using the while traversal array requires reset () to prevent the traversal from being incomplete         while(List($key,$val) = each($arr)){        if(In_array($val,$left,true)){               //into the stack            Array_push($stack,$val);//put all the left brackets that appear in the stack}Else if(In_array($val,$right,true)){            $topStack=End($stack);//If a closing parenthesis is present, the element at the top of the stack must be the opening parenthesis that matches it (because the parentheses correspond), and the top element of the stack is first removed.             if(isset($topStack) &&!Empty($topStack)){                if(Array_search($val,$right,true) ===Array_search($topStack,$left,true)){//determine if the current closing parenthesis matches the left parenthesis//out Stack                    Array_pop($stack);//pop out of the stack if it matches.}Else{                    //                    return false;//no match left or right                }            }Else{                //                return false;//The closing parenthesis is many, because the corresponding opening parenthesis is not taken out            }        }    }        return Empty($stack) ?true:false;//after the completion of the cycle to determine whether there is value in the $stack, some words prove that the left parenthesis more}

$test = ' {2*3[x*y+5+m* (i-j)/3]+k* (4+ (t+9))} ';

Var_dump (Checkmatch ($test));

The stacks in the above code are implemented by Array_pop and Array_push, as well as by Array_shift and Array_unshift.

Queue

Array_shift ()//head out

Array_push ()//Lujin

Or

Array_unshift//Head-in

Array_pop//Tail out

PHP array implementation stack data structure

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.