zoukankan      html  css  js  c++  java
  • 集合的实现

        function Set() {
            this.dataStore = [];
            this.add = add;
            this.remove = remove;
            this.show = show;
    
            this.contains = contains;
            this.union = union;
            this.intersect = intersect;
    
            this.size = size;
            this.subset = subset;
    
            this.difference = difference;
        }
        function add(data) {
            if (this.dataStore.indexOf(data) < 0) {
                this.dataStore.push(data);
                return true;
            } else {
                return false;
            }
        }
        function remove(data) {
            var pos = this.dataStore.indexOf(data);
            if (pos > -1) {
                this.dataStore.splice(pos, 1);
                return true;
            } else {
                return false;
            }
        }
        function show() {
            return this.dataStore;
        }
        var names = new Set();
        names.add("David");
        names.add("Jennifer");
        names.add("Mike");
        names.add("Raymond");
        if (names.add("Mike")) {
            document.write("Mike added" + "<br />")
        } else {
            document.write("Can't add Mike, must already be in set" + "<br />");
        }
        document.write(names.show() + "<br />");
        var removed = "Mike";
        if (names.remove(removed)) {
            document.write(removed + " removed." + "<br />");
        } else {
            document.write(removed + " not removed." + "<br />");
        }
        names.add("Clayton");
        document.write(names.show() + "<br />");
        document.write("****************" + "<br />");
    
        function contains(data) {
            if (this.dataStore.indexOf(data) > -1) {
                return true;
            } else {
                return false;
            }
        }
        function union(set) {//并操作
            var tempSet = new Set();
            for ( var i = 0; i < this.dataStore.length; ++i) {//this指的是cis
                tempSet.add(this.dataStore[i]);//将cis集合添加到tempSet临时集合
            }
            for ( var i = 0; i < set.dataStore.length; ++i) {
                if (!tempSet.contains(set.dataStore[i])) {//判断临时集合中是否包含dmp集合中的元素
                    tempSet.dataStore.push(set.dataStore[i]);
                    //tempSet.add(set.dataStore[i]);
                }
            }
            return tempSet;
        }
    
        var cis = new Set();
        cis.add("Mike");
        cis.add("Clayton");
        cis.add("Raymond");
        var dmp = new Set();
        dmp.add("Raymond");
        dmp.add("Clayton");
        var it = new Set();
        it = cis.union(dmp);
        document.write(it.show() + "<br />");
        document.write("****************" + "<br />");
    
        function intersect(set) {//交操作
            var tempSet = new Set();
            for ( var i = 0; i < this.dataStore.length; ++i) {
                if (set.contains(this.dataStore[i])) {
                    tempSet.add(this.dataStore[i]);
                }
            }
            return tempSet;
        }
        it = cis.intersect(dmp);
        document.write(it.show() + "<br />");
        document.write("****************" + "<br />");
    
        function size() {
            return this.dataStore.length;
        }
        function subset(set) {
            if (this.size() > set.size()) {
                return false;
            } else {
                for ( var member in this.dataStore) {
                    if (!set.contains(this.dataStore[member])) {
                        return false;
                    }
                }
            }
            return true;
        }
        if (it.subset(dmp)) {//判断it集合是否包含于dmp集合
            document.write("IT is a subset of DMP.");
        } else {
            document.write("IT is not a subset of DMP.");
        }
        document.write("<br />");
        document.write("****************" + "<br />");
    
        function difference(set) {//返回一个属于第一个集合但不属于第二个集合的新集合 
            var tempSet = new Set();
            for ( var i in this.dataStore) {
                if (!set.contains(this.dataStore[i])) {
                    tempSet.add(this.dataStore[i]);
                }
            }
            return tempSet;
        }
        it = cis.difference(dmp);
        document.write(it.show());
        /*上述程序运行的结果如下:
        Can't add Mike, must already be in set
        David,Jennifer,Mike,Raymond
        Mike removed.
        David,Jennifer,Raymond,Clayton
         ****************
        Mike,Clayton,Raymond
         ****************
        Clayton,Raymond
         ****************
        IT is a subset of DMP.
         ****************
        Mike */
  • 相关阅读:
    【LintCode题集】Q539
    【LintCode题解】Q407
    【LintCode题集】Q6、Q64
    【Java安全】关于Java中常用加密/解密方法的实现
    【MySQL】MySQL5.7的安装与配置
    理解CSS3 max/min-content及fit-content等width值
    Django和MySQL数据库第一次连接时遇到的若干问题及解决办法
    使用Pycharm社区版启动Django的重要补充
    使用Pycharm社区版新建Python3.7的虚拟环境并安装启动Django的完整步骤
    04-图形化编辑器功能不全?
  • 原文地址:https://www.cnblogs.com/feile/p/5389059.html
Copyright © 2011-2022 走看看