内容简介:这是书上的栈,队列感觉怪怪的
包工头带你通算法--js 描述 栈,队列(一)
简单叙述下栈 队列
- 栈:先进后出 ,队列:先进先出(就是这么简单)。 个人理解, 栈就是给个数组a = [1,2,3,4] 只能进行pop(), 和push()操作, 队列就是只能进行shift() push()操作
- talk is cheap show me the picture(左栈 右队列)
这是书上的栈,队列
感觉怪怪的
都9012年了
class Stack{
constructor(){
this.data = [];
this.top = 0;
this.length = 0;
}
// 入栈
push(ele){
this.length ++;
return this.data[this.top ++] = ele;
}
// 出栈
pop(){
this.top > 0 ? this.top -- : false;
this.length > 0 ? this.length -- : false;
return this.data.pop();
}
// 清空栈
clear(){
this.top = 0;
this.length = 0;
return this.data = [];
}
// 返回栈顶元素
posTop(){
return this.data[-- this.top]; // 这里要先减 再赋值
}
}
var st = new Stack();
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Hit Refresh
Satya Nadella、Greg Shaw / HarperBusiness / 2017-9-26 / USD 20.37
Hit Refresh is about individual change, about the transformation happening inside of Microsoft and the technology that will soon impact all of our lives—the arrival of the most exciting and disruptive......一起来看看 《Hit Refresh》 这本书的介绍吧!