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 相关文章推荐
onpropertypchange
Jul 01 Javascript
JavaScript中常见陷阱小结
Apr 27 Javascript
js获取单选框或复选框值及操作
Dec 18 Javascript
ExtJS中设置下拉列表框不可编辑的方法
May 07 Javascript
jQuery超简单选项卡完整实例
Sep 26 Javascript
jquery.cookie.js用法实例详解
Dec 25 Javascript
深入浅析JavaScript中的scrollTop
Jul 11 Javascript
bootstrap fileinput完整实例分享
Nov 08 Javascript
原生JS实现不断变化的标签
May 22 Javascript
vue component组件使用方法详解
Jul 14 Javascript
微信小程序使用input组件实现密码框功能【附源码下载】
Dec 11 Javascript
node.js使用express框架进行文件上传详解
Mar 03 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
人大复印资料处理程序_查询篇
2006/10/09 PHP
谈谈你对Zend SAPIs(Zend SAPI Internals)的理解
2015/11/10 PHP
Javascript之文件操作
2007/03/07 Javascript
JavaScript获取GridView选择的行内容
2009/04/14 Javascript
js实现简单登录功能的实例代码
2013/11/09 Javascript
jquery实现弹出窗口效果的实例代码
2013/11/28 Javascript
javascript 按键事件(兼容各浏览器)
2013/12/20 Javascript
IE与FF下javascript获取网页及窗口大小的区别详解
2014/01/14 Javascript
javascript:void(0)的问题使用探讨
2014/04/10 Javascript
jQuery中ajax错误调试分析
2016/12/01 Javascript
Vue不能观察到数组length的变化
2018/06/08 Javascript
微信小程序如何修改radio和checkbox的默认样式和图标
2019/07/24 Javascript
开源一个微信小程序仪表盘组件过程解析
2019/07/30 Javascript
vue实现计算器功能
2020/02/22 Javascript
JS加载解析Markdown文档过程详解
2020/05/19 Javascript
微信小程序报错: thirdScriptError的错误问题
2020/06/19 Javascript
python实现跨文件全局变量的方法
2014/07/07 Python
python Django批量导入不重复数据
2016/03/25 Python
Tensorflow实现卷积神经网络的详细代码
2018/05/24 Python
Python实现获取汉字偏旁部首的方法示例【测试可用】
2018/12/18 Python
Python之循环结构
2019/01/15 Python
Python深拷贝与浅拷贝用法实例分析
2019/05/05 Python
Python 函数list&read&seek详解
2019/08/28 Python
python 队列基本定义与使用方法【初始化、赋值、判断等】
2019/10/24 Python
jupyter实现重新加载模块
2020/04/16 Python
给ubuntu18安装python3.7的详细教程
2020/06/08 Python
司机检讨书
2014/02/13 职场文书
怎样填写就业意向
2014/04/02 职场文书
应聘会计求职信
2014/06/11 职场文书
2014大学校园光棍节活动策划书
2014/09/29 职场文书
2015新员工试用期工作总结
2014/12/12 职场文书
教师岗位职责范本
2015/04/02 职场文书
2015教师年度工作总结范文
2015/04/07 职场文书
导游词之宿迁乾隆行宫
2019/10/15 职场文书
springmvc直接不经过controller访问WEB-INF中的页面问题
2022/02/24 Java/Android
SQL Server中的游标介绍
2022/05/20 SQL Server