博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
es6 实现双链表
阅读量:6568 次
发布时间:2019-06-24

本文共 3234 字,大约阅读时间需要 10 分钟。

const util = require('util');/** * 链表节点类 */class Node {  constructor (ele) {    this.ele = ele;    this.next = null;    this.prev = null;  }}/** * 链表类 */class NodeList {  constructor (ele) {    this.head = null; // 初始化链表的头节点    this.length = 0;  }  /**   *  尾部插入数据   * @param {*} ele  */  append (ele) {    let newNode = new Node(ele);    let currentNode;    if (this.head === null) {      this.head = newNode;    } else {      currentNode = this.head;      while (currentNode.next) {        currentNode = currentNode.next;      }      currentNode.next = newNode;      newNode.prev = currentNode;    }    this.length++;  }  /**   * 项链表某个位置插入元素   * @param {*} position   * @param {*} ele   */  insert (position, ele) {    if (position >= 0 && position <= this.length) {      let newNode = new Node(ele);      let currentNode = this.head;      let pre;      let index = 0;      if (position === 0) {        if (currentNode === null) {          this.head = newNode;        } else {          this.head = newNode;          newNode.next = currentNode;          currentNode.prev = newNode;        }      } else if (position === this.length) {        this.append(ele);        return;      } else {        while (index < position) {          pre = currentNode;          currentNode = currentNode.next;          index++;        }        newNode.next = currentNode;        currentNode.prev = newNode;        pre.next = newNode;        newNode.prev = pre;      }      this.length++;    } else {      return new Error('位置超出范围');    }  }  removeAt (position) {    if (position >= 0 && position < this.length) {      let currentNode = this.head;      let pre;      let index = 0;      if (position === 0) {        this.head = currentNode.next;      } else {        while (index < position) { // 1,2,3          pre = currentNode;          currentNode = currentNode.next;          index++;        }        pre.next = currentNode.next;        if (currentNode && currentNode.next !== null) {          currentNode.next.prev = pre;        }      }      this.length--;    } else {      return new Error('删除位置有误');    }  }  find (ele) {    let currentNode = this.head;    let index = 0;    while (currentNode) {      if (JSON.stringify(currentNode.ele) === JSON.stringify(ele)) {        return index;      } else {        index++;        currentNode = currentNode.next;      }    }    return -1;  }  // 判断链表是否为空  isEmpty () {    return this.length === 0;  }  size () {    return this.length;  }  // 返回头  getHead () {    return this.head;  }  toString () {    let current = this.head;    let str = '';    while (current) {      str += JSON.stringify(current.ele) + ' => ';      current = current.next;    }    return str;  }}let A = { name: 'A', age: 10 };let B = { name: 'B', age: 20 };let C = { name: 'C', age: 30 };let D = { name: 'D', age: 40 };let E = { name: 'E', age: 50 };let G = { name: 'G', age: 50 };let nList = new NodeList();nList.append(A);nList.append(C);nList.append(B);nList.append(D);nList.append(E);// console.log(JSON.stringify(nList, null, 2));nList.insert(5, G);console.log(' origin: ' + nList);nList.removeAt(5);console.log('now: ' + nList);console.log(util.inspect(nList));

  

 

转载于:https://www.cnblogs.com/xiaosongJiang/p/11003920.html

你可能感兴趣的文章
团队-象棋游戏-设计文档
查看>>
hibernate Expression详解
查看>>
HTTP长连接和短连接以及推送服务原理(转)
查看>>
问卷设计入门
查看>>
input子系统分析之三:驱动模块
查看>>
jquery 选择时间(小时)区间(四)
查看>>
jquery 选择时间(小时)区间(二)
查看>>
WebService的编写与调用
查看>>
(模板)字符串哈希
查看>>
input:focus
查看>>
java中String,int,Integer,char、double类型转换
查看>>
Hdoj 2544
查看>>
nginx转发端口路由器再转发
查看>>
在eclipse里的 flex 没有可视化的编辑
查看>>
python 列出出当前目录及所有子目录下的文件
查看>>
RabbitMQ错误检查
查看>>
OSI模型
查看>>
对于数据库连接池的一些思考和MyBatis的集成与使用
查看>>
[SDOI2016]征途
查看>>
[JSOI2018]军训列队
查看>>