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));
  • 相关阅读:
    Informix IDS 11系统办理(918考试)认证指南,第6部分:IDS备份和恢复(1)
    Informix IDS 11体系处置(918检验)认证指南,第 4 局部: 性能调优(7)
    我常用网址整理
    System.Insert 插入字符串
    System.Length 获取字符串或数组的长度
    System.New、System.Dispose 为某个指针申请和释放内存
    System.GetMem、System.FreeMem 申请和释放内存
    学习 TList 类的实现[1]
    学习 TList 类的实现[2]
    System.ReallocMem 重新申请内存
  • 原文地址:https://www.cnblogs.com/pomelott/p/9478333.html
Copyright © 2011-2022 走看看