總括: 本文講解了數據結構中的[集合]概念,並使用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數據結構(四)——散列表]