Home > Web Front-end > JS Tutorial > Implementation method of stack in JavaScipt_javascript skills

Implementation method of stack in JavaScipt_javascript skills

WBOY
Release: 2016-05-16 15:15:09
Original
913 people have browsed it

The next step is the first part of the data structure, stack.
Stack is an ordered collection that follows the last-in-first-out principle (LIFO, full name: Last In First Out). The top of the stack is always the newest element.
For example: a stack is like a stack of books placed in a box. If you want to take the bottom book, you must first remove the top book. (Of course, you can’t take the book below first)
You can also understand it by looking at the diagram.

Implementation of stack in JavaScipt
First, create a constructor.

/**
 * 栈的构造函数
 */
function Stack() {

 // 用数组来模拟栈
 var item = [];
}

Copy after login

The stack needs to have the following methods:

  • push(element(s)): Add several elements to the top of the stack
  • pop(): Remove and return the top element of the stack
  • peek(): Return the top element of the stack
  • isAmpty: Check whether the stack is empty, if it is empty, return true
  • clear: Remove all elements from the stack
  • size: Returns the number of elements in the stack.
  • print: Display all contents in the stack as a string

Implementation of push method
Explanation: New elements need to be added to the stack, and the element position is at the end of the queue. In other words, we can use the push method of the array to simulate the implementation.
Implementation:

/**
 * 将元素送入栈,放置于数组的最后一位
 * @param {Any} element 接受的元素,不限制类型
 */
this.push = function(element) {
 items.push(element);
};
Copy after login

Implementation of pop method
Explanation: It is necessary to pop the top element of the stack and return the popped value at the same time. You can use the pop method of the array to simulate the implementation.
Implementation:

/**
 * 弹出栈顶元素
 * @return {Any} 返回被弹出的值
 */
this.pop = function() {
 return items.pop();
};
Copy after login

Implementation of peek method
Note: Viewing the top element of the stack can be achieved by using the array length.
Implementation:

/**
 * 查看栈顶元素
 * @return {Any} 返回栈顶元素
 */
this.peek = function() {
 return items[items.length - 1];
}
Copy after login

Implementation of other methods
Note: The first three are the core of the stack method, and the remaining methods are listed here at once. Because the queue to be discussed below will greatly overlap with this part.
Implementation:

/**
 * 确定栈是否为空
 * @return {Boolean} 若栈为空则返回true,不为空则返回false
 */
this.isAmpty = function() {
 return items.length === 0
};

/**
 * 清空栈中所有内容
 */
this.clear = function() {
 items = [];
};

/**
 * 返回栈的长度
 * @return {Number} 栈的长度
 */
this.size = function() {
 return items.length;
};

/**
 * 以字符串显示栈中所有内容
 */
this.print = function() {
 console.log(items.toString());
};

Copy after login

Practical Application
There are many practical applications of the stack. There is a function in the book that converts decimal to binary. (If you don’t know how to calculate binary, you can use Baidu.) The following is the source code of the function.
The principle is to enter the number to be converted, continuously divide by two and round. And finally use a while loop to concatenate all the numbers in the stack into a string for output.

/**
 * 将10进制数字转为2进制数字
 * @param {Number} decNumber 要转换的10进制数字
 * @return {Number}      转换后的2进制数字
 */
function divideBy2(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;
};

Copy after login

At this point, the study of the stack has come to an end. I hope it will be helpful for everyone to learn how to implement the stack in JavaScript.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template