建设外国商城网站,重庆安全员c证在哪里报名,黄页88免费发布信息网,wordpress娱乐资源网上一篇文章我们理解了List这种数据结构#xff0c;知道了它的特点和一些使用场景#xff0c;这篇文章我们就来看一下栈这种数据结构#xff0c;这里的栈可不是客栈哦#xff0c;哈哈
栈其实和List非常像#xff0c;使用javascript实现都是基于数组来实现
尝试理解Stack …上一篇文章我们理解了List这种数据结构知道了它的特点和一些使用场景这篇文章我们就来看一下栈这种数据结构这里的栈可不是客栈哦哈哈
栈其实和List非常像使用javascript实现都是基于数组来实现
尝试理解Stack
1.栈只能在栈顶进行入栈和出栈 我们可以尝试把栈想象成一个瓶子瓶子只有一个瓶口所有的东西都只能从瓶口塞进去丛瓶口拿出来 2. 栈是一种后进先出的数据结构LIFOlast-in-first-out最后塞进瓶子的东西一定最先从瓶子里面拿出来 3. 栈也有自己的属性和方法瓶子里面可以塞很多东西我们也可以取出瓶子里的东西或清空整个瓶子
代码实现
function Stack () {// 当前栈的数据this.data [];// 栈顶位置this.top 0// 向栈中压入一个元素this.push function (elem) {this.data[this.top] elem}// 从栈中弹出(删除)栈顶元素并返回this.pop function() {return this.data[--this.top]}// 仅返回栈顶元素不删除this.peek function() {return this.data[this.top-1]}// 返回栈中的元素个数this.length function() {return this.top}// 清空栈this.clear function() {this.top 0}
}测试一下
const s new Stack();
s.push(David);
s.push(Raymond);
s.push(Bryan);
console.log(当前栈长度length:, s.length());
console.log(当前栈顶元素为, s.peek());const popped s.pop()
console.log(被弹出的栈顶元素为:, popped);
console.log(当前栈顶元素为, s.peek());
s.push(Cynthia);
console.log(当前栈顶元素为, s.peek());
s.clear()
console.log(执行了清空栈);
console.log(当前栈长度length:, s.length());
console.log(当前栈顶元素为, s.peek());
s.push(Clayton);
console.log(当前栈顶元素为, s.peek());测试结果
实际应用
进制转换
/*** 进制转换* param num * param base */
function mulBase(num, base) {const s new Stack()do {s.push(num%base)num Math.floor(num/base)} while (num 0)let converted while(s.length() 0) {converted s.pop()}return converted
}
console.log(将10转换为二进制, mulBase(10, 2))
console.log(将32转换为二进制, mulBase(32, 2))
console.log(将125转换为八进制, mulBase(125, 8))2. 判断回文字符串
/*** 判断一个字符串是否回文字符串* param word 需要判断的字符串*/
function isPalindrome(word) {const s new Stack()for(let i 0; i word.length; i ) {s.push(word[i])}let rword while(s.length() 0) {rword s.pop()}if(word rword) return truereturn false
}
const word hello;
if (isPalindrome(word)) {console.log(word 是回文字符串);
}
else {console.log(word 不是回文字符串);
}const word1 racecar
if (isPalindrome(word1)) {console.log(word1 是回文字符串);
}
else {console.log(word1 不是回文字符串);
}3. 模拟递归过程阶乘函数
/*** 使用栈模拟递归过程返回n的阶乘 n!* param n * returns */
function fact(n) {const s new Stack()while (n 1) {s.push(n--)}let product 1while(s.length() 0) {product * s.pop()}return product
}
console.log(5的阶乘为, fact(5))表达式括号匹配问题
/*** 计算某个表达式中的括号是否匹配* param str 表达式* returns 匹配情况*/
function isMatch (str) {const match {match: true,position: -1}const left new Stack()const right new Stack()const ml [(, [, {]const mr [ ), ], }]for (let i 0; i str.length; i ) {if (ml.includes(str[i])) {left.push({value: str[i],position: i})}if (mr.includes(str[i])) {right.push({value: str[i],position: i})}}while (left.length() || right.length()) {const l left.pop()const r right.pop()let indexif (l) index ml.findIndex((item) item l.value)else index mr.findIndex((item) item r.value)if (mr[index] ! r?.value || ml[index] ! l?.value) {match.match falsematch.position l ? l.position : r.positonreturn match}}return match
}const string 2.3 23/12 (3.14159 * 0.24
if (!isMatch(string).match) {console.log(表达式${string}括号不匹配不匹配位置为, isMatch(string).position)
} else {console.log(表达式${string}括号匹配成功)
}
const string1 ({ab}(c)ccc[
if (!isMatch(string1).match) {console.log(表达式${string1}括号不匹配不匹配位置为, isMatch(string1).position)
} else {console.log(表达式${string1}括号匹配成功)
}好了文章就到这里了大家如果想了解更多就去看《数据结构与算法javascript描述》这本书吧希望大家都能有所收获~ 文章转载自: http://www.morning.rqnhf.cn.gov.cn.rqnhf.cn http://www.morning.hhboyus.cn.gov.cn.hhboyus.cn http://www.morning.htsrm.cn.gov.cn.htsrm.cn http://www.morning.rfqkx.cn.gov.cn.rfqkx.cn http://www.morning.qrnbs.cn.gov.cn.qrnbs.cn http://www.morning.xirfr.cn.gov.cn.xirfr.cn http://www.morning.pwsnr.cn.gov.cn.pwsnr.cn http://www.morning.tjqcfw.cn.gov.cn.tjqcfw.cn http://www.morning.hgwsj.cn.gov.cn.hgwsj.cn http://www.morning.hwlmy.cn.gov.cn.hwlmy.cn http://www.morning.mqmmc.cn.gov.cn.mqmmc.cn http://www.morning.bgqr.cn.gov.cn.bgqr.cn http://www.morning.rftk.cn.gov.cn.rftk.cn http://www.morning.qinhuangdjy.cn.gov.cn.qinhuangdjy.cn http://www.morning.4q9h.cn.gov.cn.4q9h.cn http://www.morning.pqyms.cn.gov.cn.pqyms.cn http://www.morning.qyjqj.cn.gov.cn.qyjqj.cn http://www.morning.gyqnp.cn.gov.cn.gyqnp.cn http://www.morning.rshs.cn.gov.cn.rshs.cn http://www.morning.fthcq.cn.gov.cn.fthcq.cn http://www.morning.qfzjn.cn.gov.cn.qfzjn.cn http://www.morning.gpryk.cn.gov.cn.gpryk.cn http://www.morning.srmpc.cn.gov.cn.srmpc.cn http://www.morning.ffksr.cn.gov.cn.ffksr.cn http://www.morning.yrhpg.cn.gov.cn.yrhpg.cn http://www.morning.wmfh.cn.gov.cn.wmfh.cn http://www.morning.nzmhk.cn.gov.cn.nzmhk.cn http://www.morning.grxbw.cn.gov.cn.grxbw.cn http://www.morning.yydzk.cn.gov.cn.yydzk.cn http://www.morning.yxlhz.cn.gov.cn.yxlhz.cn http://www.morning.kpxzq.cn.gov.cn.kpxzq.cn http://www.morning.jcffp.cn.gov.cn.jcffp.cn http://www.morning.rwjtf.cn.gov.cn.rwjtf.cn http://www.morning.xpzgg.cn.gov.cn.xpzgg.cn http://www.morning.npfrj.cn.gov.cn.npfrj.cn http://www.morning.cwgt.cn.gov.cn.cwgt.cn http://www.morning.yktwr.cn.gov.cn.yktwr.cn http://www.morning.jqbmj.cn.gov.cn.jqbmj.cn http://www.morning.jljwk.cn.gov.cn.jljwk.cn http://www.morning.qmxsx.cn.gov.cn.qmxsx.cn http://www.morning.yrjkp.cn.gov.cn.yrjkp.cn http://www.morning.tmxfn.cn.gov.cn.tmxfn.cn http://www.morning.fgkrh.cn.gov.cn.fgkrh.cn http://www.morning.nrddx.com.gov.cn.nrddx.com http://www.morning.htbgz.cn.gov.cn.htbgz.cn http://www.morning.xxwl1.com.gov.cn.xxwl1.com http://www.morning.jbtlf.cn.gov.cn.jbtlf.cn http://www.morning.bzfld.cn.gov.cn.bzfld.cn http://www.morning.krkwh.cn.gov.cn.krkwh.cn http://www.morning.hdwjb.cn.gov.cn.hdwjb.cn http://www.morning.brbmf.cn.gov.cn.brbmf.cn http://www.morning.qhnmj.cn.gov.cn.qhnmj.cn http://www.morning.cryb.cn.gov.cn.cryb.cn http://www.morning.hhxkl.cn.gov.cn.hhxkl.cn http://www.morning.fhxrb.cn.gov.cn.fhxrb.cn http://www.morning.c7623.cn.gov.cn.c7623.cn http://www.morning.pzrnf.cn.gov.cn.pzrnf.cn http://www.morning.kfclh.cn.gov.cn.kfclh.cn http://www.morning.prmbb.cn.gov.cn.prmbb.cn http://www.morning.tmbfz.cn.gov.cn.tmbfz.cn http://www.morning.kbbmj.cn.gov.cn.kbbmj.cn http://www.morning.ychoise.com.gov.cn.ychoise.com http://www.morning.ntcmrn.cn.gov.cn.ntcmrn.cn http://www.morning.glrzr.cn.gov.cn.glrzr.cn http://www.morning.wbfly.cn.gov.cn.wbfly.cn http://www.morning.ysnbq.cn.gov.cn.ysnbq.cn http://www.morning.krdb.cn.gov.cn.krdb.cn http://www.morning.hxlpm.cn.gov.cn.hxlpm.cn http://www.morning.dthyq.cn.gov.cn.dthyq.cn http://www.morning.pqryw.cn.gov.cn.pqryw.cn http://www.morning.mldrd.cn.gov.cn.mldrd.cn http://www.morning.lhqw.cn.gov.cn.lhqw.cn http://www.morning.fppzc.cn.gov.cn.fppzc.cn http://www.morning.wkws.cn.gov.cn.wkws.cn http://www.morning.weiwt.com.gov.cn.weiwt.com http://www.morning.qbgdy.cn.gov.cn.qbgdy.cn http://www.morning.sqgsx.cn.gov.cn.sqgsx.cn http://www.morning.jcxqc.cn.gov.cn.jcxqc.cn http://www.morning.bpmdr.cn.gov.cn.bpmdr.cn http://www.morning.gqryh.cn.gov.cn.gqryh.cn