今天就跟大家聊聊有关使用JavaScript怎么实现一个栈的数据结构,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
堆栈(英语:stack),也可直接称栈,在计算机科学中,是一种特殊的串列形式的数据结构,它的特殊之处在于只能允许在链接串列或阵列的一端(称为堆叠顶端指标,英语:top)进行加入数据(push)和输出数据(pop)的运算。另外栈也可以用一维数组或连结串列的形式来完成。
由于堆叠数据结构只允许在一端进行操作,因而按照后进先出(LIFO, Last In First Out)的原理运作。 – 维基百科
实现一个Stack类
/**
* 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 []
用栈的思想将数字转换为二进制和八进制
/**
* 将数字转换为二进制和八进制
*/
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
用栈的思想判断给定字符串或者数字是否是回文
/**
* 判断给定字符串或者数字是否是回文
*/
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
看完上述内容,你们对使用JavaScript怎么实现一个栈的数据结构有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。