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


Posted in Javascript onJune 12, 2016

单链表(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;
})();
Javascript 相关文章推荐
简单的JS多重继承示例
Mar 13 Javascript
jQuery 源码分析笔记(6) jQuery.data
Jun 08 Javascript
javascript学习笔记(三) String 字符串类型介绍
Jun 19 Javascript
js弹出窗口之弹出层的小例子
Jun 17 Javascript
JavaScript代码实现禁止右键、禁选择、禁粘贴、禁shift、禁ctrl、禁alt
Nov 17 Javascript
AngularJS学习笔记(三)数据双向绑定的简单实例
Nov 08 Javascript
JavaScript队列、优先队列与循环队列
Nov 14 Javascript
微信公众号开发 自定义菜单跳转页面并获取用户信息实例详解
Dec 08 Javascript
JavaScript 字符串数字左补位,右补位,取固定长度,截位扩展函数代码
Mar 25 Javascript
vue.js组件之间传递数据的方法
Jul 10 Javascript
JavaScript实现多球运动效果
Sep 07 Javascript
梳理一下vue中的生命周期
Dec 30 Vue.js
JavaScript实现阿拉伯数字和中文数字互相转换
Jun 12 #Javascript
深入解析JavaScript中的arguments对象
Jun 12 #Javascript
基于css3新属性transform及原生js实现鼠标拖动3d立方体旋转
Jun 12 #Javascript
JS弹出窗口插件zDialog简单用法示例
Jun 12 #Javascript
jQuery实现拖拽页面元素并将其保存到cookie的方法
Jun 12 #Javascript
仅一个form表单 js实现注册信息依次填写提交功能
Jun 12 #Javascript
JS+HTML5手机开发之滚动和惯性缓动实现方法分析
Jun 12 #Javascript
You might like
在WINDOWS中设置计划任务执行PHP文件的方法
2011/12/19 PHP
codeigniter中测试通过的分页类示例
2014/04/17 PHP
php保存信息到当前Session的方法
2015/03/16 PHP
PHP数据库操作Helper类完整实例
2016/05/11 PHP
PHP学习笔记之php文件操作
2016/06/03 PHP
nodejs入门详解(多篇文章结合)
2012/03/07 NodeJs
javascript异步编程的4种方法
2014/02/19 Javascript
node.js中的path.basename方法使用说明
2014/12/09 Javascript
js实现从数组里随机获取元素
2015/01/12 Javascript
JS与jQuery实现隔行变色的方法
2016/09/09 Javascript
分类解析jQuery选择器
2016/11/23 Javascript
微信小程序 连续旋转动画(this.animation.rotate)详解
2017/04/07 Javascript
vue组件初学_弹射小球(实例讲解)
2017/09/06 Javascript
jquery中ajax请求后台数据成功后既不执行success也不执行error的完美解决方法
2017/12/24 jQuery
vue.js根据代码运行环境选择baseurl的方法
2018/02/28 Javascript
微信小程序onLaunch异步,首页onLoad先执行?
2018/09/20 Javascript
浅谈发布订阅模式与观察者模式
2019/04/09 Javascript
cordova+vue+webapp使用html5获取地理位置的方法
2019/07/06 Javascript
使用xampp将angular项目运行在web服务器的教程
2019/09/16 Javascript
videocapture库制作python视频高速传输程序
2013/12/23 Python
17个Python小技巧分享
2015/01/23 Python
python 遍历字符串(含汉字)实例详解
2017/04/04 Python
Python绘制频率分布直方图的示例
2019/07/08 Python
使用Python的turtle模块画国旗
2019/09/24 Python
纯CSS3实现鼠标滑过按钮动画第二节
2020/07/16 HTML / CSS
HTML5 标准将把互联网视频扔回到黑暗时代
2010/02/10 HTML / CSS
数以千计的折扣工业产品:ESE Direct
2018/05/20 全球购物
意大利体育用品和运动服网上商店:Maxi Sport
2019/09/14 全球购物
俄罗斯EPL钻石珠宝店:ЭПЛ
2019/10/22 全球购物
艺术应用与设计个人的自我评价
2013/11/23 职场文书
客服服务心得体会
2013/12/30 职场文书
大学生职业生涯设计书
2014/01/02 职场文书
我们的节日端午节活动方案
2014/03/02 职场文书
抗洪救灾标语
2014/10/08 职场文书
抄袭同学作业检讨书1000字
2014/11/20 职场文书
SpringBoot整合Redis入门之缓存数据的方法
2021/11/17 Redis