資料結構JavaScript——雙向連結串列、雙向迴圈連結串列
關於連結串列簡介、單鏈表、單向迴圈連結串列、JS中的使用以及擴充方法: 單鏈表、迴圈連結串列的JS實現
雙向連結串列:單向連結串列只能向著一個方向遍歷連結串列節點,而在節點指標域中增加了前向指標的雙向連結串列,則可以向著兩個方向遍歷節點。這使得雙向連結串列也可以在任何一個節點遍歷整個連結串列。
function DoublyLinkedList() { var Node = function(element) { this.element = element; this.next = null; this.prev = null; }; var length = 0, head = null, tail = null; this.append = function(element){ var node = Node(element), current, previous; if(!head){ head = node; tail = node; }else{ current = head; while(current){ previous = current; current = current.next; } node.next = current; current.prev = node; previous.next = node; node.prev = previous; } length++; return true; } this.insert = function(position,element){ if(position > -1 && position < length){ var node = new Node(element), current = head, previous, index = 0; if(position === 0){ if(!head){ head = node; tail = node; }else{ node.next = current; current.prev = node; head = node; } }else if (position === length -1){ current = tail; current.next = node; node.prev = current; }else { while(index++ < position){ previous = current; current = current.next; } node.next = current; previous.next = node; current.prev = node; node.prev = previous; } length++; return true; }else{ return false; } }; this.removeAt = function(position){ if(position > -1 && position < length){ var current = head, index = 0, previous; if (position === 0) { head = current.next; if(length === 1){ tail = null; }else{ head.prev = null; } }else if(position === length - 1){ current = tail; tail = current.prev; tail.next = null; } else{ while(index++ < position){ previous = current; current = current.next; } previous.next = current.next; current.next.prev = previous; }; length-- ; return current.element; }else{ return false; } }; this.remove = function(element){ var current = head, previous; if(current.element === element){ head = current.next; } previous = current; current = current.next; while(current){ if (current.element = element) { previous.next = current.next; current.next.prev = previous; }else{ previous = current; current = current.next; } } return false; }; this.remove = function(){ if (length === 0) { return false; }; var current = head, previous; if(length === 1){ head = null; tail = null; length--; return current.element; } while(current){ previous = current; current = current.next; } previous.next = null; length--; return current.element; }; this.indexOf = function(element){ var current = head, index = 0; while(current && index++ < length){ if (current.element === element) { return 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; }; this.getTail = function(){ return tail; }; }
雙向迴圈連結串列:將雙向連結串列的頭尾指標相連,就構成了雙向迴圈連結串列。這種連結串列從任意一個節點都可以同時向兩個方向進行節點遍歷,查詢節點的速度也是最快的。
/*雙向迴圈連結串列*/ function DoublyCircularLinkedList(){ var Node = function(element){ this.element = element; this.next = null; this.prev = null; }; var length = 0, head = null, tail = null; this.append = function(element){ var node = new Node(element), current, previous; if (!head) { head = node; tail = node; head.prev = tail; tail.next = head; }else{ current = head; while(current.next !== head){ previous = current; current = current.next; } current.next = node; node.next = head; node.prev = current; }; length++; return true; }; this.insert = function(position, element){ if(position >= 0 && position <= length){ var node = new Node(element), index = 0, current = head, previous; if(position === 0){ if(!head){ node.next = node; node.tail = node; head = node; tail = node; }else{ current.prev = node; node.next = current; head = node; node.prev = tail; } }else if(position === length){ current = tail; current.next = node; node.prev = current; tail = node; node.next = head; }else{ while(index++ < position){ previous = current; current = current.next; } current.prev = node; node.next = current; previous.next = node; node.prev = previous; } length++; return true; }else{ return false; } }; this.removeAt = function(position){ if(position > -1 && position < length){ var current = head, index = 0, previous; if(position === 0){ current.next.previous = tail; head = current.next; }else if(position === length - 1){ current = tail; current.prev.next = head; head.prev = current.prev; tail = current.prev; }else{ while(index++ < position){ previous = current; current = current.next; } previous.next = current.next; current.next.prev = previous; } length--; return true; }else{ return false; } }; this.remove = function(element){ var current = head, previous, indexCheck = 0; while(current && indexCheck < length){ if(current.element === element){ if(indexCheck === 0){ current.next.prev = tail; head = current.next; }else{ current.next.prev = previous; previous.next = current.next; } length--; return true; } 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; tail = null; length--; return current.element; } while(indexCheck++ < length){ previous = current; current = current.next; } previous.next = head; tail = previous.next; length--; return current.element; }; this.indexOf = function(element){ var current = head, index = 0; while(current && index++ < length){ if(current.element === element){ return index; } current = current.next; } return false; }; this.toString = function(){ var current = head, indexCheck = 0, string = ''; while(current && indexCheck < length){ string += current.element; indexCheck++; current = current.next; } return string; }; this.isEmpty = function(){ return length === 0; }; this.getHead = function(){ return head; }; this.getTail = function(){ return tail; }; this.size = function(){ return length; }; }
相關推薦
資料結構作業程式碼儲存-2.1 單向迴圈連結串列的建立,插入和刪除,和指標移動
在此感謝我親愛的大神同學們!一個Z妹子講情了思路,一個M妹子提供了更高階的雙向迴圈連結串列的程式碼借鑑(雖然有些還是沒怎麼看懂。。。 我的作業就完成啦!儲存程式碼當個紀念! 1,往操作位置順時針的下一位插入一個元素,並將操作位置移到新元素上。 2,刪掉操作位置順時針方
資料結構學習心得——雙鏈表和迴圈連結串列
雙鏈表 若要尋查結點的前驅,則需要從表頭指標出發。為了克服單鏈表這種單向性的缺點,可以利用雙鏈表。顧名思義,在雙鏈表的結點中有兩個指標域,其一指向直接後繼,另一指向直接前驅。 迴圈連結串列 迴圈連結串列是另一種形式的鏈式儲存結構。它的特點是表中最後
資料結構JavaScript——雙向連結串列、雙向迴圈連結串列
關於連結串列簡介、單鏈表、單向迴圈連結串列、JS中的使用以及擴充方法: 單鏈表、迴圈連結串列的JS實現 雙向連結串列:單向連結串列只能向著一個方向遍歷連結串列節點,而在節點指標域中增加了前向指標的雙向連結串列,則可以向著兩個方向遍歷節點。這使得雙向連結串列也可以在任何
資料結構——線性表 (順序表、單鏈表、靜態連結串列、迴圈連結串列、雙向連結串列)
提示:以下內容不適合零基礎人員,僅供筆者複習之用。 一、線性結構的基本特徵: 1.集合中必存在唯一的一個“第一元素”; 2.集合中必存在唯一的一個 “最後元素”; 3.除最後元素在外,均有 唯一的後繼; 4.除第一元素之外,均有 唯一的前驅。 如:j
小白的資料結構程式碼實戰(2)----雙向連結串列的各種操作
//Author:張佳琪 #include <stdio.h> #include <stdlib.h> typedef int ElemType; typedef struct Node { ElemType data; struct
為什麼我要放棄javaScript資料結構與演算法(第五章)—— 連結串列
這一章你將會學會如何實現和使用連結串列這種動態的資料結構,這意味著我們可以從中任意新增或移除項,它會按需進行擴張。 本章內容 連結串列資料結構 向連結串列新增元素 從連結串列移除元素 使用 LinkedList 類 雙向連結串列 迴圈連結串列 第五章 連結串列 連結串列資
【資料結構】線性表的鏈式儲存連結串列的初始化、插入元素、刪除元素操作(三)
雙向連結串列的初始化插入與刪除 程式碼收穫 雙向連結串列刪除結點需要注意要刪除最後一個結點和不是最後一個結點分類討論。 插入和刪除時注意修改上一個結點裡指向下一個結點的指標與下一個結點裡指向上一個結點的指標。 #include <stdio.h>
資料結構實驗1— 線性表、連結串列的實現
約瑟夫環問題 實驗要求 問題描述 基本要求 測試資料 程式程式碼 執行結果 實現順序表各種基本操作 實驗要求 編寫程式實現順序表的各種基本運算,並在此基礎上設計一個主程式完成如下功能: (1)初始化順序表L; (2)依次在L
javascript資料結構與演算法筆記(五):連結串列
javascript資料結構與演算法筆記(五):連結串列 一:簡介 二:ES6版LinkedList類 一:簡介 連結串列儲存有序的元素集合,但不同於陣列,連結串列中的元素在記憶體中並不是連續放置的。每個 元素由一個儲
線性表(陣列、單鏈表、靜態連結串列、迴圈連結串列、雙向連結串列)
線性表的定義 線性表(List):零個或多個數據元素的有限序列。 有幾個地方需要強調: 首先它是一個序列,也就是說元素之間是有順序的,若元素存在多個,則第一個元素無前驅,最後一個元素無後繼,其他每個元素都有且只有一個前驅和後繼。 然後線性表強調的是有限的。 最
C# 演算法之連結串列、雙向連結串列以及正向反向遍歷實現
1、簡介 連結串列是一種非常基礎的資料結構之一,我們在日常開發種都會接觸到或者是接觸到相同型別的連結串列資料結構.所以本文會使用C#演算法來實現一個簡單的連結串列資料結構,並實現其中幾個簡單的api以供使用. 2、概述 連結串列是一種遞迴的資料結構,他或者為null,或者是指向像一個節點
資料結構實現 5.2:對映_基於連結串列實現(C++版)
資料結構實現 5.2:對映_基於連結串列實現(C++版) 1. 概念及基本框架 2. 基本操作程式實現 2.1 增加操作 2.2 刪除操作 2.3 修改操作 2.4 查詢操作 2.5 其他操作 3. 演
資料結構實現 4.2:集合_基於連結串列實現(C++版)
資料結構實現 4.2:集合_基於連結串列實現(C++版) 1. 概念及基本框架 2. 基本操作程式實現 2.1 增加操作 2.2 刪除操作 2.3 查詢操作 2.4 其他操作 3. 演算法複雜度分析
資料結構(一)之順序表與連結串列
順序表 運用陣列結構來構建的線性表就是順序表。 本例實現了順序表的列印、清空、判斷是否為空、求表長、獲得指定下標的元素、獲得指定元素的下標、插入和刪除操作。 #include<iostream> const int MAXSIZE=100; using
資料結構:單鏈表(三)輸出連結串列值最大的節點
/********************************************************* **************新增加功能:輸出連結串列中值的最大節點*********
資料結構的基本概念 與順序表和連結串列的區別
1.資料:就是符號 輸入到計算機被計算機加工處理的符號的集合 特點:輸入到計算機 可以被計算機加工處理 2.資料結構把資料分為:數值型別和非數值型別 3.資料元素:組成資料基本元素
(一)資料結構之線性表的簡單實現:連結串列
/* 查詢連結串列中第K個元素 */ List *FindKth( int K, List *PtrL ) { List *p = PtrL; int i = 1; while (p != NULL && i < K ) { p = p->Next; i++;
左程雲著演算法與資料結構題目最優解筆記-刪除無序連結串列中重複的元素
刪除無序連結串列中重複的元素 方法1:使用雜湊表,時間複雜度O(n),空間複雜度O(n) 方法2:類似選擇排序的過程,時間複雜度O(n^2),空間複雜度O(1); 方法1 過程如下: 1. 生成一個雜湊表,因為頭節點不用刪除,所以先將頭節點存入
C語言資料結構與演算法之深度、廣度優先搜尋
一、深度優先搜尋(Depth-First-Search 簡稱:DFS) 1.1 遍歷過程: (1)從圖中某個頂點v出發,訪問v。 (2)找出剛才第一個被頂點訪問的鄰接點。訪問該頂點。以這個頂點為新的頂點,重複此步驟,直到訪問過的頂點沒有未被訪問過的頂點為止。 (3)返回到
《OpenCV3程式設計入門》——4.2 OpenCV中常用資料結構和函式(Point、Scalar、Size、Rect、cvtColor)
目錄 1、點的表示:Point類 2、顏色的表示:Scalar類 3、尺寸的表示:Size類 4、矩形的表示:Rect類 5、顏色空間轉換:cvtColor()函式 1、點的表示:Point類 Point類資料結構表示了二維座標系下的點,即由影象座標x和y指定的2D點