這篇文章主要為大家展示了“JavaScript數(shù)據(jù)結(jié)構(gòu)之鏈表的示例分析”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“JavaScript數(shù)據(jù)結(jié)構(gòu)之鏈表的示例分析”這篇文章吧。
在昌平等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站制作、成都網(wǎng)站建設(shè) 網(wǎng)站設(shè)計(jì)制作定制網(wǎng)站,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站制作,成都營(yíng)銷網(wǎng)站建設(shè),外貿(mào)網(wǎng)站制作,昌平網(wǎng)站建設(shè)費(fèi)用合理。鏈表的概念
鏈表是一種常見的數(shù)據(jù)結(jié)構(gòu)。它是動(dòng)態(tài)地進(jìn)行存儲(chǔ)分配的一種結(jié)構(gòu)。鏈表有一個(gè)“頭指針”變量,以head表示,它存放一個(gè)地址,指向一個(gè)元素。每個(gè)結(jié)點(diǎn)都使用一個(gè)對(duì)象的引用指標(biāo)它的后繼,指向另一個(gè)結(jié)點(diǎn)的引用叫做鏈。

數(shù)組元素依靠下標(biāo)(位置)來進(jìn)行引用,而鏈表元素則是靠相互之間的關(guān)系來進(jìn)行引用。因此鏈表的插入效率很高,下圖演示了鏈表結(jié)點(diǎn)d的插入過程:

刪除過程:

基于對(duì)象的鏈表
我們定義2個(gè)類,Node類與LinkedList類,Node為結(jié)點(diǎn)數(shù)據(jù),LinkedList保存操作鏈表的方法。
首先看Node類:
function Node(element){
this.element = element;
this.next = null;
}element用來保存結(jié)點(diǎn)上的數(shù)據(jù),next用來保存指向一下結(jié)點(diǎn)的的鏈接。
LinkedList類:
function LinkedList(){
this.head = new Node('head');
this.find = find;
this.insert = insert;
this.remove = remove;
this.show = show;
}find()方法,從頭結(jié)點(diǎn)開始,沿著鏈表結(jié)點(diǎn)一直查找,直到找到與item內(nèi)容相等的element則返回該結(jié)點(diǎn),沒找到則返回空。
function find(item){
var currentNode = this.head;//從頭結(jié)點(diǎn)開始
while(currentNode.element!=item){
currentNode = currentNode.next;
}
return currentNode;//找到返回結(jié)點(diǎn)數(shù)據(jù),沒找到返回null
}Insert方法。通過前面元素插入的演示可以看出,實(shí)現(xiàn)插入簡(jiǎn)單四步:
1、創(chuàng)建結(jié)點(diǎn)
2、找到目標(biāo)結(jié)點(diǎn)
3、修改目標(biāo)結(jié)點(diǎn)的next指向鏈接
4、將目標(biāo)結(jié)點(diǎn)的next值賦值給要插入的結(jié)點(diǎn)的next
function insert(newElement,item){
var newNode = new Node(newElement);
var currentNode = this.find(item);
newNode.next = currentNode.next;
currentNode.next = newNode;
}Remove()方法。刪除某一節(jié)點(diǎn)需要先找到被刪除結(jié)點(diǎn)的前結(jié)點(diǎn),為此我們定義方法frontNode():
function frontNode(item){
var currentNode = this.head;
while(currentNode.next.element!=item&¤tNode.next!=null){
currentNode = currentNode.next;
}
return currentNode;
}簡(jiǎn)答三步:
1、創(chuàng)建結(jié)點(diǎn)
2、找到目標(biāo)結(jié)點(diǎn)的前結(jié)點(diǎn)
3、修改前結(jié)點(diǎn)的next指向被刪除結(jié)點(diǎn)的n后一個(gè)結(jié)點(diǎn)
function remove(item){
var frontNode = this.frontNode(item);
//console.log(frontNode.element);
frontNode.next = frontNode.next.next;
}Show()方法:
function show(){
var currentNode = this.head,result;
while(currentNode.next!=null){
result += currentNode.next.element;//為了不顯示head結(jié)點(diǎn)
currentNode = currentNode.next;
}
}測(cè)試程序:
var list = new LinkedList();
list.insert("a","head");
list.insert("b","a");
list.insert("c","b");
console.log(list.show());
list.remove("b");
console.log(list.show());輸出:

雙向鏈表
從鏈表的頭節(jié)點(diǎn)遍歷到尾節(jié)點(diǎn)很簡(jiǎn)單,但有的時(shí)候,我們需要從后向前遍。此時(shí)我們可以通過給 Node 對(duì)象增加一個(gè)屬性,該屬性存儲(chǔ)指向前驅(qū)節(jié)點(diǎn)的鏈接。樓主用下圖來雙向鏈表的工作原理。

首先我們先給Node類增加front屬性:
function Node(element){
this.element = element;
this.next = null;
this.front = null;
}當(dāng)然,對(duì)應(yīng)的insert()方法和remove()方法我們也需要做相應(yīng)的修改:
function insert(newElement,item){
var newNode = new Node(newElement);
var currentNode = this.find(item);
newNode.next = currentNode.next;
newNode.front = currentNode;//增加front指向前驅(qū)結(jié)點(diǎn)
currentNode.next = newNode;
}
function remove(item){
var currentNode = this.find(item);//找到需要?jiǎng)h除的節(jié)點(diǎn)
if (currentNode.next != null) {
currentNode.front.next = currentNode.next;//讓前驅(qū)節(jié)點(diǎn)指向需要?jiǎng)h除的節(jié)點(diǎn)的下一個(gè)節(jié)點(diǎn)
currentNode.next.front = currentNode.front;//讓后繼節(jié)點(diǎn)指向需要?jiǎng)h除的節(jié)點(diǎn)的上一個(gè)節(jié)點(diǎn)
currentNode.next = null;//并設(shè)置前驅(qū)與后繼的指向?yàn)榭? currentNode.front = null;
}
}反序顯示鏈表:
需要給雙向鏈表增加一個(gè)方法,用來查找最后的節(jié)點(diǎn)。 findLast() 方法找出了鏈表中的最后一個(gè)節(jié)點(diǎn),可以免除從前往后遍歷鏈。
function findLast() {//查找鏈表的最后一個(gè)節(jié)點(diǎn)
var currentNode = this.head;
while (currentNode.next != null) {
currentNode = currentNode.next;
}
return currentNode;
}實(shí)現(xiàn)反序輸出:
function showReverse() {
var currentNode = this.head, result = "";
currentNode = this.findLast();
while(currentNode.front!=null){
result += currentNode.element + " ";
currentNode = currentNode.front;
}
return result;
}測(cè)試程序:
var list = new LinkedList();
list.insert("a","head");
list.insert("b","a");
list.insert("c","b");
console.log(list);
list.remove("b");
console.log(list.show());
console.log(list.showReverse());輸出:

循環(huán)鏈表
循環(huán)鏈表是另一種形式的鏈?zhǔn)酱尜A結(jié)構(gòu)。它的特點(diǎn)是表中最后一個(gè)結(jié)點(diǎn)的指針域指向頭結(jié)點(diǎn),整個(gè)鏈表形成一個(gè)環(huán)。循環(huán)鏈表和單向鏈表相似,節(jié)點(diǎn)類型都是一樣的。唯一的區(qū)別是,在創(chuàng)建循環(huán)鏈表時(shí),讓其頭節(jié)點(diǎn)的 next 屬性指向它本身,即:
head.next = head
這種行為會(huì)傳導(dǎo)至鏈表中的每個(gè)節(jié)點(diǎn),使得每個(gè)節(jié)點(diǎn)的 next 屬性都指向鏈表的頭節(jié)點(diǎn)。樓主用下圖來表示循環(huán)鏈表:

修改構(gòu)造方法:
function LinkedList(){
this.head = new Node('head');//初始化
this.head.next = this.head;//直接將頭節(jié)點(diǎn)的next指向頭節(jié)點(diǎn)形成循環(huán)鏈表
this.find = find;
this.frontNode = frontNode;
this.insert = insert;
this.remove = remove;
this.show = show;
}這時(shí)需要注意鏈表的輸出方法show()與find()方法,原來的方式在循環(huán)鏈表里會(huì)陷入死循環(huán),while循環(huán)的循環(huán)條件需要修改為當(dāng)循環(huán)到頭節(jié)點(diǎn)時(shí)退出循環(huán)。
function find(item){
var currentNode = this.head;//從頭結(jié)點(diǎn)開始
while(currentNode.element!=item&¤tNode.next.element!='head'){
currentNode = currentNode.next;
}
return currentNode;//找到返回結(jié)點(diǎn)數(shù)據(jù),沒找到返回null
}
function show(){
var currentNode = this.head,result = "";
while (currentNode.next != null && currentNode.next.element != "head") {
result += currentNode.next.element + " ";
currentNode = currentNode.next;
}
return result;
}測(cè)試程序:
var list = new LinkedList();
list.insert("a","head");
list.insert("b","a");
list.insert("c","b");
console.log(list.show());
list.remove("b");
console.log(list.show());測(cè)試結(jié)果:

以上是“JavaScript數(shù)據(jù)結(jié)構(gòu)之鏈表的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司行業(yè)資訊頻道!
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站chinadenli.net,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
網(wǎng)頁標(biāo)題:JavaScript數(shù)據(jù)結(jié)構(gòu)之鏈表的示例分析-創(chuàng)新互聯(lián)
轉(zhuǎn)載來于:http://chinadenli.net/article34/dhcpse.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)公司、網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)、營(yíng)銷型網(wǎng)站建設(shè)、網(wǎng)站制作、網(wǎng)站排名
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)