原生JS實現單向鏈表


1.前言

用JS實現一個簡單的單向鏈表,並完成相關的功能

2.功能說明

  1. push(value):從鏈表尾部添加一個新的節點
  2. insertAfer(value,item):向鏈表中的item節點之后插入一個 值為value的新節點
  3. remove(value):刪除鏈表中值為value的節點
  4. removeAt(pos):刪除鏈表中第pos個節點
  5. find(value):查找鏈表中值為value的節點
  6. findPrevious(value):查找鏈表中值為value的節點的前一個節點
  7. indexof(vallue):查找鏈表中值為value的節點的索引值,如果查找不到則返回-1
  8. size():獲取當前鏈表的長度
  9. getHead():獲取當前鏈表的頭節點
  10. print():打印當前鏈表,供測試用

3. 代碼實現

3.1 創建鏈表類

 1 //創建一個Node輔助類,用來生成節點
 2 function Node(value) {
 3     this.value = value;
 4     this.next = null;
 5   }
 6   
 7 //鏈表類
 8 function LinkedList() {
 9     this.head = null;
10     this.length = 0;
11     //向鏈表尾部追加元素
12     this.push = push;
13     //從鏈表中查找某個元素
14     this.find = find;
15     //在鏈表中任意一個元素之后插入一個元素
16     this.insertAfter = insertAfter;
17     //從鏈表中查找任意元素節點的前一個節點
18     this.findPrevious = findPrevious;
19     //從鏈表中刪除值為value的元素
20     this.remove = remove;
21     //返回當前鏈表的長度
22     this.size = size;
23     //查找某個元素在鏈表中的索引值
24     this.indexof = indexof;
25     //刪除鏈表中第pos個元素
26     this.removeAt = removeAt;
27     //獲取鏈表中第一個元素
28     this.getHead = getHead;
29     //打印當前的鏈表,供測試用
30     this.print = print;
31   }

2.1 push(value):從鏈表尾部添加一個新的節點

function push(value) {
    var node = new Node(value);
    if (this.head == null) {
      this.head = node;
    } else {
      var current = this.head;
      while (current.next != null) {
        current = current.next;
      }
      current.next = node;
    }
    length++;
  }

3.3 insertAfer(value,item):向鏈表中的item節點之后插入一個 值為value的新節點

function insertAfter(value, item) {
    var node = new Node(value);
    var current = this.find(item);
    if (current == null) {
      return console.log('找不到元素');
    }
    node.next = current.next;
    current.next = node;
    length++;
  }

3.4 remove(value):刪除鏈表中值為value的節點

function remove(value) {
  var current = this.find(value);
  if (!current) {
    return console.log('鏈表中找不到被刪除的元素');
  }
  var previous = this.findPrevious(value);

  if (!previous) {
    this.head = current.next;
  } else {
    previous.next = current.next;
  }
  this.length--;
}

3.5 removeAt(pos):刪除鏈表中第pos個節點

function removeAt(pos) {
    if (pos > -1 && pos < length) {
      var current = this.head;
      var index = 0;
      if (pos === 0) {
        this.head = current.next;
      } else {
        while (index < pos) {
          var previous = current;
          current = current.next;
          index++;
        }
        previous.next = current.next;
      }
      length--;
    } else {
      return null;
    }
  }

3.6 find(value):查找鏈表中值為value的節點

function find(value) {
    var currentNode = this.head;
    if (currentNode == null) {
      console.log("這是一個空鏈表!!!");
      return null;
    }
    if (currentNode.value === value) {
      return currentNode;
    }
    while (currentNode.next) {
      currentNode = currentNode.next;
      if (currentNode.value === value) {
        return currentNode
      }
    }
    console.log("沒有找到該元素!!!");
    return null;
  }

3.7 findPrevious(value):查找鏈表中值為value的節點的前一個節點

function findPrevious(value) {
  var current = this.head;
  if (current == null) {
    console.log('這是一個空鏈表');
    return null;
  }
  if (current.value === value) {
    console.log('當前查找的節點為鏈表的頭節點,頭節點前再無節點');
    return null;
  }
  while (current.next) {
    if (current.next.value === value) {
      return current;
    }
    current = current.next;
  }
  console.log('找不到該元素的前一個元素');
  return null;
}

3.7 indexof(vallue):查找鏈表中值為value的節點的索引值,如果查找不到則返回-1

function indexof(value) {
    var current = this.head;
    var index = 0;
    if (current == null) {
      return null;
    } else {
      while (current) {
        if (current.value === value) {
          return index;
        }
        index++;
        current = current.next;
      }
    }
    return -1;
  }

3.8 size():獲取當前鏈表的長度

function size(){
    return length;
  }

3.9 getHead():獲取當前鏈表的頭節點

function getHead(){
    return this.head;
  }

3.10 print():打印當前鏈表,供測試用

function print() {
    var current = this.head;
    while (current != null) {
      console.log(current.value);
      current = current.next;
    }
  }

4. 功能測試

var list = new LinkedList();
  for (var i = 1; i < 6; i++) {
    list.push(i);
  }
list.print();

5.完整代碼

完整代碼請戳☞☞☞LinkedList.js

(完)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM