JavaScript 数据结构(1):栈 Stack
来源:互联网 发布:商标域名是什么 编辑:程序博客网 时间:2024/06/08 00:39
/** * 栈(Stack) * 后进先出 */// Array 实现/*function Stack () { var arr = []; return { push: function(item){ // 入栈 arr.push(item); }, pop: function(){ // 出栈 return arr.pop(); }, peek: function(){ // 查看栈顶元素 return arr[arr.length-1]; }, isEmpty: function(){ // 判读栈是否为空 return arr.length === 0; } };}*/// Object 实现function Stack () { var stack = {}, top = -1; return { push: function(item){ // 入栈 stack[++top] = item; }, pop: function(){ // 出栈 if(top < 0){ return undefined; }else{ return stack[top--]; } }, peek: function(){ // 查看栈顶元素 if(top < 0){ return undefined; }else{ return stack[top]; } }, isEmpty: function(){ // 判读栈是否为空 return top < 0; } };}// testvar stack = new Stack();console.dir(stack);console.log(stack.isEmpty());stack.push(1);stack.push(2);console.log(stack.isEmpty());console.log(stack.peek());console.log(stack.pop());console.log(stack.peek());
0 0
- JavaScript 数据结构(1):栈 Stack
- JavaScript数据结构之--------栈(Stack)
- 栈(stack )数据结构
- 基本数据结构:栈(stack)
- 基本数据结构:栈(stack)
- 基本数据结构:栈(stack)
- 数据结构(Data Structure)——1、栈(Stack)
- PHP实现栈(Stack)数据结构
- C++数据结构之Stack(栈)
- 【数据结构】顺序栈 Stack
- 数据结构-06 栈Stack
- 【数据结构】栈 Stack
- 数据结构复习 - 栈Stack
- 【数据结构】栈stack
- 数据结构基础:栈(Stack)
- C#数据结构--栈Stack
- 数据结构之栈stack
- 【数据结构】堆栈(Stack)
- 开源机器学习新工具RWeka(R Meets Weka )
- Redis Sentinel:集群Failover解决方案
- 使用spring @Scheduled注解执行定时任务、
- Fragment软键盘的显示和隐藏
- 【FAQ】oclint-xcodebuild error:cannot find original pch source file?
- JavaScript 数据结构(1):栈 Stack
- Kibana User Guide [4.2] » Getting Kibana Up and Running
- 使用synergy设置mac和ubuntu共享一套鼠标键盘
- 为什么要覆盖Java中的equals和hashcode
- unity3d OnMouseDown()函数没被调用
- 迅速增强公司信息安全性的10大划算方式
- 互联网金融青年会成立一周年庆典在京举办
- 在一串字符中匹配出4个连续的数字
- 轻松把玩HttpClient之封装HttpClient工具类(二),插件式配置HttpClient对象