欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

JavaScript基于對(duì)象的鏈表怎么定義

這篇文章主要介紹了JavaScript基于對(duì)象的鏈表怎么定義的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇JavaScript基于對(duì)象的鏈表怎么定義文章都會(huì)有所收獲,下面我們一起來(lái)看看吧。

創(chuàng)新互聯(lián)服務(wù)緊隨時(shí)代發(fā)展步伐,進(jìn)行技術(shù)革新和技術(shù)進(jìn)步,經(jīng)過(guò)十年的發(fā)展和積累,已經(jīng)匯集了一批資深網(wǎng)站策劃師、設(shè)計(jì)師、專業(yè)的網(wǎng)站實(shí)施團(tuán)隊(duì)以及高素質(zhì)售后服務(wù)人員,并且完全形成了一套成熟的業(yè)務(wù)流程,能夠完全依照客戶要求對(duì)網(wǎng)站進(jìn)行網(wǎng)站建設(shè)、成都網(wǎng)站制作、建設(shè)、維護(hù)、更新和改版,實(shí)現(xiàn)客戶網(wǎng)站對(duì)外宣傳展示的首要目的,并為客戶企業(yè)品牌互聯(lián)網(wǎng)化提供全面的解決方案。

鏈表的概念

鏈表是一種常見(jiàn)的數(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)的引用叫做鏈。

JavaScript基于對(duì)象的鏈表怎么定義

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

JavaScript基于對(duì)象的鏈表怎么定義

刪除過(guò)程:

JavaScript基于對(duì)象的鏈表怎么定義

基于對(duì)象的鏈表

我們定義2個(gè)類,Node類與LinkedList類,Node為結(jié)點(diǎn)數(shù)據(jù),LinkedList保存操作鏈表的方法。

首先看Node類:

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

element用來(lái)保存結(jié)點(diǎn)上的數(shù)據(jù),next用來(lái)保存指向一下結(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)開(kāi)始,沿著鏈表結(jié)點(diǎn)一直查找,直到找到與item內(nèi)容相等的element則返回該結(jié)點(diǎn),沒(méi)找到則返回空。

function find(item){
     var currentNode = this.head;//從頭結(jié)點(diǎn)開(kāi)始
     while(currentNode.element!=item){
         currentNode = currentNode.next;
     }
     return currentNode;//找到返回結(jié)點(diǎn)數(shù)據(jù),沒(méi)找到返回null
}

Insert方法。通過(guò)前面元素插入的演示可以看出,實(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&&currentNode.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());

輸出:

JavaScript基于對(duì)象的鏈表怎么定義

雙向鏈表

從鏈表的頭節(jié)點(diǎn)遍歷到尾節(jié)點(diǎn)很簡(jiǎn)單,但有的時(shí)候,我們需要從后向前遍。此時(shí)我們可以通過(guò)給 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è)方法,用來(lái)查找最后的節(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());

輸出:

JavaScript基于對(duì)象的鏈表怎么定義

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

修改構(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()方法,原來(lái)的方式在循環(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)開(kāi)始
  while(currentNode.element!=item&&currentNode.next.element!='head'){
    currentNode = currentNode.next;
  }
  return currentNode;//找到返回結(jié)點(diǎn)數(shù)據(jù),沒(méi)找到返回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基于對(duì)象的鏈表怎么定義

關(guān)于“JavaScript基于對(duì)象的鏈表怎么定義”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“JavaScript基于對(duì)象的鏈表怎么定義”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

分享標(biāo)題:JavaScript基于對(duì)象的鏈表怎么定義
鏈接URL:http://www.chinadenli.net/article26/piigjg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站網(wǎng)站設(shè)計(jì)品牌網(wǎng)站建設(shè)品牌網(wǎng)站制作網(wǎng)站營(yí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í)需注明來(lái)源: 創(chuàng)新互聯(lián)

綿陽(yáng)服務(wù)器托管