本系列的第一篇文章: 學習JavaScript數據結構與算法(一),棧與隊列
第二篇文章:學習JavaScript數據結構與算法(二):鏈表
第三篇文章:學習JavaScript數據結構與算法(三):集合
第四篇文章:學習JavaScript數據結構與算法(四):二叉搜索樹前端
鏈表是一種常見的數據結構,也屬於線性表,但不會按線性的順序來儲存數據。而是在每個節點中,儲存了下一個節點的指針。能夠看圖理解。(有C語言基礎的可能比較好理解)。
使用鏈表結構能夠克服數組須要預先知道數據大小的缺點(C語言的數組須要預先定義長度),鏈表結構能夠充分利用計算機內存空間,實現靈活的內存動態管理。node
接下來就是介紹兩種常見的鏈表: 單向鏈表,雙向鏈表在JavaScript中的實現。git
鏈表中最簡單的形式就是單向鏈表,鏈表中的節點都包含兩個部分,第一部分儲存着自身信息,第二部分則儲存有指向下一節點的指針。最後一個節點則指向NULL
,如圖所示:github
首先,建立一個構造函數。算法
/** * 單向鏈表構造函數 */ function LinkedList() { /** * 單向鏈表中節點的構造函數 * @param {Any} element 要傳入鏈表的節點 */ var Node = function(element) { this.element = element; //下個節點的地址 this.next = null; } //單向鏈表的長度 var length = 0; //單向鏈表的頭結點,初始化爲NULL var head = null; }
不難看出,單向鏈表構造函數比棧與隊列要複雜許多。segmentfault
單向鏈表須要有以下的方法:數組
append(element): 添加元素到鏈表尾部數據結構
insert(position,element): 向單向鏈表中某個位置插入元素app
indexOf(element): 尋找某個元素在單向鏈表中的位置函數
remove(element): 移除給定的元素
removeAt(position): 移除單向鏈表中某個位置的元素
getHead(): 獲取單向鏈表的頭部
isAmpty(): 檢查單向鏈表是否爲空,爲空則返回true
toString(): 將鏈表全部內容以字符串輸出
size(): 返回單向鏈表長度
說明: 向單向鏈表尾部添加元素。
實現:
/** * 向單向鏈表尾部添加元素 * @param {Any} element 要加入鏈表的節點 */ this.append = function(element) { var node = new Node(element); var current; if (head == null) { head = node; } else { // 當前項等於鏈表頭部元素. // while循環到最後一個,從而將該節點加入鏈表尾部。 current = head; // 當next爲null時,斷定爲false。退出循環。 while (current.next) { current = current.next; } current.next = node; } length++; };
說明: 向單向鏈表中某個位置插入元素。
實現:
/** * 向單向鏈表中插入某個元素 * @param {Number} position 要插入的位置 * @param {Any} element 要插入的元素 * @return {Boolean} 插入成功返回true,失敗返回false */ this.insert = function(position, element) { if (position >= 0 && position <= length) { var node = new Node(element); var current = head; var previous; var index = 0; if (position == 0) { node.next = current; head = node; } else { while (index++ < position) { previous = current; current = current.next; } previous.next = node; node.next = current; } length++; return true; } else { return false; } };
說明:尋找某個元素在單向鏈表中的位置。
實現:
/** * 尋找某個元素在單向鏈表中的位置 * @param {Any} element 要尋找的元素 * @return {Number} 返回值>=0則表明找到相應位置 */ this.indexOf = function(element) { var current = head; var index = -1; while (current) { if (element === current.element) { return index; } index++; current = current.next; } return -1; };
說明: 移除給定的元素。
實現:
/** * 移除給定的元素 * @param {Any} element 要移除的元素 * @return {Number} 返回值>=0表示移除成功 */ this.remove = function(element) { var index = this.indexOf(element); return this.removeAt(index); };
說明:移除單向鏈表中某個位置的元素。
實現:
/** * 移除單向鏈表中某一個元素 * @param {Number} position 要移除元素的位置 * @return {Any} 移除成功返回被移除的元素,不成功則返回NULL */ this.removeAt = function(position) { if (position > -1 && position < length) { var current = head; var previous; var index = 0; if (position == 0) { // 由於以前head指向第一個元素,如今把head修改成指向第二個元素。 // 核心概念在於鏈表先後全靠指針連接,而非數組通常。 // 因此只須要改變head的元素。 head = current.next; } else { while (index++ < position) { // previous指要操做元素位置以前的那個元素,current表示以後的那個元素。 previous = current; current = current.next; } previous.next = current.next; } length--; return current.element; } else { return null; } };
說明:獲取單向鏈表的頭部。
實現:
/** * 獲取單向鏈表的頭部 * @return {Any} 單向鏈表的頭部 */ this.getHead = function() { return head; }
實現:
/** * 判斷單向鏈表是否爲空 * @return {Boolean} 爲空則返回true,不爲空則返回false */ this.isAmpty = function() { return length === 0 }; /** * 將鏈表全部內容以字符串輸出 * @return {String} 要輸出的字符串 */ this.toString = function() { var current = head; var string = ''; while (current) { string += current.element; current = current.next; } return string; }; /** * 返回單向鏈表長度 * @return {Number} 單向鏈表的長度 */ this.size = function() { return length; };
以上的就是單向鏈表在JavaScript中的實現,有興趣的同窗能夠本身下載源代碼查看。
雙向鏈表與單向鏈表非常相像。在單向鏈表中,只有指向下一個節點的連接。但在雙向鏈表中,還有指向上一個節點的連接,是雙向的。
如圖所示:
首先,依然是構造函數:
/** * 雙向鏈表的構造函數 */ function DoublyLinkedList() { /** * 雙向鏈表中節點的構造函數 * @param {Any} element 要傳入鏈表的元素 */ var Node = function(element) { this.element = element; this.prev = null; this.next = null; } //雙向鏈表的長度 var length = 0; //雙向鏈表的頭結點,初始化爲NULL var head = null; //雙向鏈表的尾結點,初始化爲NULL var tail = null; }
雙向鏈表須要有以下的方法:
append(element): 添加元素到雙向鏈表尾部
insert(position,element): 向雙向鏈表中某個位置插入元素
removeAt(position): 移除雙向鏈表中某個位置的元素
showHead(): 獲取雙向鏈表的頭部
showLength(): 獲取雙向鏈表長度
showTail(): 獲取雙向鏈表尾部
說明: 添加元素到雙向鏈表尾部
實現:
/** * 向鏈表尾部添加元素 * @param {Any} element 要加入鏈表的節點 * @return {Any} 加入鏈表的節點 */ this.append = function(element) { var node = new Node(element); if (head === null) { head = node; tail = node; } else { var previous; var current = head; while (current.next) { current = current.next; } current.next = node; node.prev = current; tail = node; } length++; return node; };
說明: 向雙向鏈表中某個位置插入元素。
實現:
/** * 向鏈表中插入某個元素 * @param {Number} position 要插入的位置 * @return {Boolean} 插入成功返回true,失敗返回false */ this.insert = function(position, element) { if (position >= 0 && position <= length) { var node = new Node(element); var index = 0; var previous; var current = head; if (position === 0) { if (head === null) { head = node; tail = node; } else { current.prev = node; node.next = current; head = node; } } else if (position === length) { current = tail; current.next = node; node.prev = current; tail = node; } else { while (index++ < position) { previous = current; current = current.next; } previous.next = node; node.prev = previous; current.prev = node; node.next = current; } length++; return true; } else { return false; } };
說明:移除雙向鏈表中某個位置的元素。
實現:
/** * 移除鏈表中某一個元素 * @param {Number} position 要移除元素的位置 * @return {Any} 移除成功返回被移除的元素,不成功則返回false */ this.removeAt = function(position) { if (position > -1 && position < length) { var current = head; var index = 0; var previous; if (position === 0) { head = current.next; if (length === 1) { tail = null; head.prev = null; } } else if (position === length - 1) { current = tail; tail = current.prev; tail.next = null; } else { while (index++ < position) { previous = current.prev; current = current.next; } previous.next = current.next; current.next.prev = previous; } length--; return current.element; } else { return false; } };
實現:
/** * 獲取鏈表的頭部 * @return {Any} 鏈表的頭部 */ this.showHead = function() { return head; }; /** * 獲取鏈表長度 * @return {Number} 鏈表長度 */ this.showLength = function() { return length; }; /** * 獲取鏈表尾部 * @return {Any} 鏈表尾部 */ this.showTail = function() { return tail; };
源代碼在此~
鏈表這一節,基本所有都是先按需求寫代碼,寫完後再和書上對比。發現簡直被瞬間秒成渣。本身寫的不少暗坑,邏輯也很混亂。看來仍是太年輕了。
有興趣的同窗,也能夠本身試試只看要求先寫代碼,寫完後再與書上比對,就知道本身的不足了。
前端路漫漫,且行且歌~
最後附上本人博客地址和原文連接,但願能與各位多多交流。