javascript - 一道 跟谁学 的笔试题,求解决。。。。
怪我咯
怪我咯 2017-04-10 16:01:50
[JavaScript讨论组]

请实现一个Stack(pop,push,size,clear);

怪我咯
怪我咯

走同样的路,发现不同的人生

全部回复(5)
迷茫

JS 数组就可以作为一个 Stack,封装一下就可以。

function Stack(){
    this._data = [];
}
Stack.prototype={
    constructor:Stack,
    push:function (a){
        Array.prototype.push.apply(this._data,arguments);
    },
    pop:function (){
        Array.prototype.pop.apply(this._data,arguments);
    },
    size:function(){ return this._data.length;},
    clear:function(){ this._data.length=0;}
}
大家讲道理
var stack = {
    dataArr: [],
    pop: function() {
        var length = this.dataArr.length;
        if(length <= 0) {
            console.log('no data');
            return false;
        } else {
            var lastData = this.dataArr[length];
            this.dataArr.length = this.dataArr.length - 1;
            return lastData;
        }
    },
    push: function(data) {
        this.dataArr[this.dataArr.length+1] = data;
        return this.dataArr.length;
    },
    size: function() {
        return this.dataArr.length;
    },
    clear: function(){
        this.dataArr = [];
    }
}
伊谢尔伦

//Stack类

function stack(){

this.dataStore = [];/*栈模拟数组*/
this.top = 0; /*记录当前栈顶的位置*/
this.push = push;
this.pop = pop;
this.clear = clear;
this.size = size;
}

function push(element){
   this.dataStore[this.top++] = element;
 }
   
function pop(element){
   return this.dataStore[--this.top];
 }
  
function clear(){
  this.top = 0 ;
 }
  
function size(){
  return this.top;
 } 
     
      
  
伊谢尔伦

学生时代的题

黄舟

一个list或者数据就可以了。
ps:跟谁学是我的前个boss创建的,里面有不少我的老同事。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号