js用数组实现Stack
2016-10-11 07:58
281 查看
"use strict"; function Stack() { this.dataStore = []; this.stackSize = 0; this.top = 0; this.pop = pop; this.push = push; this.peek = peek; this.clear = clear; this.length = length; this.empty = empty; } function pop() { return this.dataStore[--this.top]; } function push(element) { this.dataStore[this.top++] = element; } function peek() { return this.dataStore[this.top - 1]; } function clear() { delete this.dataStore; this.dataStore = []; this.top = 0; } function length() { return this.top; } function empty() { return this.top === 0; } console.log("test the Stack"); var stack = new Stack(); stack.push("hello"); stack.push("world"); console.log("stack length=" + stack.length() + ", isEmpty=" + stack.empty() + ", topElement=" + stack.peek()); stack.pop(); console.log("stack length=" + stack.length() + ", isEmpty=" + stack.empty() + ", topElement=" + stack.peek());
相关文章推荐
- 用js实现随机返回数组的一个元素
- JS 数组 移除 实现代码
- js对数组中的数字从小到大排序实现代码
- 用一个数组实现三个stack
- JS随即打乱数组实现代码
- 用Js数组来实现下拉列表连动无刷新
- 数组实现stack
- JS 实现 创建类、继承、方法添加、对象克隆、数组封装操作
- 用数组实现Stack
- 编写js扩展方法实现判断一个数组中是否包含某个元素
- js 数组实现一个类似ruby的迭代器
- js数组传入后台非ajax实现
- asp.net通过js实现Cookie创建以及清除Cookie数组的代码
- js对数组中的数字从小到大排序实现代码
- 数据结构之stack,queue的数组与链表实现
- C++实现一个数组类及其stack适配器
- JS 在数组插入字符的实现代码(可参考JavaScript splice() 方法)
- C++用数组和链表分别实现Stack
- 实现JS数组的深拷贝
- 编写js扩展方法实现判断一个数组中是否包含某个元素