js:数据结构笔记3--栈

栈是一种特殊的列表,数据结构为LIFO;

定义:

function Stack() {
this.dataStore = [];
this.top = 0;
this.push = push;
this.pop = pop;
this.peek = peek;
this.length = length;
this.clear =clear;
}
function push(elem) {
this.dataStore[this.top++] = elem;
}
function pop() {
var data = this.dataStore[--this.top];
this.dataStore.length = this.top;
return data;
}
function peek() {
return this.dataStore[--this.top];
}
function length() {
return this.top;
}
function clear() {
this.top = 0;
}

 例子:数制转换(2-9): demo

回文判断:demo

模拟递归:demo

  

上一篇:学习微信小程序之css7


下一篇:如何限制虚拟主机可使用的CPU资源