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 相关文章推荐
页面中iframe相互传值传参
Dec 13 Javascript
JavaScript中实现继承的三种方式和实例
Jan 29 Javascript
JS onkeypress兼容性写法详解
Apr 27 Javascript
微信小程序 video组件详解
Oct 25 Javascript
jquery获取easyui日期控件的值实现方法
Nov 09 Javascript
js以分隔符分隔数组中的元素并转换为字符串的方法
Nov 16 Javascript
JavaScript BASE64算法实现(完美解决中文乱码)
Jan 10 Javascript
vue语法之拼接字符串的示例代码
Oct 25 Javascript
bootstrap实现点击删除按钮弹出确认框的实例代码
Aug 16 Javascript
Vue项目查看当前使用的elementUI版本的方法
Sep 27 Javascript
vue 解除鼠标的监听事件的方法
Nov 13 Javascript
vue 调用 RESTful风格接口操作
Aug 11 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网站在线人数统计
2008/04/09 PHP
解析PHP处理换行符的问题 \r\n
2013/06/13 PHP
php实现微信公众平台发红包功能
2018/06/14 PHP
Yii2.0实现的批量更新及批量插入功能示例
2019/01/29 PHP
laravel excel 上传文件保存到本地服务器功能
2019/11/14 PHP
document.designMode的功能与使用方法介绍
2007/11/22 Javascript
js调用flash的效果代码
2008/04/26 Javascript
js类中的公有变量和私有变量
2008/07/24 Javascript
javascript学习笔记(十九) 节点的操作实现代码
2012/06/20 Javascript
javascript分页代码(当前页码居中)
2012/09/20 Javascript
ie8模式下click无反应点击option无反应的解决方法
2014/10/11 Javascript
jQuery随机密码生成的方法
2015/03/09 Javascript
原生JS实现平滑回到顶部组件
2016/03/16 Javascript
微信小程序开发之实现选项卡(窗口顶部TabBar)页面切换
2016/11/25 Javascript
jQuery Plupload上传插件的使用
2017/04/19 jQuery
详解 vue better-scroll滚动插件排坑
2018/02/08 Javascript
[43:36]Liquid vs Mineski 2018国际邀请赛小组赛BO2 第一场 8.18
2018/08/19 DOTA
探究Python多进程编程下线程之间变量的共享问题
2015/05/05 Python
Python写的一个定时重跑获取数据库数据
2016/12/28 Python
python DataFrame 修改列的顺序实例
2018/04/10 Python
轻松掌握CSS3中的字体大小单位rem的使用方法
2016/05/24 HTML / CSS
Merchant 1948澳大利亚:新西兰领先的鞋类和靴子供应商
2018/03/24 全球购物
科茨沃尔德家居商店:Scotts of Stow
2018/06/29 全球购物
Paradox London官方网站:英国新娘鞋婚礼鞋品牌
2019/08/29 全球购物
加拿大的标志性百货公司:Hudson’s Bay(哈得逊湾)
2019/09/03 全球购物
天网面试题
2013/04/07 面试题
如何掌握自荐信格式呢
2013/11/19 职场文书
大学生学年自我鉴定
2014/02/10 职场文书
水电工岗位职责
2014/02/12 职场文书
工伤赔偿协议书范本
2014/04/15 职场文书
素质教育标语
2014/06/27 职场文书
欢迎家长标语
2014/10/08 职场文书
高中生旷课检讨书
2014/10/08 职场文书
大学生入党积极分子党校学习思想汇报
2014/10/25 职场文书
2015年物资管理工作总结
2015/05/20 职场文书
pytorch实现加载保存查看checkpoint文件
2022/07/15 Python