How PHP implements the stack data structure and the code example of the brace matching algorithm

Source: Internet
Author: User
This article mainly introduces the PHP implementation of the stack data structure and the matching algorithm, combined with examples of PHP array operation to implement the stack data structure, stack, and stack-based brace matching application skills, the need for friends can refer to the next

In this paper, we describe the stack data structure and brace matching algorithm based on PHP. Share to everyone for your reference, as follows:

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

Stack operation:


Array_pop ()//Tail out Array_push ()//Lujin

Or


Array_shift ()//head into 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.


function Checkmatch ($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 traversal of incomplete while (list ($key, $val) = each ($arr)) {if (In_array ($val, $left, True)) {//into the Stack AR Ray_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 appears, the element at the top of the stack must be the opening parenthesis that matches it (because the parentheses are the corresponding      ), first remove the top element of the stack. if (Isset ($topStack) &&!empty ($topStack)) {if (Array_search ($val, $right, true) = = = Array_search ($topStack, $le Ft,true) {//To determine if the current closing parenthesis matches the left parenthesis//out of the stack Array_pop ($stack);//Match the pop out of the stack}else{//RE Turn false; Left/Right mismatch}}else{//return FALSE;//closing parenthesis Many, because the corresponding opening parenthesis}}} return empty ($stack)?  True:false; After the completion of the cycle to determine whether there is a value in $stack, there are words to 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.

Attached: Queue operations


Array_shift ()//head out Array_push ()//Lujin

Or


Array_unshift//Head in Array_pop//tail out

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.