IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    数据结构与算法之字典

    冷石发表于 2022-08-29 09:01:22
    love 0

    字典是一种以键值对形式存储数据的数据结构,键用来查找,值用来保存数据

    前言

    字典是一种以键值对形式存储数据的数据结构,键用来查找,值用来保存数据

    实现

    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
    class Dictionary {
    data: any[] = []

    // 添加元素
    add(key: any, value: any) {
    this.data[key] = value
    }

    // 查找元素
    find(key: any) {
    return this.data[key]
    }

    // 删除元素
    remove(key: any) {
    delete this.data[key]
    }

    // 显示元素
    show() {
    Object.entries(this.data).forEach(item => {
    console.log('key:', item[0], ' value:', item[1])
    })
    }

    // 元素个数
    count() {
    let n = 0
    Object.keys(this.data).forEach(_ => {
    n += 1
    })
    return n
    }

    // 清空元素
    clear() {
    Object.keys(this.data).forEach((key: any) => {
    delete this.data[key]
    })
    }
    }

    测试

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    const dictionary = new Dictionary()

    dictionary.add('a', 'a')
    dictionary.add('b', 'b')
    dictionary.add('c', 'c')
    dictionary.add('d', 'd')
    dictionary.add('e', 'e')

    dictionary.show()

    console.log('count: ', dictionary.count())

    dictionary.clear()

    console.log('clear: ', dictionary.count())

    输出

    1
    2
    3
    4
    5
    6
    7
    key: a  value: a
    key: b value: b
    key: c value: c
    key: d value: d
    key: e value: e
    count: 5
    clear: 0

    排序字典的输出,需要重新定义 show 方法

    1
    2
    3
    4
    5
    6
    7
    8
    // 显示元素
    show() {
    Object.entries(this.data)
    .sort()
    .forEach(item => {
    console.log('key:', item[0], ' value:', item[1])
    })
    }

    测试一下

    1
    2
    3
    4
    5
    6
    7
    8
    9
    const dictionary = new Dictionary()

    dictionary.add('c', 'c')
    dictionary.add('a', 'a')
    dictionary.add('d', 'd')
    dictionary.add('e', 'e')
    dictionary.add('b', 'b')

    dictionary.show()

    输出

    1
    2
    3
    4
    5
    key: a  value: a
    key: b value: b
    key: c value: c
    key: d value: d
    key: e value: e

    使用 Object.keys 方法拿到字典的 keys 数组后,在用 sort 方法就可以显示有序的字典输出了



沪ICP备19023445号-2号
友情链接