Home  >  Article  >  Web Front-end  >  Detailed example of how JavaScript implements the stack data structure

Detailed example of how JavaScript implements the stack data structure

黄舟
黄舟Original
2017-08-03 15:31:451206browse

Stack, also known as stack, is a linear table with limited operations. The following article mainly introduces you to the relevant information about using JavaScript to implement the data structure of the stack. The article introduces it through sample code. For details, friends in need can refer to it. Let’s take a look together.

Preface

This article mainly introduces to you the relevant content about the data structure of the JavaScript implementation stack, and shares it for your reference and study. Not much to say, let’s take a look at the detailed introduction:

Stack (English: stack), which can also be directly called a stack, is a special serial data structure in computer science. The special thing is that the operations of adding data (push) and outputting data (pop) are only allowed at one end of the linked list or array (called the stack top indicator, English: top). In addition, the stack can also be implemented in the form of a one-dimensional array or a connected sequence.

Since the stacked data structure only allows operations on one end, it operates according to the principle of LIFO (Last In First Out). – Wikipedia

The above is Wikipedia’s interpretation of the stack. Next we use JavaScript (ES6) code to implement the stack data structure

Implement a Stack class


/**
* Stack 类
*/
class Stack {
 constructor() {
 this.data = []; // 对数据初始化
 this.top = 0; // 初始化栈顶位置
 }

 // 入栈方法
 push() {
 const args = [...arguments];
 args.forEach(arg => this.data[this.top++] = arg);
 return this.top;
 }

 // 出栈方法
 pop() {
 if (this.top === 0) throw new Error('The stack is already empty!');
 const peek = this.data[--this.top];
 this.data = this.data.slice(0, -1);
 return peek;
 }

 // 返回栈顶元素
 peek() {
 return this.data[this.top - 1];
 }

 // 返回栈内元素个数
 length() {
 return this.top;
 }

 // 清除栈内所有元素
 clear() {
 this.top = 0;
 return this.data = [];
 }

 // 判断栈是否为空
 isEmpty() {
 return this.top === 0;
 }
}

// 实例化
const stack = new Stack();

stack.push(1);
stack.push(2, 3);
console.log(stack.data); // [1, 2, 3]
console.log(stack.peek()); // 3
console.log(stack.pop()); // 3, now data is [1, 2]
stack.push(3);
console.log(stack.length()); // 3
stack.clear(); // now data is []

Use the idea of ​​stack to convert numbers into binary and octal


/**
 * 将数字转换为二进制和八进制
 */
const numConvert = (num, base) => {
 const stack = new Stack();
 let converted = '';

 while(num > 0) {
 stack.push(num % base);
 num = Math.floor(num / base);
 }

 while(stack.length() > 0) {
 converted += stack.pop(); 
 }

 return +converted;
}

console.log(numConvert(10, 2)); // 1010

Use stack The idea of ​​​​judging whether a given string or number is a palindrome


##

/**
 * 判断给定字符串或者数字是否是回文
 */
const isPalindrome = words => {
 const stack = new Stack();
 let wordsCopy = '';
 words = words.toString();

 Array.prototype.forEach.call(words, word => stack.push(word));

 while(stack.length() > 0) {
 wordsCopy += stack.pop();
 }

 return words === wordsCopy;
}

console.log(isPalindrome('1a121a1')); // true
console.log(isPalindrome(2121)); // false

The above is the implementation of the stack data structure using JavaScript. Some algorithms may Not appropriate, but just to demonstrate JS’s implementation of the stack

The above is the detailed content of Detailed example of how JavaScript implements the stack data structure. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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