JS實現常見數據結構:集合

緒論

集合的特點: 元素無序,不允許重複。
集合的方法:

  1. values:獲取集合中的所有元素。
  2. size:獲取集合中元素個數。
  3. has:判斷集合中是否存在某個元素。
  4. add:添加元素。
  5. remove:移除某個元素。
  6. union:獲取兩個集合的並集。
  7. intersection:獲取兩個集合的交集。
  8. difference:獲取兩個集合的差集。
  9. subset:判斷一個集合是否爲另一個集合的子集。

正文

<!DOCTYPE html>
<div lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>index</title>
    <script src="js/jquery-2.2.4.min.js"></script>
    <style>
       
    </style>
</head>

<body>

</body>
<script>

    $(document).ready(function () {
        var set_1 = new mySet();
        set_1.add(1);
        set_1.add(2);
        set_1.add(3);
        set_1.add(2);
        set_1.add(5);

        var set_2 = new mySet();
        set_2.add(2);
        set_2.add(4);

        var set_3 = new mySet();
        set_3.add(1);
        set_3.add(3);

        console.log('set_1元素:' + set_1.values());
        console.log('set_1長度:' + set_1.size());
        console.log('set_2元素:' + set_2.values());
        console.log('set_2長度:' + set_2.size());
        console.log('set_3元素:' + set_3.values());
        console.log('set_3長度:' + set_3.size());

        console.log('set_1是否存在3:' + set_1.has(3));
        console.log('set_1是否存在4:' + set_1.has(4));
        console.log('set_1移除5:' + set_1.remove(5));
        console.log('set_1元素:' + set_1.values());
        console.log('set_1長度:' + set_1.size());

        console.log('set_1和set_2並集:' + set_1.union(set_2).values());
        console.log('set_1和set_2交集:' + set_1.intersection(set_2).values());
        console.log('set_1和set_2差集:' + set_1.difference(set_2).values());
        console.log('set_2是否爲set_1的子集:' + set_2.subset(set_1));
        console.log('set_3是否爲set_1的子集:' + set_3.subset(set_1));
    });

    function mySet() {
        var arr = [];

        // 獲取集合中的所有元素
        this.values = function() {
            return arr;
        };

        // 獲取集合中元素個數
        this.size = function() {
            return arr.length;
        };

        // 判斷集合中是否存在某個元素
        this.has = function(v) {
            return arr.indexOf(v) !== -1;
        };

        // 添加元素
        this.add = function(v) {
            if (!this.has(v)) {
                arr.push(v);
                return true;
            }
            return false;
        };

        // 移除某個元素
        this.remove = function(v) {
            if (this.has(v)) {
                var index = arr.indexOf(v);
                arr.splice(index, 1);
                return true;
            }
            return false;
        };

        // 獲取兩個集合的並集
        this.union = function(otherSet) {
            var unionSet = new mySet();

            this.values().forEach(function(v) {
                unionSet.add(v);
            });
            
            otherSet.values().forEach(function(v) {
                unionSet.add(v);
            });
            return unionSet;
        };

        // 獲取兩個集合的交集
        this.intersection = function(otherSet) {
            var intersectionSet = new mySet();
            this.values().forEach(function(v) {
                if (otherSet.has(v)) {
                    intersectionSet.add(v);
                }
            });
            return intersectionSet;
        };

        // 獲取兩個集合的差集
        this.difference = function(otherSet) {
            var differenceSet = new mySet();
            this.values().forEach(function(v) {
                if (!otherSet.has(v)) {
                    differenceSet.add(v);
                }
            });
            return differenceSet;
        };

        // 判斷一個集合是否爲另一個集合的子集
        this.subset = function(otherSet) {
            return this.values().every(function(v) {
                return otherSet.has(v);
            });
        };
    }

</script>

</div>

結果

在這裏插入圖片描述

(若有什麼錯誤,請留言指正,3Q)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章