字典(Dictionary)
字典是一种以键-值对形式存储数据的数据结构
Dictionary类
function Dictionary() {
this.dataStore = new Array();
this.add = add;
this.find = find;
this.remove = remove;
this.showAll = showAll;
this.count = count;
this.clear = clear;
function add(key, value) {
this.dataStore[key] = value;
}
function find(key) {
return this.dataStore[key];
}
function remove(key) {
delete this.dataStore[key];
}
function showAll() {
for(let key in Object.keys(this.dataStore)) {
console.log(key + "->" + this.dataStore[key]);
}
}
function count() {
let n = 0;
for(let key in Object.keys(this.dataStore)) {
n++;
}
return n;
}
function clear() {
for(let key in Object.keys(this.dataStore)) {
delete this.dataStore[key];
}
}
}
参考:数据结构与算法JavaScript描述