码迷,mamicode.com
首页 > 其他好文 > 详细

集合的实现

时间:2016-04-13 23:42:42      阅读:303      评论:0      收藏:0      [点我收藏+]

标签:

    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 */

 

集合的实现

标签:

原文地址:http://www.cnblogs.com/feile/p/5389059.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!