Data structure and algorithm JavaScript stack (i)

Source: Internet
Author: User

Data structure and algorithm JavaScript this book is more superficial, the advantage is to use JavaScript language to describe the commonly used data structures under the book, many examples come from a number of common interview topics, as the Times, amateur look down on the way to record it

git code Download: https://github.com/JsAaron/data_structure.git

Stack structure

Special list, the elements inside the stack can only be accessed by one end of the list, the top of the stack

First-in, first-out (lfio,last-in-first-out) data structures

JavaScript provides an actionable way to stack push and pop, but pops remove data from the stack

Implementing the implementation class for a stack

The underlying data structure takes an array of

Because pop is removing data from the stack, you need to implement a Find method peek

Implement a Cleanup method clear

Sum of elements in stack find length

Find out if element empty still exists

function Stack () { This. DataStore = []     This. top =0;  This. Push =Push This. Pop =Pop This. Peek =Peek This. length =length;} function push (Element) { This. datastore[ This. top++] =element;} function peek (element) {return  This. datastore[ This. top-1];} function Pop () {return  This. datastore[-- This. top];} function Clear () { This. top =0}function Length () {return  This. Top}

Palindrome

A palindrome is a word, an array, a phrase, the same as the back and forth from the past 12321.ABCBA

Palindrome The simplest idea is to reverse the element if the original element is equal, then it means that this is a palindrome

Here you can use this stack class to manipulate

function Ispalindrome (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; }}ispalindrome ("Aarra")//falseIspalindrome ("Aaraa")//true

Look at this ispalindrome function, in fact, by calling the Stack class, and then passing in the word this element to the decomposition of each of the constituent units to press into the stack, according to the principle of the stack, first into the first out of the principle, through the method of pop in the anti-assembly of this element, The last comparison before and after the assembled, if equal is a palindrome

Recursive

Implementing a factorial algorithm with recursion

5! = 5 * 4 * 3 * 2 * 1 = 120

With recursion

function factorial (n) {    if0) {        return1;     Else {        return1);}    }

Working with Stacks

function fact (n) {vars =NewStack () while(N >1) {        //[5,4,3,2]S.push (n--); }    varProduct =1;  while(S.length () >0) {Product*=S.pop (); }    returnproduct;} Fact (5)// -

Using the while to decrement n = 5 into the stack, and then through a loop or according to the principle of first-in and first-out of the stack, using the Pop method to stack the top out with the product

Data structure and algorithm JavaScript stack (i)

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.