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 相关文章推荐
JavaScript的面向对象(一)
Nov 09 Javascript
Mootools 1.2教程 事件处理
Sep 15 Javascript
jQuery的实现原理的模拟代码 -5 Ajax
Aug 07 Javascript
详解angular用$sce服务来过滤HTML标签
Apr 11 Javascript
AngularJS自定义指令之复制指令实现方法
May 18 Javascript
VUE中v-model和v-for指令详解
Jun 23 Javascript
Vue.js中组件中的slot实例详解
Jul 17 Javascript
Mac下安装vue
Apr 11 Javascript
深入理解Vue nextTick 机制
Apr 28 Javascript
Node.Js中实现端口重用原理详解
May 03 Javascript
微信小程序实现上拉加载功能
Nov 20 Javascript
Node.js API详解之 os模块用法实例分析
May 06 Javascript
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
星际争霸任务指南——人族
2020/03/04 星际争霸
php的计数器程序
2006/10/09 PHP
PHP生成网页快照 不用COM不用扩展.
2010/02/11 PHP
php实现检查文章是否被百度收录
2015/01/27 PHP
PHP实现唤起微信支付功能
2019/02/18 PHP
Laravel 实现添加多语言提示信息
2019/10/25 PHP
Ext面向对象开发实践(续)
2008/11/18 Javascript
利用JQuery为搜索栏增加tag提示
2009/06/22 Javascript
为JavaScript添加重载函数的辅助方法
2010/07/04 Javascript
Javascript中定义方法的另类写法(批量定义js对象的方法)
2011/02/25 Javascript
javascript SpiderMonkey中的函数序列化如何进行
2012/12/05 Javascript
jQuery链式操作如何实现以及为什么要用链式操作
2013/01/17 Javascript
jQuery中Ajax的get、post等方法详解
2015/01/20 Javascript
JS拖动鼠标画出方框实现鼠标选区的方法
2015/08/05 Javascript
Javascript之String对象详解
2016/06/08 Javascript
react-router实现按需加载
2017/05/09 Javascript
Angular中ng-repeat与ul li的多层嵌套重复问题
2017/07/24 Javascript
vue+webpack实现异步加载三种用法示例详解
2018/04/24 Javascript
vue实现自定义H5视频播放器的方法步骤
2019/07/01 Javascript
javascript实现画板功能
2020/04/12 Javascript
React中使用Vditor自定义图片详解
2020/12/25 Javascript
python使用多线程不断刷新网页的方法
2015/03/31 Python
OpenCV2.3.1+Python2.7.3+Numpy等的配置解析
2018/01/05 Python
Python实现迭代时使用索引的方法示例
2018/06/05 Python
python 进程的几种创建方式详解
2019/08/29 Python
pycharm无法安装第三方库的问题及解决方法以scrapy为例(图解)
2020/05/09 Python
利用Python发送邮件或发带附件的邮件
2020/11/12 Python
css3 中实现炫酷的loading效果
2019/04/26 HTML / CSS
六道php面试题附答案
2014/06/05 面试题
c语言常见笔试题总结
2016/09/05 面试题
焊接专业毕业生求职信
2013/10/01 职场文书
运动会解说词50字
2014/01/18 职场文书
医药销售自荐书
2014/05/29 职场文书
2014年教务工作总结
2014/12/03 职场文书
2015年司机年终工作总结
2015/05/14 职场文书
详解Laravel服务容器的优势
2021/05/29 PHP