Data structure and algorithm JavaScript description--Use of stacks

Source: Internet
Author: User

There are some issues that are especially suitable for stack resolution. This section describes several examples of this.1) Mutual conversion between the numbering unitA stack can be used to convert a number from one to another. The false idea is to convert the number n to a number based on B, and the algorithm to implement the conversion is as follows. Using stacks, implementing the algorithm in JavaScript is a piece of cake. Here is the definition of the function, which converts the number to a number from two to nine:
//============================ using the Stack class ====================================/** * 1. Conversion between the numbering*/functionmulbase (num, base) {vars =NewStack ();  Do{s.push (num%base); Num= Math.floor (num/base); } while(num > 0); varconverted = "";  while(s.length () > 0) {converted+=S.pop (); }    returnconverted;}//The following shows how to use this method to convert numbers to binary and octal numbers. varnum = 32;varBase = 2;varNewnum =mulbase (num, base); Console.log (Num+ "converted to base" + Base + "is" +newnum); Num= 125; base= 8;varNewnum =mulbase (num, base); Console.log (Num+ "converted to base" + Base + "is" + newnum);
View Code

Print as follows:

2) palindromeA palindrome is a phenomenon in which a word, phrase, or number is the same as when it is written and written from the back to the back. For example, the word "Dad", "racecar" is a palindrome, if you ignore the space and punctuation, the following sentence is a palindrome, "a man, a plan, a Canal:panama"; The number 1001 is a palindrome. Using stacks, it is easy to tell if a string is a palindrome. Each character of the string we get is pressed into the stack from left to right. When the characters in the string are stacked, a reversed string is saved inside the stack, the last character at the top of the stack, and the first character at the bottom of the stack. After the string is fully pressed into the stack, a new string is obtained by continuously popping each letter in the stack, which is exactly the opposite of the original string order.  We only need to compare the two strings, if they are equal, is a palindrome. Code:
//============================ using the Stack class ====================================/** * 2. Determine if a given string is a palindrome*/functionIspalindrome (word) {vars =NewStack ();  for(vari=0; i<word.length; ++i)    {S.push (word[i]); }    varRWord = "";  while(s.length () > 0) {RWord+=S.pop (); }    if(Word = =RWord) {        return true; }Else{        return false; }}//Test Code:varWord = "Hello";if(Ispalindrome (Word)) {Console.log (Word+ "is a palindrome.");}Else{console.log (Word+ "is not a palindrome.");} Word= "Racecar";if(Ispalindrome (Word)) {Console.log (Word+ "is a palindrome.");}Else{console.log (Word+ "is not a palindrome.");}
View Code

Print:

3) Recursive presentationOnly stacks are used here to simulate the recursive process. To demonstrate how to implement recursion with a stack, consider the recursive definition of the factorial function. First look at how the factorial of 5 is defined: 5! = 5X4X3X2X1 = 120 Below is a recursive function that calculates the factorial of any number: Use the stack to simulate the calculation 5! Process, first press the number from 5 to 1 into the stack, and then use a loop, the number is popped into a multiply, get the correct answer: 120. Code:
//============================ using the Stack class ====================================/** * 3. Using the stack to simulate the recursive process*/functionfact (n) {vars =NewStack ();  while(N > 1) {s.push (n--); }    varProduct = 1;  while(s.length () > 0) {Product*=S.pop (); }    returnproduct;} Console.log (Fact (5));//Display
View Code

Data structure and algorithm JavaScript description--Use of stacks

Related Article

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.