美文网首页程序员
JavaScript 数据结构之链表

JavaScript 数据结构之链表

作者: Greatiga | 来源:发表于2020-08-24 10:50 被阅读0次

前言

掌握基本的数据结构与算法,并且能应用到实际中,应该是每一个计算机语言学习者的必备技能,前端的学习也一样。

既然是前端,那就用 JavaScript 来进行学习

数组与字符串

这个 JavaScript 原生就支持的,数组常见 Array 实例,字符串常见于 String 包装类型。

let arry = [1,2,3,4,5];
let str = 'Great';

const ary1 = new Array(1,'er',{ key: 1 });
const str1 = new String('yes');

数组和字符串可以说是应用最多的了。学习的时候,除了基本的赋值、操作方法,还应该掌握位置方法。

  • 对于数组,还应该掌握JS原生支持的迭代方法;以及几大数组排序算法、数组去重
  • 对于字符串,要掌握拼接、查找;除此之外,也要灵活应用正则表达式

链表

JavaScript 里面没有对链表的原生支持,需要进行封装。首先要知道链表的结构与特性。

  • 每一个节点应该包括数据元素和指针。
  • 数据元素存储了该节点携带的数据,指针用来指向下一个节点。
  • 有一个头节点来作为链表的开始
  • 链表的最后一个节点的指针指向为 null,代表结束

在 JS 里面,我们可以利用将其封装,用来产生节点,把通用方法定义为原型。

单链表

采用原型构造函数组合模式,将通用方法封装为原型,每一个实例对象用构造函数创建。

链表及链表节点构造函数

//链表节点构造函数
function ListNode(next = null, date = null) {
  this.next = next;
  this.date = date;
}
//链表构造函数
function LList() { 
  this.head = new ListNode();
  this.length = 0;//链表长度
}

查找方法

LList.prototype.findNode = function(date) {
  let p = this.head;
  while(p && p.date != date) {
    p = p.next;
  }
  return p;
}

插入方法

  • 头插法

始终让头结点指向新的节点,插入的顺序是颠倒的

LList.prototype.addListByHead = function(date) {//封装头插法
  let temp = this.head.next;
  let node = new ListNode(null, date);
  this.head.next = node;
  node.next = temp;
  this.length ++;
}
  • 尾插法

始终插入在最后一个节点后面,也是顺序添加的方法

LList.prototype.insert = function(newDate, lastNode) {//封装头插法
  let temp = lastNode.next;
  let node = new ListNode(null, newDate);
  lastNode.next = node;
  node.next = temp;
  this.length ++;
}
  • 指定位置插入

先找到指定节点,接着在指定节点后添加

LList.prototype.insertLocal = function(newDate, date) {
  let preNode = this.findNode(date);
  if(preNode) {
    let temp = preNode.next;
    let node = new ListNode(null, newDate);
    preNode.next = node;
    node.next = temp;
    this.length ++;
  } else {
    return false;
  }
}

打印方法

LList.prototype.display = function() {//封装打印链表
  let p = this.head.next;
  while(p) {
    console.log(p.date);
    p = p.next;
  }
}

删除方法

基本思路,找到删除节点的前驱节点,让前驱节点指向删除节点的下一个节点。删除节点失去引用,JS垃圾清除会回收它

LList.prototype.remove = function(date) {
  let p = this.head, pre;
  while(p && p.date != date) {
    pre = p;
    p = p.next;
  }
  if(p) {
    pre.next = p.next;
    this.length --;
  } else {
    return false;
  }
}

完整代码


//链表节点构造函数
function ListNode(next = null, date = null) {
  this.next = next;
  this.date = date;
}

//链表构造函数
function LList() { 
  this.head = new ListNode();
  this.length = 0;//链表长度

  LList.prototype.findNode = function(date) {//查找指定节点
    let p = this.head;
    while(p && p.date != date) {
      p = p.next;
    }
    return p;
  }

  LList.prototype.insert = function(newDate, lastNode) {//封装尾插法
    let temp = lastNode.next;
    let node = new ListNode(null, newDate);
    lastNode.next = node;
    node.next = temp;
    this.length ++;
    return node;
  }

  LList.prototype.addListByHead = function(date) {//封装头插法
    let temp = this.head.next;
    let node = new ListNode(null, date);
    this.head.next = node;
    node.next = temp;
    this.length ++;
  }

  LList.prototype.insertLocal = function(newDate, date) {//指定位置插入法
    let preNode = this.findNode(date);
    if(preNode) {
      let temp = preNode.next;
      let node = new ListNode(null, newDate);
      preNode.next = node;
      node.next = temp;
      this.length ++;
    } else {
      return false;
    }
  } 

  LList.prototype.remove = function(date) {//移出方法
    let p = this.head, pre;
    while(p && p.date != date) {
      pre = p;
      p = p.next;
    }
    if(p) {
      pre.next = p.next;
      this.length --;
    } else {
      return false;
    }
  }

  LList.prototype.display = function() {//封装打印链表
    let p = this.head.next;
    while(p) {
      console.log(p.date);
      p = p.next;
    }
  }
}


let list = new LList();

let lastNode =  list.insert('First', list.head);
lastNode = list.insert('Second', lastNode);
lastNode = list.insert('Three', lastNode);
lastNode = list.insert('Four', lastNode);

list.remove('Three');

list.display();

双向链表

在单链表的基础上,添加一个指向前一个节点的指针,其他操作与单链表类似

循环链表

与单链表基本一样,不同于循环链表最后一个节点始终指向头结点

相关文章

网友评论

    本文标题:JavaScript 数据结构之链表

    本文链接:https://www.haomeiwen.com/subject/vingjktx.html