数据结构 –集合 字典:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//封装集合类
function Set() {
//属性
this.items = {}
//方法
//add
Set.prototype.add = function (value) {
//要判断当前集合中是否已经包含了要加入的元素
if (this.has(value)) {
return false
}
//将元素添加到集合中
this.items[value] = value
return true
}
//has方法
Set.prototype.has = function (value) {
return this.items.hasOwnProperty(value)
}
//remove方法
Set.prototype.remove = function (value) {
//1.判断该集合中是否包含该元素
if (!this.has(value)) {
return false
}
//2.将元素从属性中删除
delete this.items[value]
return true
}
//clear方法
Set.prototype.clear = function () {
this.items = {}
}
//size方法
Set.prototype.size = function () {
return Object.keys(this.items).length
}
//获取集合中所有的值
Set.prototype.values = function () {
return Object.keys(this.items)
}

//集合间的操作
// 并集
Set.prototype.union = function (otherSet) {
//this:集合对象A
//otherSet 集合对象B
//创建新集合
let unionSet = new Set()
//将A集合中的元素放入新的集合
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
}

//交集
Set.prototype.intersection = function (otherSet) {
//this:集合A
//otherSet:集合B
//1.创建一个新的集合
let intersectionSet = new Set()
//2.从A中取出一个元素,判断是否同时存在于集合B 存在的话放到新的集合里
let values = this.values()
for (let i = 0; i < values.length; i++) {
let item = values[i]
if (otherSet.has(item)) {
intersectionSet.add(item)
}
}
return intersectionSet
}
//差集
Set.prototype.difference = function (otherSet) {
//this.集合A
//otherSet 集合B
//1.创建新的集合
let differenceSet=new Set()
//取出A集合的元素 判断是否存在于B中,不存在于B中就添加到新的集合中
let values=this.values()
for (let i=0;i< values.length;i++){
let item=values[i]
if (!otherSet.has(item)){
differenceSet.add(item)
}
}
return differenceSet
}
//子集
Set.prototype.subset = function (otherSet) {
//this 集合A
//other集合B
//遍历集合A中元素 如果集合A中的元素 在B中不存在 那么就是false
//如果遍历完了整个集合,没有返回false的话就 直接返回true
let values=this.values()
for (let i=0;i< values.length;i++){
let item= values[i]
if(!otherSet.has(item)){
return false
}
}
return true
}
}

字典

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// 创建字典的构造函数
function Dictionay() {
// 字典属性
this.items = {}

// 字典操作方法
}
// 创建字典的构造函数
function Dictionay() {
// 字典属性
this.items = {}

// 字典操作方法
// 在字典中添加键值对
Dictionay.prototype.set = function (key, value) {
this.items[key] = value
}

// 判断字典中是否有某个key
Dictionay.prototype.has = function (key) {
return this.items.hasOwnProperty(key)
}

// 从字典中移除元素
Dictionay.prototype.remove = function (key) {
// 1.判断字典中是否有这个key
if (!this.has(key)) return false

// 2.从字典中删除key
delete this.items[key]
return true
}

// 根据key去获取value
Dictionay.prototype.get = function (key) {
return this.has(key) ? this.items[key] : undefined
}

// 获取所有的keys
Dictionay.prototype.keys = function () {
return Object.keys(this.items)
}

// 获取所有的value
Dictionay.prototype.values = function () {
return Object.values(this.items)
}

// size方法
Dictionay.prototype.size = function () {
return this.keys().length
}

// clear方法
Dictionay.prototype.clear = function () {
this.items = {}
}
}