Node.js環(huán)境下JavaScript實(shí)現(xiàn)單鏈表與雙鏈表結(jié)構(gòu)
單鏈表(LinkedList)的javascript實(shí)現(xiàn)
npmjs相關(guān)庫:
complex-list、smart-list、singly-linked-list
編程思路:
- add方法用于將元素追加到鏈表尾部,借由insert方法來實(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)庫:
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)文章
Node Puppeteer圖像識別實(shí)現(xiàn)百度指數(shù)爬蟲的示例
本篇文章主要介紹了Node Puppeteer圖像識別實(shí)現(xiàn)百度指數(shù)爬蟲的示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-02-02
Node.js API詳解之 vm模塊用法實(shí)例分析
這篇文章主要介紹了Node.js API詳解之 vm模塊用法,結(jié)合實(shí)例形式分析了Node.js API中vm模塊基本功能、函數(shù)、使用方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2020-05-05
優(yōu)化Node.js Web應(yīng)用運(yùn)行速度的10個(gè)技巧
這篇文章主要介紹了優(yōu)化Node.js Web應(yīng)用運(yùn)行速度的10個(gè)技巧,本文講解了從并行、異步、緩存、gzip 壓縮、客戶端渲染等等技巧,需要的朋友可以參考下2014-09-09
搭建基于express框架運(yùn)行環(huán)境的方法步驟
Express提供了一個(gè)輕量級模塊,把Node.js的http模塊功能封裝在一個(gè)簡單易用的接口中,這篇文章主要介紹了搭建基于express框架運(yùn)行環(huán)境的方法步驟,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11
Webpack 實(shí)現(xiàn) Node.js 代碼熱替換
Webpack有一個(gè)很實(shí)用的功能叫做熱替換(Hot-replace),尤其是結(jié)合React Hot Loader插件,開發(fā)過程中都不需要刷新瀏覽器,任何前端代碼的更改都會實(shí)時(shí)的在瀏覽器中表現(xiàn)出來。2015-10-10
詳解node登錄接口之密碼錯(cuò)誤限制次數(shù)(含代碼)
這篇文章主要介紹了nodejs登錄接口之密碼錯(cuò)誤限制次數(shù)(含代碼),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
node.js模擬實(shí)現(xiàn)自動發(fā)送郵件驗(yàn)證碼
這篇文章主要為大家介紹了node.js模擬實(shí)現(xiàn)自動發(fā)送郵件驗(yàn)證碼的實(shí)例過程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-04-04
Node.js五大應(yīng)用性能技巧小結(jié)(必須收藏)
本篇文章主要介紹了Node.js五大應(yīng)用性能技巧小結(jié)(必須收藏),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家2017-08-08

