zoukankan      html  css  js  c++  java
  • 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));
  • 相关阅读:
    基于讯飞语音API应用开发之——离线词典构建
    在Linux下安装redis
    java学习路线
    怎么做压力测试
    性能测试概念及参数介绍
    总结1-JMeter压力测试
    在软件测试面试过程中如何进行自我介绍?
    Python模块常用的几种安装方式
    Jmeter(三)关联数组
    Jmeter(二)关联
  • 原文地址:https://www.cnblogs.com/pomelott/p/9478333.html
Copyright © 2011-2022 走看看