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í)
這篇文章主要為大家介紹了一款像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含有一系列內(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ān)資料,文中給出了詳細(xì)的介紹和示例代碼供大家參考學(xué)習(xí),相信對(duì)大家的學(xué)習(xí)或者工作具有一定的學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編一起來(lái)看看吧。2017-05-05解決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-04nodejs async異步常用函數(shù)總結(jié)(推薦)
這篇文章主要介紹了nodejs async異步常用函數(shù)總結(jié)的相關(guān)資料,需要的朋友可以參考下2017-11-11express + 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-06nodejs中操作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