JavaScript data structure and algorithm---stack

Source: Internet
Author: User

JavaScript data structure and algorithm---stack

in the previous blog introduced the following table, the list is the simplest kind of structure, but if you want to deal with some of the more complex structure, the list is too primitive, so we need some sort of similar to the list but more complex data structure---stack. Stacks are an efficient data structure, because data can only be added or deleted at the top of the stack, so this is fast and easy to implement.

One: The operation of the stack.

stacks are a special list of elements that can only be accessed from one end of the list, which is the top of the stack. For example, in the restaurant washing dishes, you can only wash the top plate, the plate after washing, you can only screw to the top of the stack of dishes. The stack is known as a "post-in, first Out" (LIFO) data structure.

Since the stack has a post-in first out feature, so any element not at the top of the stack can not be accessed, in order to get the lower stack elements, you must first take off the above elements. The two main things we can do with the stack are to push an element into the stack and pop an element out of the stack. Into the stack we can use the method push () method, out of the stack we use the Pop () method. Although the Pop () method can access the element at the top of the stack, the top element of the stack is permanently deleted from the stack after the method is called. Another common method we use is peek (), which returns only the top element of the stack without deleting it.

A real-list diagram of the stack and the stack is as follows:

Push (), pop (), and Peek () are the three main methods of the stack, but there are other methods and properties on the stack. As follows:

Clear (): Clears all elements within the stack.

Length (): The number of elements in the record stack.

Second: the implementation of the stack is as follows:

we can start by implementing the Stack class method, as follows:

function Stack () {
This.datastore = [];
this.top = 0;
}

As above: DataStore is the preservation of all elements within the stack. Variable top records the position of the top of the stack, initialized to 0. Indicates that the starting position of the corresponding array at the top of the stack is 0, if an element is pressed into the stack. The value of the variable will vary.

We also have the following methods: Push (), pop (), Peek (), clear (), length ();

1. Push () method; When a new element is pressed into the stack, it needs to be stored in the position of the variable top in the array, and the top value is added 1 to the next position in the array. The following code:

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

2. The pop () method is the opposite of the push () method---it returns the top element of the stack and the top value minus 1. The following code:

function Pop () {
return this.datastore[--this.top];
}

3. The peek () method returns the element at the top-1 position of the array, which is the top element of the stack;

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

4. Length () method Sometimes we need to know how many elements are inside the stack, and we can return the number of elements in the stack by returning the top value of the variable, as in the following code:

function Length () {
return this.top;
}

5. Clear (); Sometimes we want to empty the stack, we set the variable top value to 0, the following code:

function Clear () {

this.top = 0;

}

All the following code:

functionStack () { This. DataStore = [];  This. top = 0;} Stack.prototype= {        //Press a new element into the stackPushfunction(Element) { This. datastore[ This. top++] =element; },    //access the top element of the stack, the top element of the stack is permanently deleted.Popfunction(){        return  This. datastore[-- This. Top]; },    //returns an element of the top-1 position in the array, that is, the top element of the stackPeek:function(){        return  This. datastore[ This. top-1]; },    //How many elements are stored in a stackLengthfunction(){        return  This. Top; },    //Empty StackClearfunction(){         This. top = 0; The example;d emo is as follows:varstack =NewStack (); Stack.push (A); Stack.push ("B"); Stack.push (C); Console.log (Stack.length ()); //3Console.log (Stack.peek ());//Cvarpopped =Stack.pop (); Console.log (popped); //CConsole.log (Stack.peek ());//bStack.push ("D"); Console.log (Stack.peek ()); //Dstack.clear (); Console.log (Stack.length ()); //0Console.log (Stack.peek ()); //undefined

Below we can implement a recursive definition of a factorial function, such as 5! The factorial 5! = 5 * 4 * 3 * 2 * 1;

The following code:

  Fact (n) { var  s = new   Stack ();   while  (n > 1) {S.push (n --);     var  product = 1 while  (S.length () > 0*= S.pop ();  return   product; Console.log (Fact ( 5));  

The above code means: the first number 5 into the function, using the while loop, each time the self-reduction of 1 before the function push () to use the stack into the stack, until the variable n is less than 1.  Then a variable product is defined, and the length () of the stack is used to determine whether it is greater than 0 and each execution product* = S.pop (); The Pop () method returns the top element of the stack and deletes the element from the stack. So each time it executes, it deletes an element until s.length () <= 0. So product = 5*4*3*2*1.

JavaScript data structure and algorithm---stack

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.