总括: 本文讲解了数据结构中的[集合]概念,并使用javascript实现了集合。javascript
人生多风雨,何处无险阻。前端
在上一篇学习javascript数据结构(二)——链表中咱们说了链表这种数据结构,但归根结底,不管是栈,队列亦或是链表都是线性结构。他们都是一种很规矩的数据结构,就像幼儿园的小朋友排队乖乖的站在那不会动同样。java
然而纷杂的数据并不会老是排队站在那里,幼儿园小朋友一旦下了课那可就撒欢了,乱糟糟一团。可咱们的幼儿园老师却能分辨出这些小朋友,由于啥?由于每一个小朋友都在一个班里,并且每个小朋友都有本身的名字。老师天然很容易就找到小朋友了。git
而本篇博文要说的集合正是一堆乱糟糟的数据
,惟一的共同点是这些数据隶属于同一个集合
,看下百科给出的解释:github
由一个或多个元素所构成的叫作集合。api
此处的元素就是小朋友了,他们所在的集合就是他们的班级。其实咱们在高中的时候也接触过集合的概念。那时候尚未套路这个名词,单纯的岁月,那个年代对于集合是这么解释的:数组
集合是指具备某种特定性质的具体的或抽象的对象汇总成的集体,这些对象称为该集合的元素。数据结构
而后又是这么分类的:学习
不过,数据结构中集合是没有无限集这个概念的。再而后那时候的集合还有这么几个特性:ui
想当年哥仍是个数学学霸,现在却沦落为了一个码农......真是让人唏嘘啊。咳咳!接着说:
集合还有这几种常见的基本操做:
并且咱们数据结构中的集合基本是也符合高中时候的数学中的概念的。接下来咱们是用ES5来实现集合,为啥子这么说呢......由于在ES6中已经新给出了Set,Map等几个集合类,更加方便快捷的锁定键值对。
首先咱们先声明一个集合类:
function(){
var items={};
}复制代码
接下来,咱们须要给链表声明一些方法:
下面是Set类的完整代码:
function Set() {
let items = {};
this.add = function(value){
if (!this.has(value)){
items[value] = value;
return true;
}
return false;
};
this.delete = function(value){
if (this.has(value)){
delete items[value];
return true;
}
return false;
};
this.has = function(value){
return items.hasOwnProperty(value);
//return value in items;
};
this.clear = function(){
items = {};
};
/** * Modern browsers function * IE9+, FF4+, Chrome5+, Opera12+, Safari5+ * @returns {Number} */
this.size = function(){
return Object.keys(items).length;
};
/** * cross browser compatibility - legacy browsers * for modern browsers use size function * @returns {number} */
this.sizeLegacy = function(){
let count = 0;
for(let key in items) {
if(items.hasOwnProperty(key))
++count;
}
return count;
};
/** * Modern browsers function * IE9+, FF4+, Chrome5+, Opera12+, Safari5+ * @returns {Array} */
this.values = function(){
let values = [];
for (let i=0, keys=Object.keys(items); i<keys.length; i++) {
values.push(items[keys[i]]);
}
return values;
};
this.valuesLegacy = function(){
let values = [];
for(let key in items) {
if(items.hasOwnProperty(key)) {
values.push(items[key]);
}
}
return values;
};
this.getItems = function(){
return items;
};
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){
if (this.size() > otherSet.size()){
return false;
} else {
let values = this.values();
for (let i=0; i<values.length; i++){
if (!otherSet.has(values[i])){
return false;
}
}
return true;
}
};
}复制代码
下面是ES6版本代码:
let Set2 = (function () {
const items = new WeakMap();
class Set2 {
constructor () {
items.set(this, {});
}
add(value){
if (!this.has(value)){
let items_ = items.get(this);
items_[value] = value;
return true;
}
return false;
}
delete(value){
if (this.has(value)){
let items_ = items.get(this);
delete items_[value];
return true;
}
return false;
}
has(value){
let items_ = items.get(this);
return items_.hasOwnProperty(value);
}
clear(){
items.set(this, {});
}
size(){
let items_ = items.get(this);
return Object.keys(items_).length;
}
values(){
let values = [];
let items_ = items.get(this);
for (let i=0, keys=Object.keys(items_); i<keys.length; i++) {
values.push(items_[keys[i]]);
}
return values;
}
getItems(){
return items.get(this);
}
union(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;
}
intersection(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;
}
difference(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;
};
subset(otherSet){
if (this.size() > otherSet.size()){
return false;
} else {
let values = this.values();
for (let i=0; i<values.length; i++){
if (!otherSet.has(values[i])){
return false;
}
}
return true;
}
};
}
return Set2;
})();复制代码
集合是一种比较常见的数据结构,在JS中咱们已经有了一种相似哈希表的东西:Object(对象)。但如今咱们所说的集合只是以[value,value]形式存储数据,下一节咱们使用[键,值]形式存储数据,和本文集合的实现略有不一样。敬请期待:[学习javascript数据结构(四)——散列表]