JavaScript data structures and algorithms reading notes > Fourth stacks

Source: Internet
Author: User
Tags stack pop

1. Operation of the Stack

A stack is a special list of elements that can only be accessed by one end of the list, that is, the top of the stack. Similar to a pile of tired plates, can only be placed in the last, the first can be accessed.

That's what we call a post-in-first-out (LIFO).

On the stack mainly into the stack push, out of the stack pop, get stack top element peek, three methods.

2. Implementation of the Stack

The basic class structure is as follows:

function Stack () {    this. DataStore = [];      this. top = 0;     this. Push = push;     this. pop = pop;     this. Peek = peek;
this.length = length;
This.clear = clear;
}

About the in-stack operation:

function push (obj) {     this. datastore[. top++] = obj;} 

Out Stack:

function Pop () {
if (this.top!=0) { returnthis. datastore[--this. Top];
}else{
return undefined;
}}

Gets the top element of the stack:

function Peek () {    returnthis. datastore[this. top-1];}

Get Length:

function Length () {    returnthis. Top;}

Empty:

function Clear () {     this. top = 0;} 

3 Using the Stack class

a) mutual conversion of the numbering unit

  You can use the stack to do 10-in-2~9 binary operations

Here's how: a decimal number A, binary B

1> will a%b, press into the stack

2> Replace A with A/b

3> if a is greater than 0, continue to hit 1> repeatedly

If it is less than 0, jump out

4> the elements of the stack pop up once, forming a new character (the character is the result of conversion completion)

As an example:

  10 to 2 binary:

10%2 = 0--into the stack--0

5%2 =--1 in the stack, 0

2%2 = 0--into the stack--0, 1, 0

1%2 =--1 in stacks, 0, 1, 0

The last stack order is 1010.

The code is as follows:  

functionmulnum (num, base) {varstack =NewStack (); //Operation three     while(num>0){        //Operation OneStack.push (num%base); //operation two (attention to rounding)num = Math.floor (num/base);    }       varrs = ";  for(stack.length>0) {RS+=Stack.pop (); }   returnrs;}

b) palindrome

  

functionispalindromic (str) {varstack =NewStack ();  for(vari=0;i<str.length;i++) {Stack.push (str[i]); }    varRevstr;  while(Stack.length () >0) {Revstr+=Stack.pop (); }    if(Revstr = = =str) {        return true; }Else{        return false; }}

is to flip it through a stack.

C) Recursive demo

In the first chapter, we refer to the recursive method of factorial, where we use stack to find factorial and simulate recursion:

function (num) {    var rs = 1;     var New Stack ();      while (num>1) {       stack.push (num--);    }      while (stack.length>0) {       *= stack.pop ();    }     return rs;}

JavaScript data structures and algorithms reading notes > Fourth 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.