Balance Questions of the Stack Application Symbolization of JavaScript Data Structure

This example describes the symbol balance problem of the stack application in the JavaScript data structure. Share to everyone for your reference, specifically as follows:

Due to the structure of the stack, we can use it as a useful tool, and the application is introduced below.

The first is the balance problem of symbols. There is a string of strings, we need to judge whether the fixed characters are paired, such as {} [] (), etc. Of course, there are many methods, but the implementation of the stack is relatively simpler.

The JavaScript code to achieve the above algorithm is

        Function isbalance (STR) {var str = str.tostring (), len = str.length () m = new array (); for (var i = 0; i    <len;i++){
      if(str[i]=='['||str[i]=='{'||str[i]=='('){
        myArray.push(str[i]);
      }
      if(str[i]==']'||str[i]=='}'||str[i]==')'){
        if(myArray.length==0){
          document.write("the string 不平衡");
          return false;
        }
        var popValue=myArray.pop();
        switch(str[i]){
          case ']':{
            if('['!=popValue){
              document.write('] 不平衡');
            }
            break;
          }
          case '}':{
            if('{'!=popValue){
              document.write('} 不平衡');
            }
            break;
          }
          case ')':{
            if('('!=popValue){
              document.write(') 不平衡');
            }
            break;
          }
          default:
            break;
        }
      }
    }
  }
  isbalance('{[]}');//true
  isbalance('{[[]]}');//true
  isbalance('(()[])');//true
  isbalance('{[]]');//false
   The  algorithm is intentional 
YES: It is assumed that after reading a string of strings, it is pressed if the left portion of the symmetric symbol is encountered. In the stack, when the right part of the symmetric symbol is encountered, an object in the stack is popped up, the implementation alignment, if it is symmetrical, then the current symbol is balanced, if it is asymmetrical, then the current string is not Balance, when the string is read, if all the symbols are balanced, the stack should be empty, and if it is empty in the judgment stack, the string is symbolized.
More about JavaScript Related Content Interested readers can view this topic: “JavaScript Data Structure and Algorithm Skill Summary”, “Summary of JavaScript Mathematical Computing Law”, “JavaScript Sort Algorithm Summary”, “JavaScript Overall Algorithm and TechnologySummary “,” JavaScript Find Algorithm Skills “and” JavaScript Error and Debugging Skill Summary “

I hope this article will help you JavaScript programming.

© Copyright Notice
THE END
Just support it if you like
like0
share
comment Grab the couch

Please log in to comment