JavaScript data structure and algorithm stack detailed _javascript tips

Source: Internet
Author: User

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

One: The operation of the stack.

Stack is a special list, the elements of the stack can only be accessed through one end of the list, which is the top of the stack. For example, washing dishes in the restaurant, you can only wash the top plate, after the plate is washed, can only screw to the top of this stack of plates. The stack is called the "back in First Out" (LIFO) data structure.

Since the stack has a back-first out feature, so any element that is not on top of the stack can not be accessed, in order to get the stack of low elements, you must first take off the above elements. The two main operations we can do on the stack are to push an element onto the stack and pop an element out of the stack. Into the stack we can use the method push () method, the stack we use Pop () method. The Pop () method can access the elements at the top of the stack, but when the method is invoked, the top element of the stack is permanently deleted from the stack. Another method we often use is peek (), which returns only the top element of the stack without deleting it.

The actual columns of the stack and stack are 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 (): Records the number of elements in the stack.

Two: the implementation of the stack is as follows:

We can start by implementing the method of the Stack class first, as follows:

Copy Code code as follows:

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

As above: Datastore is to save all the elements within the stack. The position of the top record stack of the variable, initialized to 0. Indicates that the starting position of the corresponding array on the top of the stack is 0, if an element is pressed into the stack. The value of the variable will change.

There are several ways to do this: push (), pop (), Peek (), clear (), length ();

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

Copy Code code as follows:

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 while minus 1. The following code:
Copy Code code as follows:

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

3. Peek () method returns the element of the first top-1 position of the array, that is, the top element of the stack;
Copy Code code as follows:

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

4. Length () method Sometimes we want to know how many elements in the stack, we can return the top value of the variable to return the number of elements in the stack, the following code:
Copy Code code as follows:

function Length () {
return this.top;
}

5. Clear (); Sometimes we have to empty the stack, we set the variable top value to 0, and the following code:
Copy Code code as follows:

function Clear () {

this.top = 0;

}


All code below:
Copy Code code as follows:

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

Stack.prototype = {
   
   //pressing a new element into the stack
    push: function (Element) {
        this.datastore[this.top++] = element;
    },
   //access stack top element, stack top element permanently deleted
    pop:function () {
  & nbsp;     return this.datastore[--this.top];
   },
   //Returns the element of the top-1 position in the array, that is, the top element of the stack
    peek:function () { br>         return this.datastore[this.top-1];
   },
    How many elements are stored in the stack
    length:function () {
 & nbsp;      return this.top;
   },
   //empty stack
    clear:function () {
         this.top = 0;
   }
};

Demo examples are as follows:

var stack = new stack ();
Stack.push ("a");
Stack.push ("B");
Stack.push ("C");
Console.log (Stack.length ()); 3
Console.log (Stack.peek ()); C

var popped = Stack.pop ();
Console.log (popped); C

Console.log (Stack.peek ()); B

Stack.push ("D");

Console.log (Stack.peek ()); D

Stack.clear ();

Console.log (Stack.length ()); 0

Console.log (Stack.peek ()); Undefined

Here we can implement a recursive definition of a factorial function; like 5! Factorial 5! = 5 * 4 * 3 * 2 * 1;

The following code:

Copy Code code as follows:

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

The above code meaning is: first number 5 incoming function, use while loop, each time since minus 1, the function of using the stack push () into the stack, until the variable n is less than 1.  Then define a variable product and determine whether it is greater than 0 by the length () of the stack and each execution product* = S.pop (); The Pop () method returns the top element of the stack and deletes the element from the stack. So every time you do it, you delete an element until s.length () <= 0. So the Product = 5*4*3*2*1. etc operations.

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.