雙向鏈表的常用操作(非常詳細(xì))

1.雙向鏈表
數(shù)據(jù)結(jié)構(gòu)中常見的操作如下:
// 1.append(element)
// 2.inset(position,element)
// 3.get(position)
// 4.indexOf(element)
// 5.update(position)
// 6.removeAt(position)
// 7.remove(element)
// 8.isEmpty()
// 9.size()
// 10.toString()
// 11.forwardString()
// 12.backwordString()

① 首先應(yīng)該建立節(jié)點(diǎn)

class Node{
  constructor(data){
    this.data = data;
    this.next = null;
    this.prev = null;
  }
}

② 然后建立鏈表函數(shù)

class doubleLinkList {
  constructor() {
    this.head = null;
    this.tail = null;
    this.length = 0;
// 插入各種具體的方法...........................................
}

③ 插入的12個方法如下:
1.append(element) (尾插法)

append(data){
    var newNode = new Node(data);
    // 判斷是否是第一個節(jié)點(diǎn)
    if (this.length===0){
      this.head = newNode;
      this.tail = newNode;
    }else {
      var current = this.head;
      // 尋找最后的節(jié)點(diǎn)
      while (current.next){
        current = current.next
      }
      current.next = newNode;
      newNode.prev = current;
      this.tail = newNode;
    }
    // 最后不要忘記長度加一
    this.length += 1;
  }

2.toString 將鏈表轉(zhuǎn)化成字符串方法
3.forwardString():返回正向遍歷的節(jié)點(diǎn)字符串形式
4.backwordString():返回反向遍歷的節(jié)點(diǎn)字符串形式


// 2.toString 將鏈表轉(zhuǎn)化成字符串方法
  toString(){
    return this.backwordString()
  }

// 3.forwardString():返回正向遍歷的節(jié)點(diǎn)字符串形式
  forwardString(){
    // 從后往前遍歷
    var current = this.tail;
    var resultString = "";
    while (current){
      resultString += current.data + "";
      current = current.prev;
    }
    return resultString;

  }

// 4.backwordString():返回反向遍歷的節(jié)點(diǎn)字符串形式
  backwordString(){
    // 1.定義遍歷
    var current = this.head;
    var resultString = "";
    // 2.依次向后遍歷,獲得每一個點(diǎn)
    while (current){
      resultString += current.data + "";
      current = current.next;
    }
    return resultString;
  }

5.insert任意位置插入
分為三種情況:
1.開頭插入
2.尾插入
3.中間插入

insert(data,position){
    if (position <0 || position > this.length)return false
    var newNode = new Node(data)
// 1.原來的列表是否為空
    if (this.length == 0){
      this.head = newNode;
      this.tail = newNode;
    }else {
// 2.當(dāng)在第一個位置
      if (position === 0){
        this.head.prev = newNode;
        newNode.next = this.head;
        this.head = newNode;
// 3. 當(dāng)在最后的位置
      }else if (position == this.length){
        newNode.prev = this.tail;
        this.tail.next = newNode;
        this.tail = newNode;
// 4. 當(dāng)在中間的位置
      }else {
        var current = this.head;
        var index = 0;
        while (index++ < position){
          current = current.next
        }
// 5. 修改指針
        newNode.next = current;
        newNode.prev = current.prev;
        current.prev.next = newNode;
        current.prev = newNode;
      }
    }
    this.length += 1;
    return true;
  }

6. get 方法

get(position){
    if (position < 0 ||position >= this.length)return null
    if (this.length / 2 > position){
      var current = this.head;
      var index = 0;
      while (index++ < position){
        current = current.next;
      }
      return current.data;

    }else {
      var current = this.tail;
      var index = this.length - 1;
      while (index-- > position){
        current = current.prev;
      }
      return current.data;
    }
  }

7.indexOf() 正反方向一起查,加快速度

indexOf(data){
    var current1 = this.head;
    var current2 = this.tail;
    var index = 0;
    var index2 = this.length;
    while (current1 && current2){
      if (current1.data == data){
        return index
      }
      if (current2.data == data){
        return index2
      }
      current1 = current1.next;
      current2 = current2.prev;
      index += 1;
      index2 -= 1;
    }
    return false;
  }

8.update方法 , 也可以分情況前后查找

  update(newdata,position){
    if (position < 0 || position >= this.length){return false}
    var current = this.head
    var index = 0
    while (index++ < position){
      current = current.next
    }
    current.data = newdata;
    return true;
  }

9.removeAt方法

  removeAt(position){
    // 1.越界判斷
    if (position < 0 || position > this.length) return false
    var current = this.head;
    if (this.length == 1){
      this.head = null;
      this.tail = null;
    }else {
      if (position == 0){
        this.head.next.prev = null;
        this.head = this.head.next;
      }else if (position == this.length){
        this.tail.prev.next = null;
        this.tail = this.tail.prev;
      }else {
        var index = 0;
        while (index++ < position){
          current = current.next;
        }
        current.prev.next =  current.next;
        current.prev.next.prev = current.prev;
      }
    }
    this.length -= 1;
    return current.data;
  }

10.remove方法

remove(data){
    var index = this.indexOf(data);
    return this.removeAt(index);
  }

12.isEmpty方法

isEmpty(){
    return this.length == 0
  }

13.size方法

size(){
    return this.length
  }

打印代碼 - 放在鏈表函數(shù)中

// 打印出鏈表
  printList(){
    const nodes = []
    let current = this.head;
    while (current){
      nodes.push(current.data);
      current = current.next;
    }
    return nodes.join('-->')
  }

④ 測試代碼

// 測試代碼
// 測試代碼
var list = new doubleLinkList();
// 1.測試加入
list.append('aaa');
list.append('bbb');
list.append('ccc');
list.append('ddd');
list.append('eee');
list.append('fff');

list.insert('ggg',6);
console.log("插入'ggg'后 打印鏈表 ",list.printList());

// 3.測試正反方向分別打印轉(zhuǎn)換的字符串
console.log("正序",list.backwordString());
console.log("正序",list.forwardString());

// 4.獲得指定位置的元素
console.log("獲得位置是 4 的元素  ",list.get(4));

console.log(" ccc 的位置是 ",list.indexOf('ccc'));

list.update('jojo',2)
console.log("更新位置為 2 的元素 ",list.printList());

list.removeAt(2)
console.log("刪除位置為 2 的元素 ",list.printList());

list.remove('ggg');
console.log("移除'ggg' ",list.printList());

console.log("判斷是否為空",list.isEmpty());
console.log("判斷大小",list.size());
image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容