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));
  • 相关阅读:
    UVA 254 Towers of Hanoi
    UVA 701 The Archeologists' Dilemma
    UVA 185 Roman Numerals
    UVA 10994 Simple Addition
    UVA 10570 Meeting with Aliens
    UVA 306 Cipher
    UVA 10160 Servicing Stations
    UVA 317 Hexagon
    UVA 10123 No Tipping
    UVA 696 How Many Knights
  • 原文地址:https://www.cnblogs.com/pomelott/p/9478333.html
Copyright © 2011-2022 走看看