1-3 链表
每个元素都带有下一个元素的位置
基础操作
-
插入元素 insert(position, el)
-
尾部添加元素 append(el)
-
获取元素索引 indexOf(el)
-
移除一项(元素) remove(el)
-
移除一项(位置)removeAt(position)
const LokedList = function () {
let head = null;
let length = 0;
let Node = function (el) {
this.element = el;
this.next = null;
};
// 尾部添加元素
this.append = function (el) {
let node = new Node(el);
if (head === null) {
head = node;
} else {
let current = head;
while (current.next) {
current = current.next;
}
current.next = node;
}
length++;
};
this.insert = function (position, el) {
let node = new Node(el);
if (position > -1 && position < length) {
let current = head;
if (position === 0) {
head = node;
node.next = current;
} else {
let index = 0;
let previous = null;
while (index < position) {
previous = current;
current = current.next;
index++;
}
previous.next = node;
node.next = current;
}
length++;
}
};
this.removeAt = function (position) {
if (position > -1 && position < length) {
let current = head;
if (position === 0) {
head = current.next;
} else {
let index = 0;
let pre = null;
while (index < position) {
pre = current;
current = current.next;
index++;
}
pre.next = current.next;
}
length--;
return current;
}
return null;
};
this.remove = function (el) {
let position = this.indexOf(el);
return this.removeAt(position);
};
this.indexOf = function (el) {
let index = 0;
let current = head;
while (current) {
if (current.element === el) {
return index;
}
index++;
current = current.next;
}
return -1;
};
this.isEmpty = function () {
return length === 0;
};
this.size = function () {
return length;
};
this.getHead = () => {
return head;
};
};
双向链表
const Node = function (el) {
this.element = el;
this.pre = null;
this.next = null;
}
1-4 集合Skipper
es6新增数据结构
Map、Set、WeakMap、WeakSet
特性与概念
-
无重复性 Set {1, 2, 3} => 实际存储格式{1: 1, 2:2, 3:3}
-
空集 {}
-
子集
操作方法
-
has(value)
-
delete(value)
-
add(value)
let Set = function () {
let items = {};
let length = 0;
this.has = function (val) {
return items.hasOwnProperty(val);
};
this.add = function (val) {
if (!thia.has(val)) {
items[val] = val;
length++;
return val;
}
return false;
};
this.remove = function (val) {
if (this.has(val)) {
delete items[val];
length--;
return true;
}
return false;
};
this.clear = function () {
items = {};
};
this.size = function () {
return length;
// return Object.keys(items).length
};
this.value = function () {
let values = [];
for (const key in items) {
if (Object.hasOwnProperty.call(items, key)) {
values.push(items[key]);
}
}
return values;
};
// 并集
this.union = function (otherSet) {
let resultSet = new Set();
let arr = this.value();
arr.forEach(item => {
resultSet.add(item);
});
arr = otherSet.value();
arr.forEach(item => {
resultSet.add(item);
});
return resultSet;
};
// 交集
this.intersection = function (otherSet) {
let resultSet = new Set();
let arr = this.value();
arr.forEach(item => {
if (otherSet.has(item)) {
resultSet.add(item);
}
});
return resultSet;
};
};
es6 的Set 和 WeakSet
WeakSet 弱集合
-
只有add、delete、has方法
-
value只能是object
区别
Set强引用,WeakSet弱引用
Set中的数据,只有调用Set.delete方法,才会被垃圾回收机制回收;
WeakSet中的数据,一旦变量被删除或者指向了null,则直接会被垃圾回收;