zoukankan      html  css  js  c++  java
  • 栈的实现

     //栈的实现
        function Stack() {
            this.dataStore = [];//初始化数组
            this.top = 0;//记录栈顶位置
            this.push = push;//入栈
            this.pop = pop;//出栈
            this.peek = peek;//栈顶元素
            this.length = length;//栈内元素
            this.clear = clear;//清空
        }
     
        //入栈
        function push(element) {
            this.dataStore[this.top++] = element;
        }
     
        //出栈
        function pop() {
            return this.dataStore[--this.top];
        }
     
        //栈顶元素
        function peek() {
            return this.dataStore[this.top - 1];
        }
     
        //栈内元素大小
        function length() {
            return this.top;
        }
        
        //清空
        function clear() {
            delete this.dataStore;
            this.dataStore = [];
            this.top = 0;
        }
        var s = new Stack();
        s.push("a");
        s.push("b");
        s.push('c');
        s.push('d');


    /// <summary>
    /// 数组去重
    /// </summary>
    /// <param name="fieldName">去重的字段名</param> 
    Array.prototype.distinct = function (fieldName) {
        var arr = this;
        var uniqueArr = [];
        var includedKey = {};
        for (var i = 0; i < arr.length; i++) {
            var value = arr[i][fieldName];
            if (includedKey[value]) continue;
            uniqueArr.push(arr[i]);
            includedKey[value] = 1;
        }
        return uniqueArr;
    }
    // 测试
    var test = [{ id: '1' }, { id: '2' }, { id: '2' }, { id: '1' }, { id: '3' }];
    test = test.distinct('id');
    alert(JSON.stringify(test));
    好好学习,天天向上。
  • 相关阅读:
    关于codeblocks插件(持续更新)
    自定义gvim配色方案qiucz.vim的源码
    mark it
    poj 1032
    poj 1028
    最小公倍数是lcm
    problems
    hdu 1067
    某些题的做法。。。
    突然明白了什么
  • 原文地址:https://www.cnblogs.com/Zhengxue/p/6141400.html
Copyright © 2011-2022 走看看