8

前言:

还记得当初用C语言写各种数据结构的苦逼时代嘛,但是用JS来实现栈和队列是如此的简单啊,但是你们真的了解用js模拟栈和队列,就当你们真的很了解了,毕竟是基础知识,我就写几个案例,不喜勿喷

demo1(栈方法):
栈就是先进后出(LIFO),JS就是如此便捷push/pop就可以简单实现栈

var demoArr = new Array();
demoArr.push('111')
demoArr.push('222')
demoArr.push('333')
console.log(demoArr.pop()) // 333

队列

队列是先进先出(FIFO),高3有两种方法实现,shift和push模拟/unshift和pop模拟

demo2(队列方法1)

var demoArr = new Array();
demoArr.push('july');
demoArr.push('deny');
demoArr.push('mirok'); //demoArr = ['july', 'deny', 'mirok'],按先进先出,出来的则是july
console.log(demoArr.shift()) // july

demo2(队列方法2)

var demoArr = new Array();
demoArr.unshift('july')
demoArr.unshift('deny')
demoArr.unshift('mirok')
console.log(demoArr.pop()) //july

flayPig
460 声望379 粉丝

attemp to do something that is defferent.