二进制转化成十进制
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类的实现,即我们所说的字典
这里写代码片