• js数据结构之集合的详细实现方法


    数据结构中的集合,类似于数学中常说的集合,是一类数据的群组。集合与集合之间还存在交集,并集,补集的运算。

    ***集合为无序,集合内元素不重复
    ***js的set基于数组, 使用SetClass为类名,区别于ES6
     
     
    集合的实现方法如下:
    function SetClass () {
        this.dataList = [];
    
        this.add = function (data) {
            if (this.dataList.indexOf(data)<0) {
                this.dataList.push(data);
                return true;
            }
            return false;
        };
    
        this.remove = function (data) {
            var index = this.dataList.indexOf(data);
            if (index > -1) {
                this.dataList.splice(index, 1);
                return true;
            }
            return false;
        };
    
        this.size = function () {
            return this.dataList.length;
        };
    
        this.show = function () {
            return this.dataList;
        };
    
        this.contains = function (data) {
            if (this.dataList.indexOf(data)!=-1) {
                return true;
            }
            return false;
        };
        // 并集
        this.union = function (targetSet) {
            var tempSet = new SetClass();
            targetSet.show().forEach(function (item, i) {
                tempSet.add(item);
            });
    
            this.dataList.forEach(function (item, i) {
                if (!tempSet.contains(item)) {
                    tempSet.add(item);
                }
            });
            return tempSet;
        };
        // 交集
        this.intersect = function (targetSet) {
            var tempSet = new SetClass();
            this.dataList.forEach(function (item, i) {
                if (targetSet.contains(item)) {
                    tempSet.add(item);
                }
            });
            return tempSet;
        };
        // 判断当前集合是否是目标集合的子集
        this.subset = function (targetSet) {
            if (this.size() > targetSet.size()) return false;
            this.dataList.forEach(function (item, i) {
                if (!targetSet.contains(item)) {
                    return false;
                }
            }); 
            return true;
        };
        // 补集:targetSet中不存在,this.dataList中存在
        this.difference = function (targetSet) {
            var tempSet = new SetClass();
            this.dataList.forEach(function (item, i) {
                if (!targetSet.contains(item)) {
                    tempSet.add(item);
                }
            });
            return tempSet;
        };
    }

    集合的使用方法如下:

    var testSet = new SetClass();
    var testSet2 = new SetClass();
    var testSet3 = new SetClass();
    testSet.add("a");
    testSet.add("b");
    testSet.add("c");
    testSet.add("d");
    testSet.add("e");
    testSet2.add("d");
    testSet2.add("e");
    testSet2.add("f");
    testSet2.add("g");
    testSet2.add("h");
    testSet3.add("a");
    testSet3.add("b");
    
    console.log(testSet.difference(testSet3));
  • 相关阅读:
    (转)非常好的协同过滤入门文章
    PHP错误:Call to undefined function curl_init()
    (转)Ubuntu下Apache、php、mysql默认安装路径
    (转)matlab各类数学公式
    centos7下安装momgodb3
    centos7 使用mount 挂载window10 (超简单)
    Centos7 安装nginx1.14
    Centos7 安装MySQL5.7
    python3.5 安装 numpy1.14.4
    python3 tkinter 桌面软件教程
  • 原文地址:https://www.cnblogs.com/pomelott/p/9478333.html
Copyright © 2020-2023  润新知