javascript数据结构(栈)解决佩兹糖果盒问题
来源:互联网 发布:网络安全保密责任书 编辑:程序博客网 时间:2024/05/18 03:05
function Stack() { this.top = 0; this.dataStore = []; this.push = push; this.pop = pop; this.peek = peek; this.length = length; this.clear = clear; this.toString = toString;};//push压栈function push(element) { this.dataStore[this.top++] = element;};//pop出栈function pop() { return this.dataStore[--this.top];};//peek查询栈顶元素function peek() { return this.dataStore[this.top-1];};//length返回栈内元素个数function length() { return this.top;};//clear清空栈内元素function clear() { this.top = 0;};
var sweetBox = new Stack();sweetBox.push('red');sweetBox.push('yellow');sweetBox.push('red');sweetBox.push('yellow');sweetBox.push('red');sweetBox.push('white');sweetBox.push('yellow');sweetBox.push('white');sweetBox.push('yellow');sweetBox.push('white');sweetBox.push('red');//取出不喜欢的颜色 getfunction getColor(element,stack) { var getColorStack = new Stack(); var setColorStack = new Stack(); while(stack.length()>0){ if(stack.peek() == element){ getColorStack.push(element); stack.pop(); }else{ setColorStack.push(stack.peek()); stack.pop(); } } while (setColorStack.length()>0){ stack.push(setColorStack.peek()); setColorStack.pop(); } console.log(stack.peek());}getColor('red',sweetBox);
0 0
- javascript数据结构(栈)解决佩兹糖果盒问题
- oldssoj1074糖果盒(悬线法)
- 糖果盒
- 【数据结构】使用栈Stack解决迷宫问题
- 数据结构用栈解决n皇后问题
- 数据结构-顺序栈解决迷宫问题
- 奥运场馆规划问题(数据结构解决)
- 【悬线法】糖果盒
- 解决javascript乱码问题
- 【数据结构】使用栈解决火车硬席(H)和软席(S)的调度问题
- 数据结构(八) 用顺序栈解决括号匹配的问题
- 用C++链式栈解决数据结构中的括号匹配问题。
- 数据结构——用栈解决简单迷宫问题
- 数据结构 实验二:解决约瑟夫问题(待补充)
- 递归:解决汉诺塔问题(数据结构3.2 P103)
- 数据结构实验遇到的问题及解决(施工中)
- JavaScript 数据结构(1):栈 Stack
- 数据结构 JavaScript实现 (列表 栈)
- 从2月14号开始,上传AppStore会碰到:Failed to locate or generate matching signing assets
- 前端开发规范
- 2、spring_ioc
- 一个js实例
- CMAKE使用笔记
- javascript数据结构(栈)解决佩兹糖果盒问题
- 【腾讯内部干货分享】分析Dalvik字节码进行减包优化
- 框架 day47 BOS项目练习1(项目概述,环境,页面[easyUI,Ztree],数据库PowerDesigner)
- 知识点
- iOS学习之——UIGestureRecognizer
- javaScript学习笔记---入门
- ios应用内跳转到appstore里评分
- Spring AOP时的问题
- SMA