JavaScript數據結構--鏈表

鏈表是一種數據結構。javascript

實現一個單項鍊表

// 鏈表單個節點
class Node {
  constructor (value) {
    this.value = value
    this.next = null
  }
}
複製代碼

實現一個鏈表的java

append 添加節點方法node

removeAt 刪除指定位置節點數據結構

insert 指定位置插入app

indexOf 查找是否包含ui

remove 刪除指定節點this

size 返回鏈表代下spa

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)

複製代碼
相關文章
相關標籤/搜索