<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
        問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
        當前位置: 首頁 - 科技 - 知識百科 - 正文

        JavaScript數據結構之單鏈表和循環鏈表

        來源:懂視網 責編:小采 時間:2020-11-27 22:24:40
        文檔

        JavaScript數據結構之單鏈表和循環鏈表

        JavaScript數據結構之單鏈表和循環鏈表:數據結構系列前言: 數據結構作為程序員的基本知識,需要我們每個人牢牢掌握。近期我也展開了對數據結構的二次學習,來彌補當年挖的坑。 當時上課的時候也就是跟著聽課,沒有親自實現任何一種數據結構,更別提利用數據結構來解決問題了。 現在就來
        推薦度:
        導讀JavaScript數據結構之單鏈表和循環鏈表:數據結構系列前言: 數據結構作為程序員的基本知識,需要我們每個人牢牢掌握。近期我也展開了對數據結構的二次學習,來彌補當年挖的坑。 當時上課的時候也就是跟著聽課,沒有親自實現任何一種數據結構,更別提利用數據結構來解決問題了。 現在就來

        數據結構系列前言:

        數據結構作為程序員的基本知識,需要我們每個人牢牢掌握。近期我也展開了對數據結構的二次學習,來彌補當年挖的坑。。。   當時上課的時候也就是跟著聽課,沒有親自實現任何一種數據結構,更別提利用數據結構來解決問題了。  現在就來填坑了奮斗   在這里提醒看到我博客的孩子們,如果你還是在校生,永遠不要輕視任何一門基礎課的學習,這個時候挖的坑,要么需要用雙倍的努力去填,要么會直接影響一個人的能力等等。。。 千萬別給自己挖坑

        進入正題,關于鏈表的數據結構知識,這里簡單介紹下:

        鏈表是一種物理存儲單元上非線性、非連續性的數據結構(它在數據邏輯上是線性的),它的每個節點由兩個域組成:數據域和指針域。數據域中存儲實際數據,指針域則存儲著指針信息,指向鏈表中的下一個元素或者上一個元素。正是由于指針的存在,鏈表的存儲在物理單元是非連續性的。

        鏈表的優點和缺點同樣明顯。和線性表相比,鏈表在添加和刪除節點上的效率更高,因為其只需要修改指針信息即可完成操作,而不像線性表(數組)那樣需要移動元素。同樣的,鏈表的長度在理論上也是無限的(在存儲器容量范圍內),并可以動態變化長度,相比線性表優勢很大。 相應的,由于線性表無法隨機訪問節點,只能通過指針順著鏈表進行遍歷查詢來訪問,故其訪問數據元素的效率比較低。 

        下面是JS部分

        這里面封裝了的常用方法及描述:

        方法 描述
        append(element)   向鏈表尾部添加結點element
        insert(position,element)  向位置position處插入結點element
        removeAt(position)  按照索引值position刪除結點
        remove(element)  搜索并刪除給定結點element
        remove()  刪除鏈表中最后一個結點
        indexOf(element) 查找并返回給定結點element的索引值
        isEmpty()  判斷鏈表是否為空
        size()  獲取鏈表長度
        toString()  轉換為字符串輸出
        getHead() 獲取頭結點
        getTail()  獲取尾結點

        對于各常用方法的算法描述在這里就不寫了,相信大家都可以輕易讀懂并理解,畢竟都是非常基礎的知識了。

        單鏈表:

        function LinkedList(){ 
         /*節點定義*/ 
         var Node = function(element){ 
         this.element = element; //存放節點內容 
         this.next = null; //指針 
         } 
         
         var length = 0, //存放鏈表長度 
         head = null; //頭指針 
         
         this.append = function(element){ 
         var node = new Node(element), 
         current; //操作所用指針 
         
         if (!head){ 
         head = node; 
         }else { 
         current = head; 
         
         while(current.next){ 
         current = current.next; 
         } 
         
         current.next = node; 
         } 
         
         length++; 
         return true; 
         }; 
         
         this.insert = function(position, element){ 
         if (position >= 0 && position <= length) { 
         var node = new Node(element), 
         current = head, 
         previous, 
         index = 0; 
         
         if(position === 0){ 
         node.next = current; 
         head = node; 
         }else{ 
         while(index++ < position){ 
         previous = current; 
         current = current.next; 
         } 
         node.next = current; 
         previous.next = node; 
         } 
         
         length++; 
         return true; 
         }else{ 
         return false; 
         } 
         }; 
         
         this.removeAt = function(position){ 
         if(position > -1 && position < length){ 
         var current = head, 
         previous, 
         index = 0; 
         
         if (position === 0) { 
         
         head = current.next; 
         
         }else{ 
         
         while (index++ < position){ 
         previous = current; 
         current = current.next; 
         } 
         
         previous.next = current.next; 
         }; 
         
         length--; 
         return current.element; 
         }else{ 
         return null; 
         } 
         }; 
         
         this.remove = function(element){ 
         var current = head, 
         previous; 
         
         if(element === current.element){ 
         head = current.next; 
         length--; 
         return true; 
         } 
         previous = current; 
         current = current.next; 
         
         while(current){ 
         if(element === current.element){ 
         previous.next = current.next; 
         length--; 
         return true; 
         }else{ 
         previous = current; 
         current = current.next; 
         } 
         } 
         return false; 
         }; 
         
         this.remove = function(){ 
         if(length < 1){ 
         return false; 
         } 
         
         var current = head, 
         previous; 
         
         if(length == 1){ 
         head = null; 
         length--; 
         return current.element; 
         } 
         
         
         while(current.next !== null){ 
         previous = current; 
         current = current.next; 
         } 
         
         previous.next = null; 
         length--; 
         return current.element; 
         }; 
         
         this.indexOf = function(element){ 
         var current = head, 
         index = 0; 
         
         while(current){ 
         if(element === current.element){ 
         return index; 
         } 
         index++; 
         current = current.next; 
         } 
         
         return false; 
         }; 
         
         this.isEmpty = function(){ 
         return length === 0; 
         }; 
         
         this.size = function(){ 
         return length; 
         }; 
         
         this.toString = function(){ 
         var current = head, 
         string = ''; 
         
         while(current){ 
         string += current.element; 
         current = current.next; 
         } 
         return string; 
         }; 
         
         this.getHead = function(){ 
         return head; 
         } 
         
        } 
        

        循環鏈表:在單鏈表的基礎上,將尾節點的指針指向頭結點,就構成了一個循環鏈表。環形鏈表從任意一個節點開始,都可以遍歷整個鏈表。

        function CircularLinkedList(){ 
         var Node = function(element){ 
         this.element = element; 
         this.next = null; 
         } 
         
         var length = 0, 
         head = null; 
         
         this.append = function(element){ 
         var node = new Node(element), 
         current; 
         
         if (!head) { 
         head = node; 
         node.next = head; 
         }else{ 
         current = head; 
         
         while(current.next !== head){ 
         current = current.next; 
         } 
         
         current.next = node; 
         node.next = head; 
         }; 
         
         length++; 
         return true; 
         }; 
         
         this.insert = function(position, element){ 
         if(position > -1 && position < length){ 
         var node = new Node(element), 
         index = 0, 
         current = head, 
         previous; 
         
         
         if (position === 0) { 
         
         node.next = head; 
         head = node; 
         
         }else{ 
         
         while(index++ < position){ 
         previous = current; 
         current = current.next; 
         } 
         
         previous.next = node; 
         node.next = current; 
         
         }; 
         
         length++; 
         return true; 
         }else{ 
         return false; 
         } 
         }; 
         
         this.removeAt = function(position){ 
         if(position > -1 && position < length){ 
         var current = head, 
         previous, 
         index = 0; 
         
         if (position === 0) { 
         
         head = current.next; 
         
         }else{ 
         
         while (index++ < position){ 
         previous = current; 
         current = current.next; 
         } 
         
         previous.next = current.next; 
         }; 
         
         length--; 
         return current.element; 
         }else{ 
         return null; 
         } 
         }; 
         
         this.remove = function (element){ 
         var current = head, 
         previous, 
         indexCheck = 0; 
         
         while(current && indexCheck < length){ 
         if(current.element === element){ 
         if(indexCheck == 0){ 
         head = current.next; 
         length--; 
         return true; 
         }else{ 
         previous.next = current.next; 
         length--; 
         return true; 
         } 
         }else{ 
         previous = current; 
         current = current.next; 
         indexCheck++; 
         } 
         } 
         return false; 
         }; 
         
         this.remove = function(){ 
         if(length === 0){ 
         return false; 
         } 
         
         var current = head, 
         previous, 
         indexCheck = 0; 
         
         if(length === 1){ 
         head = null; 
         length--; 
         return current.element; 
         } 
         
         while(indexCheck++ < length){ 
         previous = current; 
         current = current.next; 
         } 
         previous.next = head; 
         length--; 
         return current.element; 
         }; 
         
         this.indexOf = function(element){ 
         var current = head, 
         index = 0; 
         
         while(current && index < length){ 
         if(current.element === element){ 
         return index; 
         }else{ 
         index++; 
         current = current.next; 
         } 
         } 
         return false; 
         }; 
         
         
         this.isEmpty = function(){ 
         return length === 0; 
         }; 
         
         this.size = function(){ 
         return length; 
         }; 
         
         this.toString = function(){ 
         var current = head, 
         string = '', 
         indexCheck = 0; 
         
         while(current && indexCheck < length){ 
         string += current.element; 
         current = current.next; 
         indexCheck++; 
         } 
         
         return string; 
         }; 
         
        } 
        

        使用方法:

        在類外部擴充方法:

        聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

        JavaScript數據結構之單鏈表和循環鏈表

        JavaScript數據結構之單鏈表和循環鏈表:數據結構系列前言: 數據結構作為程序員的基本知識,需要我們每個人牢牢掌握。近期我也展開了對數據結構的二次學習,來彌補當年挖的坑。 當時上課的時候也就是跟著聽課,沒有親自實現任何一種數據結構,更別提利用數據結構來解決問題了。 現在就來
        推薦度:
        • 熱門焦點

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: 久久亚洲精品无码av| 亚洲国产中文在线视频 | 日韩精品一区二区亚洲AV观看| 免费人成网上在线观看| 亚洲成网777777国产精品| 免费播放国产性色生活片| 久久久久久久亚洲精品| 中文字幕无码毛片免费看| 久久久久亚洲精品影视| 99久久精品国产免费| 亚洲伊人精品综合在合线| 午夜成年女人毛片免费观看| 亚洲国产精品成人午夜在线观看| 午夜免费福利影院| 日产久久强奸免费的看| 亚洲AV永久青草无码精品| 亚洲第一网站免费视频| 久久久久久亚洲精品影院| 免费日本黄色网址| 国产猛男猛女超爽免费视频| 亚洲国产视频一区| 免费在线观看日韩| 免费网站看av片| 亚洲色偷偷色噜噜狠狠99| 亚洲国产一区二区三区| 久久久久久久岛国免费播放| 亚洲最大中文字幕无码网站 | 成年免费大片黄在线观看com| 国产亚洲精品拍拍拍拍拍| 久久久精品2019免费观看| 亚洲sm另类一区二区三区| 亚洲中文字幕无码一区二区三区| 最近免费中文字幕高清大全| 免费亚洲视频在线观看| 亚洲va久久久噜噜噜久久狠狠 | 亚洲人午夜射精精品日韩| 免费A级毛片无码A∨| 羞羞视频免费网站日本| 亚洲国产精品日韩在线观看| 亚洲国产电影av在线网址| 五月亭亭免费高清在线|