基础数据结构--手动实现列表及其相关方法

列表的定义:列表是一组有序的数据。其中的元素可以是任意类型。列表一般有的属性为:

  • listSize: 保存列表中元素的个数;
  • pos: 列表中当前元素的位置;
  • length(): 列表中元素的个数;
  • clear(): 清空列表;
  • toString(): 返回列表中的元素;
  • getElement(): 返回当前位置的元素;
  • insert(): 在现有元素的前后插入某个元素;
  • append(): 在列表的末尾添加元素;
  • find(): 查找列表中的指定元素;
  • remove(): 删除列表中的指定元素;
  • front(): 将当前元素移动到列表的第一个元素;
  • end(): 将当前元素移动到列表的最后一个元素;
  • pre(): 将当前元素前移一位;
  • next(): 将当前元素后移一位;
  • hasNext(): 判断后一位元素;
  • hasPre(): 判断前一位元素;
  • currPos(): 返回列表的当前位置;
  • moveTo(): 将当前元素移动到指定位置;
  • contains(): 判断给定的元素是不是包含在列表中。
    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
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    function List () {
    // 保存在列表中的数据
    this.dataStore = []
    // 列表的当前位置
    this.pos = 0
    // 列表的元素个数
    this.listSize = 0
    this.append = append
    this.
    }
    function append (item) {
    this.dataStore[this.listSize++] = item
    }
    function find (item) {
    for (let i = 0; i < this.dataStore.length;i++) {
    if (this.dataStore[i] === item) {
    return i
    }
    }
    return -1
    }
    function remove (item) {
    // 先查找到要删除元素的位置
    let index = find(item)
    // 判断是否找到了该元素
    if (index > 0) {
    // splice方法删除找到的元素
    this.dataStore.splice(index, 1)
    // 整个列表的长度减1
    --this.listSize
    return true
    }
    return false
    }
    function toString () {
    return this.dataStore
    }
    // item 要插入的元素
    // after在那个元素后插入
    function insert (item, after) {
    let index = find(after)
    if (index > 0) {
    this.dataStore.splice(index + 1, 0, item)
    ++this.listSize
    return true
    }
    return false
    }
    function clear () {
    delete this.dataStore
    this.listSize = 0
    this.pos = 0
    }
    function contains (item) {
    for (let i = 0; i < this.dataStore.length; i++) {
    if (this.dataStore[i] === item) {
    return true
    }
    }
    return false
    }
    function length () {
    return this.listSize
    }
    function front () {
    this.pos = 0
    }
    function end () {
    this.pos = this.listSize - 1
    }
    function currPos () {
    return this.pos
    }
    function moveTo (pos) {
    this.pos = pos
    }
    function getElement () {
    return this.listStore[this.pos]
    }
    function pre () {
    --this.pos
    }
    function next () {
    if (this.pos < this.listSize) {
    ++this.pos
    }
    }
    function hasNext () {
    return this.pos < this.listSize
    }
    function hasPre () {
    return this.pos >= 0
    }
    // 测试
    let list = new List()
    注:列表的运用看起来和数组区别不是很大,但是这里重要的是运用到像列表中插入元素的方法。
  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!
  • Copyrights © 2015-2022 Lee
  • 访问人数: | 浏览次数:

请我喝杯咖啡吧~

支付宝
微信