JavaScript Algorithms and Data Structures - Detailed Dictionary

A dictionary is a data structure that stores data in the form of key-value pairs. Next we will implement the dictionary data structure using JavaScript.

1. Define the dictionary class

Since the data structure of the comparison dictionary is relatively simple, it is good to go directly to the code.

class Dictionary {
  constructor () {
    this.data = []
  }
}
2、add()

This method is used to add elements to the dictionary and needs to accept two parameters, key and value

  add (key, value) {
    this.data[key] = value
  }
3、find()

This method is used to return the corresponding value by looking up the key value

  find (key) {
    return this.data[key]
  }
4、remove()

This method is used to delete key-value pairs in a dictionary (with the help of JavaScript's delete() function)

  remove (key) {
    delete this.data[key]
  }
5、showAll()

This method is used to display all key-value pairs

  showAll () {
    for(let key in this.data) {
      console.log(key + '->' + this.data[key])
    }
  }
6、count()

This method is used to return the number of elements in the dictionary. Length cannot be used in the method, because if the key is a string, the length attribute is invalid, for example let arr = []; arr['one'] = 1; arr['two'] = 2;console.log(arr.length); the output is 0.

  count () {
    let n = 0
    for (let key in this.data) {
      n++
    }
    console.log(n)
  }
7、pSort()

This method is used to sort the dictionary, which can be achieved by sorting the keys of the dictionary

  pSort () {
    const keySort = Object.keys(this.data).sort()
    for (let i = 0; i < keySort.length; i++) {
      console.log(keySort[i] + '->' + this.data[keySort[i]])
    }
  }
8、clear()

This method is used to empty the dictionary

  clear () {
    for (let key in this.data) {
      delete this.data[key]
    }
  }
9. Test example
let dic = new Dictionary()
dic.add('Candy', 999)
dic.add('Allen', 666)
dic.add('Scott', 777)
dic.add('Tom', 555)
dic.add('Jack', 333)
console.log('字典中的元素有:')
dic.showAll()
console.log('字典中Tom的值为:')
dic.find('Tom')
console.log('字典中的元素个数为:')
dic.count()
console.log('移除字典中key为Jack的元素后的剩余的元素:')
dic.remove('Jack')
dic.showAll()
console.log('按key排序:')
dic.pSort()
console.log('开始清除所有元素')
dic.clear()
console.log('清除后字典中元素个数为')
dic.count()
10. Running results

Use the node environment to run this js file, node dictionary.js, the visible output is as follows:
write picture description here

11. Complete source code
class Dictionary {
  constructor () {
    this.data = []
  }
  add (key, value) {
    this.data[key] = value
  }
  find (key) {
    return this.data[key]
  }
  remove (key) {
    delete this.data[key]
  }
  showAll () {
    for(let key in this.data) {
      console.log(key + '->' + this.data[key])
    }
  }
  count () {
    let n = 0
    for (let key in this.data) {
      n++
    }
    console.log(n)
  }
  pSort () {
    const keySort = Object.keys(this.data).sort()
    for (let i = 0; i < keySort.length; i++) {
      console.log(keySort[i] + '->' + this.data[keySort[i]])
    }
  }
  clear () {
    for (let key in this.data) {
      delete this.data[key]
    }
  }
}
let dic = new Dictionary()
dic.add('Candy', 999)
dic.add('Allen', 666)
dic.add('Scott', 777)
dic.add('Tom', 555)
dic.add('Jack', 333)
console.log('字典中的元素有:')
dic.showAll()
console.log('字典中Tom的值为:')
dic.find('Tom')
console.log('字典中的元素个数为:')
dic.count()
console.log('移除字典中key为Jack的元素后的剩余的元素:')
dic.remove('Jack')
dic.showAll()
console.log('按key排序:')
dic.pSort()
console.log('开始清除所有元素')
dic.clear()
console.log('清除后字典中元素个数为')
dic.count()

"JavaScript Algorithms and Data Structures - Dictionary Implementation" is over, please point out any mistakes, happy May Day~

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325399172&siteId=291194637