線性表的鏈式存儲

/**this

  • 鏈式存儲解決了順序存儲刪除和添加元素效率低的問題
  • 它的存儲空間不是連續的,而使用指針指向下一元素,來表示邏輯關係
  • 優勢:解決了順序表的問題
  • 缺點:爲了保存指針而花費了額外的空間 */

/**指針

  • 節點數據類型 */ class Node { constructor (data, next = null) { this.data = data this.next = next } }

/**rem

  • 單鏈表數據類型 / class Chain { /*
    • 初始化操做,建立一個帶有頭節點的空表 */ constructor () { let head = new Node() head.next = null if (head) { this.data = head } else { throw new Error('create fail') }string

      this.RANGE_ERROR = 'range_error' this.SUCCESS = 'success' this.FATAL = 'FATAL' this.FAIL = 'fail' } /**it

    • 根據傳遞的參數建立一個單鏈表 / create (eleList) { let status = this.RANGE_ERROR if (Array.isArray(eleList)) { let p,q,n,i q = new Node() // 頭節點 n = eleList.length i = n - 1 while (i >= 0) { p = new Node(eleList[i]) p.next = q.next q.next = p i-- } this.data = q status = this.SUCCESS } return status } /*io

    • 按位置取值console

    • @param {*} pos 下標 (從1開始)class

    • @param {*} ele 獲取值 / retrieve (pos, ele) { let status = this.RANGE_ERROR let p = this.data.next // 第一個節點 if (this.setPosition(pos, p) === 'success') { status = this.SUCCESS ele = p } console.log(p) return status } /*效率

    • 根據值查找位置List

    • @param {*} val

    • @param {*} pos / locate (val, pos) { let status = this.RANGE_ERROR let p = this.data.next let i = 1 while (p !== null) { if (p.data === val) break p = p.next i++ } if (p) { pos = i status = this.SUCCESS } console.log(i) return status } /*

    • 返回第pos個節點的指針

    • @param {*} pos

    • @param {*} p */ setPosition (pos, ptr = null) { let status = this.RANGE_ERROR let p = this.data

      let i = 0 while (p && i < pos) { p = p.next i++ } if (p && i === pos) { ptr = p
      status = this.SUCCESS } return { status, ptr } } /**

    • 在看指定的位置插入值

    • @param {*} pos 下標

    • @param {*} val 須要插入值 / insert (pos, val) { let s let { status, ptr:p } = this.setPosition(pos - 1) if (status === 'success') { s = new Node() if (s) { s.data = val s.next = p.next p.next = s } else { status = this.FAIL } } return status } /*

    • 移除指定pos的節點

    • @param {*} pos 下標 / remove (pos) { let { status, ptr:p } = this.setPosition(pos - 1) if (status === 'success') { let q = p.next p.next = q.next q = null } return status } /*

    • 清空鏈表,只保留頭指針 / clear () { let p = this.data, q = this.data.next while (q) { p.next = q.next q = null q = p.next } } /*

    • 銷燬實例,清空全部數據包括頭指針 / destory () { this.clear() this.data = null } empty () { return this.data.next === null } size () { let i = 0, p = this.data.next while (p) { i++ p = p.next } return length } /*

    • 求指定節點的前驅

    • @param {*} pos 下標 */ prior (pos, ele) { let {status, ptr:p } = this.setPosition(pos - 1) if (status === 'success') { ele = p } return status } next (pos, ele) { let {status, ptr:p } = this.setPosition(pos + 1) if (status === 'success') { ele = p } return status } print () { console.log(this.data) } }

let chainList = new Chain() let res = chainList.create([1,2,3,4,5,6, 'hi']) chainList.insert(1, 'ni') console.log(JSON.stringify(chainList.data)) chainList.remove(2) console.log(JSON.stringify(chainList.data))

相關文章
相關標籤/搜索