欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Node.js環(huán)境下JavaScript實(shí)現(xiàn)單鏈表與雙鏈表結(jié)構(gòu)

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

單鏈表(LinkedList)的javascript實(shí)現(xiàn)
npmjs相關(guān)庫(kù):
complex-list、smart-list、singly-linked-list
編程思路:

  • add方法用于將元素追加到鏈表尾部,借由insert方法來(lái)實(shí)現(xiàn);
  • 注意各個(gè)函數(shù)的邊界條件處理。

自己的實(shí)現(xiàn):

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實(shí)現(xiàn)
npmjs相關(guān)庫(kù):
complex-list、smart-list
編程思路:

  • 雙鏈表多了一個(gè)指向前趨的指針,故單鏈表中的輔助函數(shù)findPre就不需要了;
  • 增加了反向輸出方法;
  • 注意邊界條件的處理。

自己的實(shí)現(xiàn)
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;
})();

相關(guān)文章

  • Windows下安裝Bun像Node或Deno的現(xiàn)代JS運(yùn)行時(shí)

    Windows下安裝Bun像Node或Deno的現(xiàn)代JS運(yùn)行時(shí)

    這篇文章主要為大家介紹了一款像Node或Deno的現(xiàn)代JavaScript運(yùn)行時(shí)的bun在Windows下安裝過(guò)程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-07-07
  • 使用Node.js創(chuàng)建HTTP服務(wù)器并實(shí)現(xiàn)公網(wǎng)訪問(wèn)本地Server的步驟

    使用Node.js創(chuàng)建HTTP服務(wù)器并實(shí)現(xiàn)公網(wǎng)訪問(wèn)本地Server的步驟

    Node.js含有一系列內(nèi)置模塊,使得程序可以脫離 Apache HTTP Server 或 IIS,作為獨(dú)立服務(wù)器運(yùn),下面將介紹如何簡(jiǎn)單幾步實(shí)現(xiàn)遠(yuǎn)程公共網(wǎng)絡(luò)下訪問(wèn)windwos node.js的服務(wù)端,感興趣的朋友一起看看吧
    2023-11-11
  • 利用forever和pm2部署node.js項(xiàng)目過(guò)程

    利用forever和pm2部署node.js項(xiàng)目過(guò)程

    這篇文章主要給大家介紹了如何利用forever和pm2部署node.js項(xiàng)目的相關(guān)資料,文中給出了詳細(xì)的介紹和示例代碼供大家參考學(xué)習(xí),相信對(duì)大家的學(xué)習(xí)或者工作具有一定的學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編一起來(lái)看看吧。
    2017-05-05
  • node.js中npm包管理工具用法分析

    node.js中npm包管理工具用法分析

    這篇文章主要介紹了node.js中npm包管理工具用法,結(jié)合實(shí)例形式分析了node.js中npm包管理工具查看、安裝、更新、卸載等相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下
    2020-02-02
  • nvm安裝方法以及安裝后node不能使用解決

    nvm安裝方法以及安裝后node不能使用解決

    在我們的日常開(kāi)發(fā)中經(jīng)常會(huì)遇到這種情況,手上有好幾個(gè)項(xiàng)目,每個(gè)項(xiàng)目的需求不同,進(jìn)而不同項(xiàng)目必須依賴不同版的NodeJS運(yùn)行環(huán)境,nvm應(yīng)運(yùn)而生,這篇文章主要給大家介紹了關(guān)于nvm安裝方法以及安裝后node不能使用解決的相關(guān)資料,需要的朋友可以參考下
    2023-04-04
  • 解決node終端下運(yùn)行js文件不支持ES6語(yǔ)法

    解決node終端下運(yùn)行js文件不支持ES6語(yǔ)法

    這篇文章主要介紹了解決node終端下運(yùn)行js文件不支持ES6語(yǔ)法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • nodejs async異步常用函數(shù)總結(jié)(推薦)

    nodejs async異步常用函數(shù)總結(jié)(推薦)

    這篇文章主要介紹了nodejs async異步常用函數(shù)總結(jié)的相關(guān)資料,需要的朋友可以參考下
    2017-11-11
  • express + jwt + postMan驗(yàn)證實(shí)現(xiàn)持久化登錄

    express + jwt + postMan驗(yàn)證實(shí)現(xiàn)持久化登錄

    這篇文章主要介紹了express + jwt + postMan驗(yàn)證實(shí)現(xiàn)持久化登錄,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • nodejs中操作mysql數(shù)據(jù)庫(kù)示例

    nodejs中操作mysql數(shù)據(jù)庫(kù)示例

    這篇文章主要介紹了nodejs中操作mysql數(shù)據(jù)庫(kù)示例,本文演示了如何在NodeJS中創(chuàng)建創(chuàng)建mysql連接、mysql數(shù)據(jù)庫(kù)、插入數(shù)據(jù)、查詢數(shù)據(jù)等功能,需要的朋友可以參考下
    2014-12-12
  • 詳解Node.js 中使用 ECDSA 簽名遇到的坑

    詳解Node.js 中使用 ECDSA 簽名遇到的坑

    這篇文章主要介紹了詳解Node.js 中使用 ECDSA 簽名遇到的坑,主要是使用 Node.js 的 Crypto 模塊無(wú)法校驗(yàn)網(wǎng)絡(luò)傳輸過(guò)來(lái)的簽名結(jié)果,感興趣的小伙伴們可以參考一下
    2018-11-11

最新評(píng)論