Node.js环境下JavaScript实现单链表与双链表结构

 更新时间:2016年06月12日 16:24:45   作者:zhoutk  
Node环境下通过npm可以获取list的几个相关库,但是我们这里注重于自己动手实现,接下来就一起来看一下Node.js环境下JavaScript实现单链表与双链表结构

单链表(LinkedList)的javascript实现
npmjs相关库:
complex-list、smart-list、singly-linked-list
编程思路:

  • add方法用于将元素追加到链表尾部,借由insert方法来实现;
  • 注意各个函数的边界条件处理。

自己的实现:

SingleNode.js

(function(){
 "use strict";

 function Node(element){
  this.element = element;
  this.next = null;
 }

 module.exports = Node;
})();

LinkedList.js

(function(){
 "use strict";

 var Node = require("./lib/SingleNode");

 function LinkedList(){
  this._head = new Node("This is Head Node.");
  this._size = 0;
 }

 LinkedList.prototype.isEmpty = function(){
  return this._size === 0;
 };

 LinkedList.prototype.size = function(){
  return this._size;
 };

 LinkedList.prototype.getHead = function(){
  return this._head;
 };

 LinkedList.prototype.display = function(){
  var currNode = this.getHead().next;
  while(currNode){
   console.log(currNode.element);
   currNode = currNode.next;
  }
 };

 LinkedList.prototype.remove = function(item){
  if(item) {
   var preNode = this.findPre(item);
   if(preNode == null)
    return ;
   if (preNode.next !== null) {
    preNode.next = preNode.next.next;
    this._size--;
   }
  }
 };

 LinkedList.prototype.add = function(item){
  this.insert(item);
 };

 LinkedList.prototype.insert = function(newElement, item){
  var newNode = new Node(newElement);
  var finder = item ? this.find(item) : null;
  if(!finder){
   var last = this.findLast();
   last.next = newNode;
  }
  else{
   newNode.next = finder.next;
   finder.next = newNode;
  }
  this._size++;
 };

 /*********************** Utility Functions ********************************/

 LinkedList.prototype.findLast = function(){
  var currNode = this.getHead();
  while(currNode.next){
   currNode = currNode.next;
  }
  return currNode;
 };

 LinkedList.prototype.findPre = function(item){
  var currNode = this.getHead();
  while(currNode.next !== null && currNode.next.element !== item){
   currNode = currNode.next;
  }
  return currNode;
 };

 LinkedList.prototype.find = function(item){
  if(item == null)
   return null;
  var currNode = this.getHead();
  while(currNode && currNode.element !== item){
   currNode = currNode.next;
  }
  return currNode;
 };

 module.exports = LinkedList;
})();


双链表(DoubleLinkedList)的javascript实现
npmjs相关库:
complex-list、smart-list
编程思路:

  • 双链表多了一个指向前趋的指针,故单链表中的辅助函数findPre就不需要了;
  • 增加了反向输出方法;
  • 注意边界条件的处理。

自己的实现
DoubleNode.js

(function(){
 "use strict";

 function Node(element){
  this.element = element;
  this.next = null;
  this.previous = null;
 }

 module.exports = Node;
})();

DoubleLinkedList.js

(function(){
 "use strict";
 var Node = require("./lib/DoubleNode");

 function DoubleLinkedList(){
  this._head = new Node("This is Head Node.");
  this._size = 0;
 }

 DoubleLinkedList.prototype.getHead = function(){
  return this._head;
 };

 DoubleLinkedList.prototype.isEmpty = function(){
  return this._size === 0;
 };

 DoubleLinkedList.prototype.size = function(){
  return this._size;
 };

 DoubleLinkedList.prototype.findLast = function(){
  var currNode = this.getHead();
  while(currNode.next){
   currNode = currNode.next;
  }
  return currNode;
 };

 DoubleLinkedList.prototype.add = function(item){
  if(item == null)
   return null;
  this.insert(item);
 };

 DoubleLinkedList.prototype.remove = function(item){
  if(item) {
   var node = this.find(item);
   if(node == null)
    return ;
   if (node.next === null) {
    node.previous.next = null;
    node.previous = null;
   } else{
    node.previous.next = node.next;
    node.next.previous = node.previous;
    node.next = null;
    node.previous = null;
   }
   this._size--;
  }
 };

 DoubleLinkedList.prototype.find = function(item){
  if(item == null)
   return null;
  var currNode = this.getHead();
  while(currNode && currNode.element !== item){
   currNode = currNode.next;
  }
  return currNode;
 };

 DoubleLinkedList.prototype.insert = function(newElement, item){
  var newNode = new Node(newElement);
  var finder = item ? this.find(item) : null;
  if(!finder){
   var last = this.findLast();
   newNode.previous = last;
   last.next = newNode;
  }
  else{
   newNode.next = finder.next;
   newNode.previous = finder;
   finder.next.previous = newNode;
   finder.next = newNode;
  }
  this._size++;
 };

 DoubleLinkedList.prototype.dispReverse = function(){
  var currNode = this.findLast();
  while(currNode != this.getHead()){
   console.log(currNode.element);
   currNode = currNode.previous;
  }
 };

 DoubleLinkedList.prototype.display = function(){
  var currNode = this.getHead().next;
  while(currNode){
   console.log(currNode.element);
   currNode = currNode.next;
  }
 };

 module.exports = DoubleLinkedList;
})();

相关文章

  • Node Puppeteer图像识别实现百度指数爬虫的示例

    Node Puppeteer图像识别实现百度指数爬虫的示例

    本篇文章主要介绍了Node Puppeteer图像识别实现百度指数爬虫的示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-02-02
  • Node.js API详解之 vm模块用法实例分析

    Node.js API详解之 vm模块用法实例分析

    这篇文章主要介绍了Node.js API详解之 vm模块用法,结合实例形式分析了Node.js API中vm模块基本功能、函数、使用方法及相关操作注意事项,需要的朋友可以参考下
    2020-05-05
  • 优化Node.js Web应用运行速度的10个技巧

    优化Node.js Web应用运行速度的10个技巧

    这篇文章主要介绍了优化Node.js Web应用运行速度的10个技巧,本文讲解了从并行、异步、缓存、gzip 压缩、客户端渲染等等技巧,需要的朋友可以参考下
    2014-09-09
  • 搭建基于express框架运行环境的方法步骤

    搭建基于express框架运行环境的方法步骤

    Express提供了一个轻量级模块,把Node.js的http模块功能封装在一个简单易用的接口中,这篇文章主要介绍了搭建基于express框架运行环境的方法步骤,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-11-11
  • nodejs中的读取文件fs与文件路径path解析

    nodejs中的读取文件fs与文件路径path解析

    这篇文章主要介绍了nodejs中的读取文件fs与文件路径path解析,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-08-08
  • Node.js下自定义错误类型详解

    Node.js下自定义错误类型详解

    在JavaScript里面,运行过程中的错误的类型总是被人忽略,这篇文章给大家详细介绍了如何在Node.js下自定义错误类型,对大家学习或者使用Node.js具有一定的参考借鉴价值,有需要的朋友们可以参考借鉴,下面来一起看看吧。
    2016-10-10
  • Webpack 实现 Node.js 代码热替换

    Webpack 实现 Node.js 代码热替换

    Webpack有一个很实用的功能叫做热替换(Hot-replace),尤其是结合React Hot Loader插件,开发过程中都不需要刷新浏览器,任何前端代码的更改都会实时的在浏览器中表现出来。
    2015-10-10
  • 详解node登录接口之密码错误限制次数(含代码)

    详解node登录接口之密码错误限制次数(含代码)

    这篇文章主要介绍了nodejs登录接口之密码错误限制次数(含代码),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-10-10
  • node.js模拟实现自动发送邮件验证码

    node.js模拟实现自动发送邮件验证码

    这篇文章主要为大家介绍了node.js模拟实现自动发送邮件验证码的实例过程,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-04-04
  • Node.js五大应用性能技巧小结(必须收藏)

    Node.js五大应用性能技巧小结(必须收藏)

    本篇文章主要介绍了Node.js五大应用性能技巧小结(必须收藏),小编觉得挺不错的,现在分享给大家
    2017-08-08

最新评论