JavaScript数据结构与算法---字典

130 阅读3分钟

字典是一种以键 - 值对形式存储数据的数据结构,就像电话号码簿里的名字和电话号码一样。要找一个电话时,先找名字,名字找到了,紧挨着它的电话号码也就找到了。这里的键是指你用来查找的东西,值是查找得到的结果。

Dictionary类

Dictionary类的基础是Array类,而不是Object类。

定义Dictionary类

function Dictionary() {
    this.dataStore = new Array();
}

定义add方法

该方法接受两个参数:键和值,键是值在字典中的索引。

function add(key, value) {
    this.dataStore[key] = value;
}

定义find方法

该方法以键为参数,返回对应的值。

function find(key) {
    return this.dataStore[key];
}

删除方法

从字典中删除键-值,需要使用javaScript内置方法delete。

function remove(key) {
    delete this.dataStore[key];
}

显示字典中所有的键-值对

function showAll() {
    for(var key of Object.keys(this.dataStore)) {
        console.log(key + "->" + this.dataStore[key]);
    }
}

代码示例

function Dictionary() {
  this.add = add;
  this.datastore = new Array();
  this.find = find;
  this.remove = remove;
  this.showAll = showAll;
}
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 (var key of Object.keys(this.datastore)) {
    console.log(key + " -> " + this.datastore[key]);
  }
}

var pBook = new Dictionary();
pBook.add("Mike", 123);
pBook.add("Alice", 231);
console.log(pBook.find("Mike"));
pBook.showAll();
pBook.remove("Mike");
pBook.showAll();
输出为:
123
Mike -> 123
Alice -> 231
Alice -> 231

Dictionary类的辅助方法

定义字典中的元素个数count方法

function count() {
    let n = 0;
    for(let key of Object.keys(this.datastore)) {
        ++n;
    }
    return n;
}

这里不能使用数组的length方法,因为当键的类型为字符串时,length属性就不管用了。

为Dictionary类添加排序功能

字典的主要用途是通过键取值,我们无须太关心数据在字典中的实际存储顺序,但是很多人都希望看到一个有序的字典,因此我们需要实现一个有序的字典。 重新定义showAll()方法

function showAll() {
  for (var key of Object.keys(this.datastore).sort()) {
    console.log(key + " -> " + this.datastore[key]);
  }
}

该定义和之前的定义唯一的区别是:从数组 datastore 拿到键后,调用 sort() 方法对键重新排了序。

练习题

  1. 写一个程序,该程序从一个文本文件中读入名字和电话号码,然后将其存入一个字典。该程序需包含如下功能:显示单个电话号码、显示所有电话号码、增加新电话号码、删除电话号码、清空所有电话号码。
/**
 * 假设已经读入了数据,数据内容为data
 */
const data = [['Alice',12243341221],['David',12243341222],['Mike',12243341223],['Bob',12243341224]];
function Dictionary() {
    this.dataStore = new Array();
    this.add = add;
    this.showAll = showAll;
    this.find = find;
    this.remove = remove;
    this.clear = clear;
}
// 增加新电话号码
function add(key,value) {
    this.dataStore[key] = value;
}
// 查找单个电话号码
function find(key) {
    console.log(key + '->' + this.dataStore[key])
}
// 显示所有电话号码
function showAll() {
    for(let key of Object.keys(this.dataStore)) {
        console.log(key + '->' + this.dataStore[key])
    }
}
// 删除电话号码
function remove(key) {
    delete this.dataStore[key];
}
// 清空所有电话号码
function clear() {
    for(let key of Object.keys(this.dataStore)) {
        this.remove(key)
    }
}
const messages = new Dictionary();
for (let item of data) {
    messages.add(item[0],item[1]);
}
messages.find('Mike');
messages.showAll();
messages.remove('Mike');
messages.showAll();
messages.clear();
messages.showAll();
  1. 使用Dictionary类写一个程序,该程序用来存储一段文本中各个单词出现的次数。该程序显示每个单词出现的次数,但每个单词只显示一次。比如下面这段话“the brown fox jumped over the blue fox”,程序的输出应为: the: 2 brown: 1 fox: 2 jumped: 1 over: 1 blue: 1
const string = "the brown fox jumped over the blue fox";
function Dictionary() {
    this.dataStore = new Array();
    this.add = add;
    this.showAll = showAll;
    this.find = find;

}
// 增加新电话号码
function add(key) {
    if(this.find(key)) {
        this.dataStore[key] += 1;
    } else {
        this.dataStore[key] = 1;
    }
}
// 查找单个电话号码
function find(key) {
    return !!this.dataStore[key];
}
// 显示所有电话号码
function showAll() {
    for(let key of Object.keys(this.dataStore)) {
        console.log(key + ': ' + this.dataStore[key])
    }
}
const messages = new Dictionary();
const arr = string.split(" ");
for (let item of arr) {
    messages.add(item);
}
messages.showAll();
  1. 修改练习2,使单词按字母顺序显示。
const string = "the brown fox jumped over the blue fox";
function Dictionary() {
    this.dataStore = new Array();
    this.add = add;
    this.showAll = showAll;
    this.find = find;

}
// 增加新电话号码
function add(key) {
    if(this.find(key)) {
        this.dataStore[key] += 1;
    } else {
        this.dataStore[key] = 1;
    }
}
// 查找单个电话号码
function find(key) {
    return !!this.dataStore[key];
}
// 显示所有电话号码
function showAll() {
    for(let key of Object.keys(this.dataStore).sort()) {
        console.log(key + ': ' + this.dataStore[key])
    }
}
const messages = new Dictionary();
const arr = string.split(" ");
for (let item of arr) {
    messages.add(item);
}
messages.showAll();