js实现数据结构及算法之字典(Dictionary)

751 阅读1分钟

字典(Dictionary)

字典是一种以 键-值对 形式存储数据的数据结构 ,就像我们的电话簿一样,要找一个电话时,首先先找到该号码的名字,名字找到了,紧接着电话号码也就有了

JavaScript 中的 Object 类就是以字典的形式设计的,我们要实现一个Dictionary类,这样会比Object方便显示字典中的所有元素,对属性进行排序等

js实现

 /**
  * 一个简单的字典
  * @constructor
  */
 function Dictionary() {
   this.dataStore = new Array();    //保存字典元素
   this.add = add;         // 添加元素
   this.find = find;       // 查找元素
   this.count = count;     // 字典中元素个数
   this.clear = clear;     // 清空字典
   this.remove = remove;   // 删除元素
   this.showAll = showAll; // 显示字典元素
 }
//添加元素
 function add(key, value) {
   this.dataStore[key] = value
 }
 //查找元素
 function find(key) {
   return this.dataStore[key]
 }
 //字典中元素个数
 function count() {
   return Object.keys(this.dataStore).length
 }
 //清空字典
 function clear() {
   var datakeys = Object.keys(this.dataStore)
   for(var keys in datakeys) {
     delete this.dataStore[datakeys[keys] ]
   }
 }
 //删除元素
 function remove(key) {
   delete this.dataStore[key]
 }
 //显示字典所有元素
 function showAll() {
   var datakeys = Object.keys(this.dataStore)
   for(var keys in datakeys) {
     console.log(datakeys[keys] + '-->' + this.dataStore[datakeys[keys]])
   }
 }
 var dictionary = new Dictionary()
 dictionary.add('小黑', '1')
 dictionary.add('小红', '2')
 dictionary.add('小白', '23')
 dictionary.add('小紫', '4')
 console.log('小黑', dictionary.find('小黑'))
 dictionary.showAll()
 dictionary.remove('小黑')
 dictionary.showAll()
 console.log('个数', dictionary.count())
 dictionary.clear()
 console.log('个数', dictionary.count())