摘要:最近在看數(shù)據(jù)結(jié)構(gòu)與算法,但是一直搞不明白在代碼中的實(shí)現(xiàn)。今天結(jié)合找到的一些資料總結(jié)一下鏈表在中的實(shí)現(xiàn)。這種結(jié)構(gòu)允許在迭代期間有效地從序列中的任何位置插入或刪除元素。
最近在看js數(shù)據(jù)結(jié)構(gòu)與算法,但是一直搞不明白在代碼中的實(shí)現(xiàn)。今天結(jié)合找到的一些資料總結(jié)一下鏈表在js中的實(shí)現(xiàn)。
首先說下鏈表,在計算機(jī)科學(xué)中, 一個鏈表是數(shù)據(jù)元素的線性集合, 元素的線性順序不是由它們在內(nèi)存中的物理位置給出的。相反,每個元素指向下一個元素。它是由一組節(jié)點(diǎn)組成的數(shù)據(jù)結(jié)構(gòu),這些節(jié)點(diǎn)一起表示序列。在最簡單的形式下,每個節(jié)點(diǎn)由數(shù)據(jù)和到序列中下一個節(jié)點(diǎn)的引用(換句話說,鏈接)組成。這種結(jié)構(gòu)允許在迭代期間有效地從序列中的任何位置插入或刪除元素。關(guān)于鏈表更多的說明請參考鏈接描述
大致了解了鏈表的概念之后我們就看下鏈表在js代碼中的實(shí)現(xiàn):
單向鏈表
//這里element存放的是該節(jié)點(diǎn)上的數(shù)據(jù),next存放的是指向下一個節(jié)點(diǎn)的鏈接 function Node(element){ this.element = element; this.next = null; } //接下來構(gòu)建一個List類 function List(node){ this.node = new Node(node); //查找節(jié)點(diǎn) this.find = function(target){ var current = this.node; while(current.element != target){ current = current.next; if(!current){ return false; } } return current; }; //插入節(jié)點(diǎn) this.insert = function(newNode, target){ var newnode = new Node(newNode); var current = this.find(target); newnode.next = current.next; current.next = newnode; }; //查找前一個節(jié)點(diǎn) this.findPre = function(item){ var current = this.node; while(!current.next && current.next.element != item){ current = current.next; } return current; }; //刪除節(jié)點(diǎn) this.delete = function(target){ var deltarget = this.find(target); this.findPre(deltarget).next = deltarget.next; }; }
執(zhí)行查找節(jié)點(diǎn)操作,可見如下輸出:
執(zhí)行插入節(jié)點(diǎn)操作,可見:
執(zhí)行刪除節(jié)點(diǎn)操作,可見:
雙向鏈表
在計算機(jī)科學(xué)中, 一個雙向鏈表(doubly linked list) 是由一組稱為節(jié)點(diǎn)的順序鏈接記錄組成的鏈接數(shù)據(jù)結(jié)構(gòu)。每個節(jié)點(diǎn)包含兩個字段,稱為鏈接,它們是對節(jié)點(diǎn)序列中上一個節(jié)點(diǎn)和下一個節(jié)點(diǎn)的引用。開始節(jié)點(diǎn)和結(jié)束節(jié)點(diǎn)的上一個鏈接和下一個鏈接分別指向某種終止節(jié)點(diǎn),通常是前哨節(jié)點(diǎn)或null,以方便遍歷列表。如果只有一個前哨節(jié)點(diǎn),則列表通過前哨節(jié)點(diǎn)循環(huán)鏈接。它可以被概念化為兩個由相同數(shù)據(jù)項組成的單鏈表,但順序相反。
function Node(element){ this.pre = null; this.element = element; this.next = null; } function List(node){ this.node = new Node(node); this.find = function(target){ var current = this.node; while(current.element != target){ current = current.next; if(current == null){ return false; } } return current; }; this.insert = function(newNode, target){ var target = this.find(target); var newNode = new Node(newNode); newNode.next = target.next; newNode.pre = target; target.next = newNode; }; this.delete = function(delNode){ var delNode = this.find(delNode); delNode.next.pre = delNode.pre; delNode.pre.next = delNode.next; }; } var list = new List("a"); list.insert("b", "a"); list.insert("c", "b"); list.delete("b"); console.log(list);
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://m.hztianpu.com/yun/98898.html
摘要:在存儲多個元素時,我們最常用的數(shù)據(jù)結(jié)構(gòu)可能是數(shù)組,究其原因可能是數(shù)組訪問方便,可以直接通過訪問,但是數(shù)組也存在一定的缺點(diǎn),數(shù)組的大小是固定,數(shù)組在執(zhí)行插入或者刪除的時候成本很高。 在存儲多個元素時,我們最常用的數(shù)據(jù)結(jié)構(gòu)可能是數(shù)組,究其原因可能是數(shù)組訪問方便,可以直接通過[]訪問,但是數(shù)組也存在一定的缺點(diǎn),數(shù)組的大小是固定,數(shù)組在執(zhí)行插入或者刪除的時候成本很高。鏈表存儲的是有序的元素集合...
摘要:實(shí)現(xiàn)移除給定的元素要移除的元素返回值表示移除成功方法說明移除單向鏈表中某個位置的元素。的前端樂園原文鏈接寒假前端學(xué)習(xí)學(xué)習(xí)數(shù)據(jù)結(jié)構(gòu)與算法二鏈表 本系列的第一篇文章: 學(xué)習(xí)JavaScript數(shù)據(jù)結(jié)構(gòu)與算法(一),棧與隊列第二篇文章:學(xué)習(xí)JavaScript數(shù)據(jù)結(jié)構(gòu)與算法(二):鏈表第三篇文章:學(xué)習(xí)JavaScript數(shù)據(jù)結(jié)構(gòu)與算法(三):集合第四篇文章:學(xué)習(xí)JavaScript數(shù)據(jù)結(jié)構(gòu)與...
閱讀 2609·2023-04-25 14:22
閱讀 3911·2021-11-15 18:12
閱讀 1441·2019-08-30 15:44
閱讀 3381·2019-08-29 15:37
閱讀 903·2019-08-29 13:49
閱讀 3596·2019-08-26 12:11
閱讀 1075·2019-08-23 18:28
閱讀 1755·2019-08-23 14:55