最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Node.js環(huán)境下JavaScript實現單鏈表與雙鏈表結構

 更新時間:2016年06月12日 16:24:45   作者:zhoutk  
Node環(huán)境下通過npm可以獲取list的幾個相關庫,但是我們這里注重于自己動手實現,接下來就一起來看一下Node.js環(huán)境下JavaScript實現單鏈表與雙鏈表結構

單鏈表(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;
})();

相關文章

  • Node Puppeteer圖像識別實現百度指數爬蟲的示例

    Node Puppeteer圖像識別實現百度指數爬蟲的示例

    本篇文章主要介紹了Node Puppeteer圖像識別實現百度指數爬蟲的示例,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-02-02
  • Node.js API詳解之 vm模塊用法實例分析

    Node.js API詳解之 vm模塊用法實例分析

    這篇文章主要介紹了Node.js API詳解之 vm模塊用法,結合實例形式分析了Node.js API中vm模塊基本功能、函數、使用方法及相關操作注意事項,需要的朋友可以參考下
    2020-05-05
  • 優(yōu)化Node.js Web應用運行速度的10個技巧

    優(yōu)化Node.js Web應用運行速度的10個技巧

    這篇文章主要介紹了優(yōu)化Node.js Web應用運行速度的10個技巧,本文講解了從并行、異步、緩存、gzip 壓縮、客戶端渲染等等技巧,需要的朋友可以參考下
    2014-09-09
  • 搭建基于express框架運行環(huán)境的方法步驟

    搭建基于express框架運行環(huán)境的方法步驟

    Express提供了一個輕量級模塊,把Node.js的http模塊功能封裝在一個簡單易用的接口中,這篇文章主要介紹了搭建基于express框架運行環(huán)境的方法步驟,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-11-11
  • nodejs中的讀取文件fs與文件路徑path解析

    nodejs中的讀取文件fs與文件路徑path解析

    這篇文章主要介紹了nodejs中的讀取文件fs與文件路徑path解析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • Node.js下自定義錯誤類型詳解

    Node.js下自定義錯誤類型詳解

    在JavaScript里面,運行過程中的錯誤的類型總是被人忽略,這篇文章給大家詳細介紹了如何在Node.js下自定義錯誤類型,對大家學習或者使用Node.js具有一定的參考借鑒價值,有需要的朋友們可以參考借鑒,下面來一起看看吧。
    2016-10-10
  • Webpack 實現 Node.js 代碼熱替換

    Webpack 實現 Node.js 代碼熱替換

    Webpack有一個很實用的功能叫做熱替換(Hot-replace),尤其是結合React Hot Loader插件,開發(fā)過程中都不需要刷新瀏覽器,任何前端代碼的更改都會實時的在瀏覽器中表現出來。
    2015-10-10
  • 詳解node登錄接口之密碼錯誤限制次數(含代碼)

    詳解node登錄接口之密碼錯誤限制次數(含代碼)

    這篇文章主要介紹了nodejs登錄接口之密碼錯誤限制次數(含代碼),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-10-10
  • node.js模擬實現自動發(fā)送郵件驗證碼

    node.js模擬實現自動發(fā)送郵件驗證碼

    這篇文章主要為大家介紹了node.js模擬實現自動發(fā)送郵件驗證碼的實例過程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-04-04
  • Node.js五大應用性能技巧小結(必須收藏)

    Node.js五大應用性能技巧小結(必須收藏)

    本篇文章主要介紹了Node.js五大應用性能技巧小結(必須收藏),小編覺得挺不錯的,現在分享給大家
    2017-08-08

最新評論

桦南县| 安福县| 界首市| 延长县| 徐州市| 永吉县| 牙克石市| 安化县| 阳江市| 镇坪县| 萨迦县| 河西区| 大渡口区| 阜新市| 吴旗县| 庄河市| 岳普湖县| 延吉市| 铜川市| 林口县| 治县。| 浙江省| 思南县| 宽城| 思南县| 云林县| 五华县| 同江市| 绥中县| 襄城县| 和平县| 双桥区| 桃江县| 英德市| 鄢陵县| 永兴县| 双峰县| 新田县| 漳浦县| 突泉县| 普安县|