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 操作cookies及正确使用cookies的属性
Oct 15 Javascript
JS 实现双色表格实现代码
Nov 24 Javascript
window.returnValue使用方法示例介绍
Jul 03 Javascript
js基本算法:冒泡排序,二分查找的简单实例
Oct 08 Javascript
vue指令以及dom操作详解
Mar 04 Javascript
bootstrap多层模态框滚动条消失的问题
Jul 21 Javascript
JS 中使用Promise 实现红绿灯实例代码(demo)
Oct 20 Javascript
JavaScript设计模式之装饰者模式定义与应用示例
Jul 25 Javascript
如何把vuejs打包出来的文件整合到springboot里
Jul 26 Javascript
在Vue.js中使用TypeScript的方法
Mar 19 Javascript
原生JS实现九宫格抽奖
Sep 13 Javascript
如何vue使用el-table遍历循环表头和表体数据
Apr 26 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
php单件模式结合命令链模式使用说明
2008/09/07 PHP
PHP缓存机制Output Control详解
2014/07/14 PHP
php中数据库连接方式pdo和mysqli对比分析
2015/02/25 PHP
浅谈PHP中的
2016/04/23 PHP
图文详解phpstorm配置Xdebug进行调试PHP教程
2016/06/13 PHP
Laravel框架中Blade模板的用法示例
2017/08/30 PHP
突发奇想的一个jquery插件
2010/11/19 Javascript
iframe子父页面调用js函数示例
2013/11/07 Javascript
JS实现无限级网页折叠菜单(类似树形菜单)效果代码
2015/09/17 Javascript
jQueryUI中的datepicker使用方法详解
2016/05/25 Javascript
利用NPM淘宝的node.js镜像加速nvm
2017/03/27 Javascript
微信小程序页面间通信的5种方式
2017/03/31 Javascript
jQuery Collapse1.1.0折叠插件简单使用
2017/08/28 jQuery
Angular弹出模态框的两种方式
2017/10/19 Javascript
Node.Js生成比特币地址代码解析
2018/04/21 Javascript
解决vue2.0 element-ui中el-upload的before-upload方法返回false时submit()不生效问题
2018/08/24 Javascript
微信公众号H5支付接口调用方法
2019/01/10 Javascript
vue实现吸顶、锚点和滚动高亮按钮效果
2019/10/21 Javascript
在vue-cli3.0 中使用预处理器 (Sass/Less/Stylus) 配置全局变量操作
2020/08/10 Javascript
[38:54]完美世界DOTA2联赛PWL S2 Rebirth vs LBZS 第一场 11.28
2020/12/01 DOTA
Python使用稀疏矩阵节省内存实例
2014/06/27 Python
python实时获取外部程序输出结果的方法
2019/01/12 Python
Django利用cookie保存用户登录信息的简单实现方法
2019/05/27 Python
Python如何基于selenium实现自动登录博客园
2019/12/16 Python
使用pycharm和pylint检查python代码规范操作
2020/06/09 Python
PyCharm Ctrl+Shift+F 失灵的简单有效解决操作
2021/01/15 Python
洛杉矶健身中心女性专用运动服饰品牌:Marika
2018/05/09 全球购物
设计师珠宝:Ylang 23
2018/05/11 全球购物
会计实习生自我鉴定
2013/12/12 职场文书
派出所正风肃纪剖析材料
2014/10/10 职场文书
党的群众路线教育实践活动查摆问题及整改措施
2014/10/10 职场文书
批评与自我批评范文
2014/10/15 职场文书
物业前台接待岗位职责
2015/04/03 职场文书
2019学校运动会开幕词
2019/05/13 职场文书
ORACLE数据库应用开发的三十个注意事项
2021/06/07 Oracle
排查Tomcat进程假死的问题
2022/05/06 Servers