Implementation of stacks in javascip_javascript tips-js tutorial

Source: Internet
Author: User
Tags decimal to binary
This article mainly introduces the stack details of JavaScript data structures and algorithms. This article describes stack operations and stack implementation instances, for more information, see the first part of the data structure, Stack.
StackIt is an ordered set that complies with the following First-In-First-Out principle (LIFO. Stack top is always the latest element.
For example, a stack is like a stack of books in a box. You need to take the books that you want to take out first. (Of course, you cannot win the book first)
You can also see the illustration.

Stack implementation in JavaScipt
First, create a constructor.

/*** Stack constructor */function Stack () {// simulate Stack var item = [] Using arrays;}

Stack requires the following methods:

  • Push (element (s): add several elements to the top of the stack
  • Pop (): removes and returns the top element of the stack.
  • Peek (): returns the top element of the stack.
  • IsAmpty: Check whether the stack is empty. If it is null, true is returned.
  • Clear: removes all elements from the stack.
  • Size: returns the number of elements in the stack.
  • Print: Display All content in the stack as a string

Implementation of the push Method
Note: A new element needs to be added to the stack. The element is located at the end of the queue. That is to say, we can use the push method of the array to simulate the implementation.
Implementation:

/*** Send the element to the stack and place it in the last position of the array * @ param {Any} element. The type is not limited */this. push = function (element) {items. push (element );};

Pop method implementation
Note: The top element of the stack needs to be popped up and the pop-up value is returned. You can use the pop method of the array to simulate the implementation.
Implementation:

/*** Top stack element * @ return {Any} returns the value to be popped up */this. pop = function () {return items. pop ();};

Implementation of the peek Method
Views the top element of the stack, which can be achieved using the array length.
Implementation:

/*** View the top stack element * @ return {Any} return the top stack element */this. peek = function () {return items [items. length-1];}

Implementation of other methods
Note: The first three are the core of the stack method, and the remaining methods are listed here at one time. The queue mentioned below will overlap with this part.
Implementation:

/*** Determine whether the stack is empty * @ return {Boolean} returns true if the stack is empty. If the stack is not empty, returns false */this. isAmpty = function () {return items. length = 0};/*** clear all content in the stack */this. clear = function () {items = [] ;};/*** return stack length * @ return {Number} stack length */this. size = function () {return items. length ;};/*** display all content in the stack with strings */this. print = function () {console. log (items. toString ());};

Practical Application
Stack has many practical applications. There is a decimal to binary function in the book. (If you do not know how to calculate binary, Baidu can) The following is the source code of the function.
The principle is to input the number to be converted, divide it by two and take the integer. Finally, we use the while loop to splice all the numbers in the stack into strings for output.

/*** Convert a 10-digit Number to a 2-digit Number * @ param {Number} decNumber the 10-digit Number to be converted * @ return {Number} The 2-digit Number after conversion number */function pideBy2 (decNumber) {var remStack = new Stack (), rem, binaryString = ''; while (decNumber> 0) {rem = Math. floor (decNumber % 2); remStack. push (rem); decNumber = Math. floor (decNumber/2);} while (! RemStack. isAmpty () {binaryString + = remStack. pop (). toString () ;}return binaryString ;};

So far, stack learning has come to an end, hoping to help you learn how to implement the stack in javascript.

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.