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 相关文章推荐
JQuery扩展插件Validate—4设置错误提示的样式
Sep 05 Javascript
侧栏跟随滚动的简单实现代码
Mar 18 Javascript
js跨域问题浅析及解决方法优缺点对比
Nov 08 Javascript
js实现缓冲运动效果的方法
Apr 10 Javascript
jQuery实现的漂亮表单效果代码
Aug 18 Javascript
Jquery ajax请求导出Excel表格的实现代码
Jun 08 Javascript
让浏览器崩溃的12行JS代码(DoS攻击分析及防御)
Oct 10 Javascript
JS制作适用于手机和电脑的通知信息效果
Oct 28 Javascript
vue.js数据绑定的方法(单向、双向和一次性绑定)
Jul 13 Javascript
JS实现键值对遍历json数组功能示例
May 30 Javascript
JS实现的RC4加密算法示例
Aug 16 Javascript
Node使用Selenium进行前端自动化操作的代码实现
Oct 10 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函数)
2006/10/09 PHP
PHP 验证码不显示只有一个小红叉的解决方法
2013/09/30 PHP
一个图片地址分解程序(用于PHP小偷程序)
2014/08/23 PHP
php生成shtml类用法实例
2014/12/09 PHP
详解PHP执行定时任务的实现思路
2015/12/21 PHP
用 JSON 处理缓存
2007/04/27 Javascript
JavaScript 工具库 Cloudgamer JavaScript Library v0.1 发布
2009/10/29 Javascript
setTimeout和setInterval的深入理解
2013/11/08 Javascript
jquery仿搜索自动联想功能代码
2014/05/23 Javascript
jQuery学习笔记之创建DOM元素
2015/01/19 Javascript
JavaScript 模块化编程(笔记)
2015/04/08 Javascript
js实现touch移动触屏滑动事件
2015/04/17 Javascript
jQuery实现本地预览上传图片功能
2016/01/08 Javascript
Javascript 实现放大镜效果实例详解
2016/12/03 Javascript
原生js编写基于面向对象的分页组件
2016/12/05 Javascript
概述jQuery中的ajax方法
2016/12/16 Javascript
Vue中computed与methods的区别详解
2018/03/24 Javascript
详解vue 自定义组件使用v-model 及探究其中原理
2019/10/11 Javascript
pymongo实现多结果进行多列排序的方法
2015/05/16 Python
Python上传package到Pypi(代码简单)
2016/02/06 Python
详解Python中的文件操作
2016/08/28 Python
python 读写文件,按行修改文件的方法
2018/07/12 Python
Python使用Selenium爬取淘宝异步加载的数据方法
2018/12/17 Python
Python程序暂停的正常处理方法
2019/11/07 Python
python 实现让字典的value 成为列表
2019/12/16 Python
英国第一家领先的在线处方眼镜零售商:Glasses Direct
2018/02/23 全球购物
来自世界各地的优质葡萄酒:VineShop24
2018/07/09 全球购物
Brother加拿大官网:打印机、贴标机、缝纫机
2019/10/09 全球购物
世界上最大的字体市场:MyFonts
2020/01/10 全球购物
Jar包的作用是什么
2014/03/30 面试题
临床医学专业个人的自我评价
2013/09/27 职场文书
青年教师培训方案
2014/02/06 职场文书
剪彩仪式主持词
2014/03/19 职场文书
教育见习报告范文
2014/11/03 职场文书
诚信高考倡议书
2019/06/24 职场文书
MongoDB连接数据库并创建数据等使用方法
2021/11/27 MongoDB