亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

JS雙向鏈表實(shí)現(xiàn)與使用方法示例(增加一個(gè)previous屬性實(shí)現(xiàn))

 更新時(shí)間:2019年01月31日 14:12:13   作者:白楊-M  
這篇文章主要介紹了JS雙向鏈表實(shí)現(xiàn)與使用方法,在之前鏈表的基礎(chǔ)上增加一個(gè)previous屬性實(shí)現(xiàn)的雙向鏈表功能,需要的朋友可以參考下

本文實(shí)例講述了JS雙向鏈表實(shí)現(xiàn)與使用方法。分享給大家供大家參考,具體如下:

前面一篇講述了《JS基于對(duì)象的鏈表實(shí)現(xiàn)與使用方法》,這里的雙向鏈表通過(guò)增加一個(gè)previous屬性實(shí)現(xiàn)。

單鏈表中若需要查找某一個(gè)元素時(shí),必須從第一個(gè)元素開(kāi)始進(jìn)行查找,而雙向鏈表除開(kāi)頭節(jié)點(diǎn)和最后一個(gè)節(jié)點(diǎn)外每個(gè)節(jié)點(diǎn)中儲(chǔ)存有兩個(gè)指針,這連個(gè)指針?lè)謩e指向前一個(gè)節(jié)點(diǎn)的地址和后一個(gè)節(jié)點(diǎn)的地址,這樣無(wú)論通過(guò)那個(gè)節(jié)點(diǎn)都能夠?qū)ふ业狡渌墓?jié)點(diǎn)。

原理如下圖所示:

示例代碼:

/*雙向鏈表
* */
function Node(element) {
  this.element = element;
  this.next = null;
  this.previous = null;//雙向鏈表在這里需要增加一個(gè)previous屬性
}
function LList() {
  this.head = new Node("head");
  this.find = find;
  this.insert = insert;
  this.display = display;
  this.remove = remove;
  this.findLast = findLast;
  this.dispReverse = dispReverse;//將鏈表反轉(zhuǎn)
}
function dispReverse() {
  var currNode = this.head;
  currNode = this.findLast();
  var nodestr = "";
  while (!(currNode.previous == null)) {
    nodestr += " "+currNode.element;
    currNode = currNode.previous;
  }
  console.log("將鏈表反轉(zhuǎn)后: "+nodestr);
}
function findLast() {
  var currNode = this.head;
  while (!(currNode.next == null)) {
    currNode = currNode.next;
  }
  return currNode;
}
function remove(item) {
  var currNode = this.find(item);
  if (!(currNode.next == null)) {
    currNode.previous.next = currNode.next;
    currNode.next.previous = currNode.previous;
    currNode.next = null;
    currNode.previous = null;
  }
}
// findPrevious is no longer needed
/*function findPrevious(item) {
 var currNode = this.head;
 while (!(currNode.next == null) &&
 (currNode.next.element != item)) {
 currNode = currNode.next;
 }
 return currNode;
 }*/
function display() {
  var currNode = this.head;
  var nodestr = "";
  while (!(currNode.next == null)) {
    nodestr += " "+currNode.next.element;
    currNode = currNode.next;
  }
  console.log(nodestr);
}
function find(item) {
  var currNode = this.head;
  while (currNode.element != item) {
    currNode = currNode.next;
  }
  return currNode;
}
function insert(newElement, item) {
  var newNode = new Node(newElement);
  var current = this.find(item);
  newNode.next = current.next;
  newNode.previous = current;//雙向鏈表在這里需要設(shè)置新節(jié)點(diǎn)previous屬性
  current.next = newNode;
}
var cities = new LList();
cities.insert("Conway", "head");
cities.insert("Russellville", "Conway");
cities.insert("Carlisle", "Russellville");
cities.insert("Alma", "Carlisle");
cities.display();//Conway Russellville Carlisle Alma
cities.remove("Carlisle");
cities.display();//Conway Russellville Alma
cities.dispReverse();// Alma Russellville Conway

這里使用在線HTML/CSS/JavaScript代碼運(yùn)行工具http://tools.jb51.net/code/HtmlJsRun測(cè)試上述代碼,可得如下運(yùn)行結(jié)果:

更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)》、《JavaScript排序算法總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》、《JavaScript查找算法技巧總結(jié)》及《JavaScript錯(cuò)誤與調(diào)試技巧總結(jié)

希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • Javascript基礎(chǔ)教程之比較null和undefined值

    Javascript基礎(chǔ)教程之比較null和undefined值

    這篇文章主要介紹了Javascript基礎(chǔ)教程之比較null和undefined值的相關(guān)知識(shí),非常不錯(cuò),具有參考借鑒價(jià)值,感興趣的朋友一起學(xué)習(xí)吧
    2016-05-05
  • 使用getBoundingClientRect方法實(shí)現(xiàn)簡(jiǎn)潔的sticky組件的方法

    使用getBoundingClientRect方法實(shí)現(xiàn)簡(jiǎn)潔的sticky組件的方法

    本文介紹這種組件的實(shí)現(xiàn)思路,并提供一個(gè)同時(shí)支持將sticky元素固定在頂部或底部的具體實(shí)現(xiàn),由于這種組件在網(wǎng)站中非常常見(jiàn),所以有必要掌握它的實(shí)現(xiàn)方式,以便在有需要的時(shí)候基于它的思路寫出功能更多的組件出來(lái)
    2016-03-03
  • 基于JavaScript實(shí)現(xiàn)數(shù)碼時(shí)鐘效果

    基于JavaScript實(shí)現(xiàn)數(shù)碼時(shí)鐘效果

    這篇文章主要為大家詳細(xì)介紹了基于JavaScript實(shí)現(xiàn)數(shù)碼時(shí)鐘效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • js實(shí)現(xiàn)無(wú)縫輪播圖效果

    js實(shí)現(xiàn)無(wú)縫輪播圖效果

    這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)無(wú)縫輪播圖效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-03-03
  • 分享11個(gè)常用JavaScript小技巧

    分享11個(gè)常用JavaScript小技巧

    在我們的日常開(kāi)發(fā)過(guò)程中,我們經(jīng)常會(huì)遇到數(shù)字與字符串轉(zhuǎn)換,檢查對(duì)象中是否存在對(duì)應(yīng)值,條件性操作對(duì)象數(shù)據(jù),過(guò)濾數(shù)組中的錯(cuò)誤值,等等這類處理。本文整理出了一些常用的小技巧,希望大家能喜歡
    2022-06-06
  • 微信小程序?qū)崿F(xiàn)簡(jiǎn)易計(jì)算器功能

    微信小程序?qū)崿F(xiàn)簡(jiǎn)易計(jì)算器功能

    這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)簡(jiǎn)易計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-09-09
  • 最新評(píng)論