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语句可以不以;结尾的烦恼
Mar 08 Javascript
js数组的基本用法及数组根据下标(数值或字符)移除元素
Oct 20 Javascript
JS判断对象是否存在的10种方法总结
Dec 23 Javascript
为jQuery添加Webkit的触摸的方法分享
Feb 02 Javascript
跟我学习javascript的prototype,getPrototypeOf和__proto__
Nov 17 Javascript
JavaScript实现事件的中断传播和行为阻止方法示例
Jan 20 Javascript
基于JS实现bookstore静态页面的实例代码
Feb 22 Javascript
Bootstrap 模态框多次显示后台提交多次BUG的解决方法
Dec 26 Javascript
vue项目打包部署到服务器的方法示例
Aug 27 Javascript
微信实现自动跳转到用其他浏览器打开指定APP下载
Feb 15 Javascript
JS array数组检测方式解析
May 19 Javascript
angular中的post请求处理示例详解
Jun 30 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
教你如何解密 “ PHP 神盾解密工具 ”
2014/06/20 PHP
php将字符串随机分割成不同长度数组的方法
2015/06/01 PHP
PHP批量删除jQuery操作
2017/07/23 PHP
js取模(求余数)隔行变色
2014/05/15 Javascript
JS实现双击编辑可修改状态的方法
2015/08/14 Javascript
使用jQuery判断浏览器滚动条位置的方法
2016/05/30 Javascript
JS实现双击内容变为可编辑状态
2017/03/03 Javascript
详解Node.js项目APM监控之New Relic
2017/05/12 Javascript
JavaScript hasOwnProperty() 函数实例详解
2017/08/04 Javascript
nodejs+mongodb+vue前后台配置ueditor的示例代码
2018/01/02 NodeJs
深入理解js 中async 函数的含义和用法
2018/05/13 Javascript
VUE前后端学习tab写法实例
2019/08/06 Javascript
vue中重定向redirect:‘/index‘,不显示问题、跳转出错的完美解决
2020/09/28 Javascript
vue中watch的用法汇总
2020/12/28 Vue.js
复制粘贴功能的Python程序
2008/04/04 Python
Python实现同时兼容老版和新版Socket协议的一个简单WebSocket服务器
2014/06/04 Python
Python-嵌套列表list的全面解析
2016/06/08 Python
详解Python使用tensorflow入门指南
2018/02/09 Python
使用 Python 实现简单的 switch/case 语句的方法
2018/09/17 Python
Python产生Gnuplot绘图数据的方法
2018/11/09 Python
python实现名片管理系统
2018/11/29 Python
Python中一般处理中文的几种方法
2019/03/06 Python
python切片的步进、添加、连接简单操作示例
2019/07/11 Python
python3 下载网络图片代码实例
2019/08/27 Python
python+mysql实现个人论文管理系统
2019/10/25 Python
使用html5 canvas创建太空游戏的示例
2014/05/08 HTML / CSS
世界上最大的街头服饰网站:Karmaloop
2017/02/04 全球购物
波兰香水和化妆品购物网站:Notino.pl
2017/11/07 全球购物
英国历史最悠久的DJ设备供应商:DJ Finance、DJ Warehouse、The DJ Shop
2019/09/04 全球购物
Servlet的生命周期
2013/08/25 面试题
一封普通求职者的求职信
2013/11/20 职场文书
王力宏牛津大学演讲稿
2014/05/22 职场文书
2014年房产销售工作总结
2014/12/08 职场文书
感谢师恩主题班会
2015/08/17 职场文书
廉洁自律准则学习心得体会
2016/01/13 职场文书
html css3不拉伸图片显示效果
2021/06/07 HTML / CSS