JavaScript 数据结构

二进制转化成十进制

  function decToBinary(decNumber) {
            var stack = [],
                binary = "",
                variable;
            while (decNumber > 0) {
                variable = Math.floor(decNumber % 2)
                stack.push(variable);
                decNumber = Math.floor(decNumber / 2);
            }

            while (stack.length){
                binary += stack.pop();
            }

            return binary;
        }

任意进制的转化

function transformNumber(number , type) {
            var stack = [],
                variable,
                digits = "0123456789ABCDEF",
                handleNumber = "";

            while (number > 0) {
                variable = Math.floor(number % type);
                stack.push(variable);
                number = Math.floor(number / type);
            }

            while(stack.length != 0) {
                handleNumber += digits[stack .pop()]  
            }

            return handleNumber;
        }

5.链表
访问链表中间的一个元素需要从表头迭代到该元素。

6.集合
集合方法的一些实现方式如下:

function Set() {
            let items = {};
            //元素是否存在于集合
            this.has = function (ele) {
                // return ele in items;
                return items.hasOwnProperty(ele);
            };
            //添加元素
            this.add = function (ele) {
                if(!this.has(ele)) {
                    items[ele] = ele;
                    return true
                }
                return false;
            };
            //移除某个元素
            this.remove = function (ele) {
                if(this.has(ele)) {
                    delete items[ele];
                    return true;
                }
                return false;
            };
            //清除集合
            this.clear = function () {
                items = {};
            };
            //返回集合的大小
            this.size = function () {
               return Object.keys(items).length;
            }
            //返回集合的大小
            this.sizeLegacy = function () {
                let count = 0;
                for(let key in items) {
                    if(items.hasOwnProperty(key)) {
                        ++count;
                    }
                }
                return count;
            };
            //返回所有元素的数组集合
            this.values = function () {
                let values = [];
                for (let i = 0, key = Object.keys(items); i < key.length; i++) {
                    values.push(items[key[i]]);
                }
                return values;
            }
            //并集
            this.union = function (otherSet) {
                let unionSet = new Set();
                let values = this.values();
                for(let i = 0; i < values.length; i++) {
                    unionSet.add(values[i]);
                }
                values = otherSet.values();
                for(let i = 0; i < values.length; i++) {
                    unionSet.add(values[i]);
                }
                return unionSet;
            }
            //交集
            this.intersection = function (otherSet) {
                let intersectionSet = new Set();
                let values = this.values();
                for(let i = 0; i < values.length; i++) {
                    if(otherSet.has(values[i])) {
                        intersectionSet.add(values[i]);
                    }
                }
                return intersectionSet; 
            }
            //差集
            this.difference = function (otherSet) {
                let differenceSet = new Set();
                let values = this.values();
                for(let i = 0; i < values.length; i++) {
                    if(!otherSet.has(values[i])) {
                        differenceSet.add(values[i]);
                    }
                }
                return differenceSet;
            }
            //子集
            this.subset = function (otherSet) {
                let values = this.values();
                if(this.size < otherSet.size) {
                    return false;
                } else {
                    for (let i = 0; i < values.length; i++) {
                        if(!otherSet.has(values[i])) {
                            return false;
                        }
                    }
                }
                return true;
            }

       }

7.字典和散列表:以[键,值]储存数据

①字典和散列表略有不同,字典也称作映射。es6同样包含Map类的实现,即我们所说的字典

这里写代码片
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值