JavaScript資料結構--連結串列

出離心_發表於2019-04-11

連結串列是一種資料結構。

實現一個單項鍊表

// 連結串列單個節點
class Node {
  constructor (value) {
    this.value = value
    this.next = null
  }
}
複製程式碼

實現一個連結串列的

append 新增節點方法

removeAt 刪除指定位置節點

insert 指定位置插入

indexOf 查詢是否包含

remove 刪除指定節點

size 返回連結串列代下

isEmpty 判斷是否為空

初始化方法如下

class LinkedList {
  constructor (value = null) {
    this.head = null
    this.length = 0
    if (value) {
      this.head = new Node(value)
      this.length = 1
    }
  }
  append (value) {}
  removeAt (position) {}
  insert (position, value) {}
  indexOf (value, start = 0) {}
  remove (value, start = 0) {}
  size () {}
  isEmpty () {}
}
複製程式碼

完整程式碼

// 連結串列單個節點
class Node {
  constructor (value) {
    this.value = value
    this.next = null
  }
}

class LinkedList {
  constructor (value = null) {
    this.head = null
    this.length = 0
    if (value) {
      this.head = new Node(value)
      this.length = 1
    }
  }
  
  append (value) {
    const node = new Node(value)
    
    // 如果head為null 說明連結串列沒有資料,直接新增到頭部
    if (this.head === null) {
      this.head = node
    } else {
      let current = this.head
      
      // 迴圈最next為false說明next為null,則把當前節點新增到連結串列尾部
      while (current.next) {
        current = current.next
      }
      current.next = node
    }
    this.length += 1
  }
  
  removeAt (position) {
    
    // 判斷移除的節點是否在 連結串列內
    if (position >= this.length || position < 0) {
      return null
    }
    
    let current = this.head
    
    // 如果為0,則直接將 head 設定為 head.next
    if (position === 0) {
      this.head = current.next
    } else {
      let index = 0
      let prev = null
      
      // 迴圈 找到需要刪除position的節點,將其上一個節點的next 設定為當前的節點
      while (index < position) {
        prev = current
        current = current.next
        index += 1
      }
      prev.next = current.next
    }
    
    this.length -= 1
    return current.next
  }
  
  insert (position, value) {
    if (position >= this.length || position < 0) {
      return false
    }
    
    const node = new Node(value)
    
    // 節點在頭部則 直接將節點next指向this.head。然後將head的指標指向新節點
    if (position === 0) {
      node.next = this.head
      this.head = node
    } else {
      let index = 0
      let current = this.head
      let prev = null
      
      // 遍歷迴圈找節點新增進入
      while (index < position) {
        prev = current
        current = current.next
        index += 1
      }
      node.next = current
      prev.next = node
    }
    this.length += 1
    return true
  }
  
  indexOf (value, start = 0) {
    if (start > this.length) {
      return false
    }
    
    let index = 0
    let current = this.head
    
    while (index < this.length) {
      if (current.value === value && start <= index) {
        return index
      }
      current = current.next
      index += 1
    }
    return -1
  }
  
  remove (value, start = 0) {
    const index = this.indexOf(value, start)
    return this.removeAt(index)
  }
  
  size () {
    return this.length
  }
  
  isEmpty () {
    return !!this.length
  }
}

let test = new LinkedList('a')
test.append('b')
test.append('c')
test.removeAt('1')
test.insert(1, 'e')
let e = test.indexOf('e')
console.log('e', e)
console.log('test', test)

複製程式碼

相關文章