数据结构中的链表(Javascript实现)

135 阅读2分钟

什么是链表

链表存储有序的元素集合,但不同于数组,链表中的元素在内存中并不是连续放置的。每个元素都由一个存储元素本身的节点和一个指向下一个元素的引用组成。

相对于传统的数组,链表有一个好处在于,添加和移除元素的时候不需要移动其他元素。然而,链表需要使用指针,因此使用链表时需要额外注意。

在数组中, 我们可以直接访问任何位置的任何元素,而想要访问链表中间的一个元素,则需要从起点(表头)开始迭代链表直到找到所需的元素。

单向链表的实现

// 链表节点类
class Node {
   constructor(element) {
       this.element = element;
       this.next = undefined;
   }
}


// 链表类
class LinkedList {
   constructor() {
       this.count = 0; // 链表元素数量
       this.head = undefined; // 链表表头元素
   }
   /**
    * 向链表尾部添加元素
    */
   push(element) {
       const node = new Node(element);
       if(!this.head) {
           this.head = node;
       } else {
           let current = this.head;
           while(current.next) { // 迭代访问到链表结尾
               current = current.next;
           }
           current.next = node;
       }
       this.count ++;
   }
   /**
    * 移除指定位置的元素
    */
   removeAt(index) {
       // 检查越界
       if(index >= 0 && index < this.count) {
           let current = this.head;
           if(index === 0) { // 移除表头元素
               this.head = current.next;
           } else {
               let previous;
               for(let i = 0;i < index;i ++) {
                   previous = current;
                   current = current.next;
               }
               // 跳过指定位置的节点,从而达到移除元素的目的
               previous.next = current.next;
           }
           this.count --;
           return current.element;
       }
       return undefined;
   }
   /**
    * 在任意位置添加元素
    */
   insert(element, index) {
       // 检查越界
       if(index >= 0 && index < this.count) {
           const node = new Node(element);
           if(index === 0) { // 在第一个位置添加
               const current = this.head;
               node.next = current;
               this.head = node;
           } else {
               let previous, current = this.head;
               for(let i = 0;i < index;i ++) {
                   previous = current;
                   current = current.next;
               }
               node.next = current;
               previous.next = node;
           }
           this.count ++;
           return true;
       }
       return false;
   }
   /**
    * 返回一个元素的位置
    */
   indexOf(element) {
       let current = this.head;
       for (let i = 0;i < this.count && current != null;i ++) {
           if (element === current.element) {
               return i;
           }
           current = current.next;
       }
       return -1;
   }
   /**
    * 从链表中移除元素
    */
   remove(element) {
       const index = this.indexOf(element);
       return this.removeAt(index);
   }
   /**
    * 获取链表长度
    */
   size() {
       return this.count;
   }
   /**
    * 判断链表是否为空
    */
   isEmpty() {
       return this.count === 0;
   }
   toString() {
       if(!this.head) return '';
       let objString = `${this.head.element}`;
       let current = this.head.next;
       for(let i = 1;i < this.size() && current != null;i ++) {
           objString += `${current.element}`;
           current = current.next;
       }
       return objString;
   }
}

const linkedList = new LinkedList();
linkedList.push(5);
linkedList.push(4);
console.log(linkedList.size()) // 2
console.log(linkedList.toString()) // 54
console.log(linkedList.isEmpty()) // false