Stack explanation of JavaScript data structures and algorithms _ javascript skills

Source: Internet
Author: User
This article mainly introduces the stack details of JavaScript data structures and algorithms. This article describes stack operations and stack implementation instances, if you need a list, you can refer to the list described in the previous blog. The list is the simplest structure. However, if you want to process complicated structures, the list is too simple, so we need a data structure that is similar to the list but more complex-stack. Stack is an efficient data structure. Because data can only be added or deleted at the top of the stack, this operation is fast and easy to implement.

I. Stack operations.

Stack is a special list. The elements in the stack can only be accessed through one end of the list, which is the top of the stack. For example, if you wash dishes in a restaurant, you can only wash the top dishes first. After the dishes are washed, you can only screw them to the top of the pile of dishes. Stack is called the data structure of "post-import first-out" (LIFO.

Because the stack has the characteristics of "back-in-first-out", no elements that are not at the top of the stack can be accessed. To get the low-level elements of the stack, you must first remove the above elements. The two main operations we can perform on the stack are to push an element into the stack and bring an element out of the stack. In the stack, we can use the push () method, and the pop () method. Although the pop () method can access the elements at the top of the stack, after calling this method, the elements at the top of the stack will be permanently deleted from the stack. Another method we commonly use is peek (). This method returns only the top element of the stack without deleting it.

The real columns of the inbound and outbound stacks are shown as follows:

Push (), pop (), and peek () are the three main methods of stack, but the stack also has other methods and attributes. As follows:

Clear (): clear all elements in the stack.

Length (): records the number of elements in the stack.

Ii. stack implementation:

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

The Code is as follows:


Function Stack (){
This. dataStore = [];
This. top = 0;
}

As shown above: dataStore stores all elements in the stack. Variable top records the position at the top of the stack. Initialization is 0, indicating that the starting position of the array corresponding to the top of the stack is 0. If any element is pushed into the stack. The variable value changes accordingly.

We also have the following methods: push (), pop (), peek (), clear (), length ();

1. push () method; when a new element is pushed into the stack, you need to save it to the position corresponding to the top variable in the array, and then add the top value to 1, point it to the next position in the array. The following code:

The Code is 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 and reduces the top value by 1. The following code:

The Code is as follows:


Function pop (){
Return this. dataStore [-- this. top];
}


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

The Code is as follows:


Function peek (){
Return this. dataStore [this. top-1];
}


4. The length () method sometimes needs to know the number of elements in the stack. We can return the number of elements in the stack by returning the top value of the variable, as shown in the following code:

The Code is as follows:


Function length (){
Return this. top;
}


5. clear (); sometimes we need to clear the stack and set the top variable value to 0. The following code:

The Code is as follows:


Function clear (){

This. top = 0;

}


All the following code:

The Code is as follows:


Function Stack (){
This. dataStore = [];
This. top = 0;
}

Stack. prototype = {

// Add a new element to the stack
Push: function (element ){
This. dataStore [this. top ++] = element;
},
// Access the top element of the stack. The top element of the stack is permanently deleted.
Pop: function (){
Return this. dataStore [-- this. top];
},
// Return the top-1 element in the array, that is, the top element of the stack.
Peek: function (){
Return this. dataStore [this. top-1];
},
// How many elements are stored in the stack
Length: function (){
Return this. top;
},
// Clear the stack
Clear: function (){
This. top = 0;
}
};

The demo instance is as follows:

Var stack = new Stack ();
Stack. push ("");
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

Now we can implement the recursive definition of a factorial function; for example, 5! Factorial 5! = 5*4*3*2*1;

The following code:

The Code is 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 indicates that number 5 is used to pass in the function and while loop is used. Before each auto-minus 1, push () the function that uses the stack is pushed to the stack, until Variable n is less than 1. Define a variable product. Use the stack length () method to determine whether the value is greater than 0 and each execution of product * = s. the pop (); pop () method returns the top element of the stack and deletes the element from the stack. Therefore, each execution deletes an element until s. length () <= 0. Therefore, product = 5*4*3*2*1. And so on.

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.