JavaScript數(shù)據(jù)結(jié)構(gòu)之鏈表的示例分析

這篇文章主要為大家展示了“JavaScript數(shù)據(jù)結(jié)構(gòu)之鏈表的示例分析”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“JavaScript數(shù)據(jù)結(jié)構(gòu)之鏈表的示例分析”這篇文章吧。

成都創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),衢州企業(yè)網(wǎng)站建設(shè),衢州品牌網(wǎng)站建設(shè),網(wǎng)站定制,衢州網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,衢州網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。

鏈表的概念

鏈表是一種常見的數(shù)據(jù)結(jié)構(gòu)。它是動(dòng)態(tài)地進(jìn)行存儲分配的一種結(jié)構(gòu)。鏈表有一個(gè)“頭指針”變量,以head表示,它存放一個(gè)地址,指向一個(gè)元素。每個(gè)結(jié)點(diǎn)都使用一個(gè)對象的引用指標(biāo)它的后繼,指向另一個(gè)結(jié)點(diǎn)的引用叫做鏈。

JavaScript數(shù)據(jù)結(jié)構(gòu)之鏈表的示例分析

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

JavaScript數(shù)據(jù)結(jié)構(gòu)之鏈表的示例分析

刪除過程:

JavaScript數(shù)據(jù)結(jié)構(gòu)之鏈表的示例分析

基于對象的鏈表

我們定義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)插入簡單四步:

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&&currentNode.next!=null){
         currentNode = currentNode.next;
     }   
     return currentNode;
}

簡答三步:

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;
     }
}

測試程序:

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());

輸出:

JavaScript數(shù)據(jù)結(jié)構(gòu)之鏈表的示例分析

雙向鏈表

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

JavaScript數(shù)據(jù)結(jié)構(gòu)之鏈表的示例分析

首先我們先給Node類增加front屬性:

function Node(element){
  this.element = element;
  this.next = null;
   this.front = null;
 }

當(dāng)然,對應(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;
}

測試程序:

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());

輸出:

JavaScript數(shù)據(jù)結(jié)構(gòu)之鏈表的示例分析

循環(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

這種行為會傳導(dǎo)至鏈表中的每個(gè)節(jié)點(diǎn),使得每個(gè)節(jié)點(diǎn)的 next 屬性都指向鏈表的頭節(jié)點(diǎn)。樓主用下圖來表示循環(huán)鏈表:

JavaScript數(shù)據(jù)結(jié)構(gòu)之鏈表的示例分析

修改構(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á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&&currentNode.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;
}

測試程序:

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é)果:

JavaScript數(shù)據(jù)結(jié)構(gòu)之鏈表的示例分析

以上是“JavaScript數(shù)據(jù)結(jié)構(gòu)之鏈表的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

網(wǎng)頁標(biāo)題:JavaScript數(shù)據(jù)結(jié)構(gòu)之鏈表的示例分析
標(biāo)題網(wǎng)址:http://muchs.cn/article34/pisise.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、微信公眾號、網(wǎng)頁設(shè)計(jì)公司、網(wǎng)站策劃、網(wǎng)站設(shè)計(jì)公司云服務(wù)器

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)