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));
  • 相关阅读:
    Flutter图片选择 image_picker(官方)插件使用详解
    androidstudo如何跨越这个厚厚的墙,亲测有效 Could not resolve com.android.tools.build:gradle:
    qwq。。胡诌qwq
    关于很狗的军训qwq
    Leetcode每日一题 503.下一个更大元素II
    C++ 关于volatlie
    C++虚成员函数与动态联编
    graphics pipeline
    pointer or function
    线段树
  • 原文地址:https://www.cnblogs.com/pomelott/p/9478333.html
Copyright © 2011-2022 走看看