Node.js環(huán)境下JavaScript實現(xiàn)單鏈表與雙鏈表結(jié)構(gòu)
單鏈表(LinkedList)的javascript實現(xiàn)
npmjs相關(guān)庫:
complex-list、smart-list、singly-linked-list
編程思路:
- add方法用于將元素追加到鏈表尾部,借由insert方法來實現(xiàn);
- 注意各個函數(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實現(xiàn)
npmjs相關(guān)庫:
complex-list、smart-list
編程思路:
- 雙鏈表多了一個指向前趨的指針,故單鏈表中的輔助函數(shù)findPre就不需要了;
- 增加了反向輸出方法;
- 注意邊界條件的處理。
自己的實現(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)行時
這篇文章主要為大家介紹了一款像Node或Deno的現(xiàn)代JavaScript運(yùn)行時的bun在Windows下安裝過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07使用Node.js創(chuàng)建HTTP服務(wù)器并實現(xiàn)公網(wǎng)訪問本地Server的步驟
Node.js含有一系列內(nèi)置模塊,使得程序可以脫離 Apache HTTP Server 或 IIS,作為獨立服務(wù)器運(yùn),下面將介紹如何簡單幾步實現(xiàn)遠(yuǎn)程公共網(wǎng)絡(luò)下訪問windwos node.js的服務(wù)端,感興趣的朋友一起看看吧2023-11-11nodejs async異步常用函數(shù)總結(jié)(推薦)
這篇文章主要介紹了nodejs async異步常用函數(shù)總結(jié)的相關(guān)資料,需要的朋友可以參考下2017-11-11express + jwt + postMan驗證實現(xiàn)持久化登錄
這篇文章主要介紹了express + jwt + postMan驗證實現(xiàn)持久化登錄,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06