javaScript數(shù)據(jù)結(jié)構(gòu)--雙向鏈表

雙向鏈表和普通鏈表的區(qū)別是,普通鏈表中一個節(jié)點只有一個next指針指向下一個節(jié)點,雙向鏈表有2個指針,一個指向下一個節(jié)點,一個指向前面一個節(jié)點。

雙向鏈表的結(jié)構(gòu)圖

雙向鏈表的代碼實現(xiàn):

function DoubleLinkedList() {

? ? var Node = function(element) {

? ? ? ? this.element = element;

? ? ? ? this.next = null;

? ? ? ? this.prev = null;

? ? }

? ? var head = null,

? ? ? ? tail = null,

? ? ? ? length = 0;

? ? this.append = function(element) {

? ? ? ? const node = new Node(element);

? ? ? ? if(length === 0) {

? ? ? ? ? ? head = node;

? ? ? ? ? ? tail = node;

? ? ? ? }else {

? ? ? ? ? ? tail.next = node;

? ? ? ? ? ? node.prev = tail;

? ? ? ? ? ? tail = node;

? ? ? ? }

? ? ? ? length++;

? ? ? ? return true;

? ? }


? ? this.insert = function(position, element) {

? ? ? ? if(position>=0 && position <=length) {

? ? ? ? ? ? var current = head,

? ? ? ? ? ? ? ? previous,

? ? ? ? ? ? ? ? index = 0;

? ? ? ? ? ? const node = new Node(element);

? ? ? ? ? ? if(position === 0) {

? ? ? ? ? ? ? ? if(!head) {

? ? ? ? ? ? ? ? ? ? head = node;

? ? ? ? ? ? ? ? ? ? tail = node;

? ? ? ? ? ? ? ? } else {

? ? ? ? ? ? ? ? ? ? node.next = current;

? ? ? ? ? ? ? ? ? ? current.prev = node;

? ? ? ? ? ? ? ? ? ? head = current;

? ? ? ? ? ? ? ? }

? ? ? ? ? ? } else if(position === length) {

? ? ? ? ? ? ? ? current = tail;

? ? ? ? ? ? ? ? node.prev = current;

? ? ? ? ? ? ? ? current.next = node;

? ? ? ? ? ? ? ? tail = node;

? ? ? ? ? ? }else {

? ? ? ? ? ? ? ? while(index++ < position) {

? ? ? ? ? ? ? ? ? ? previous = current;

? ? ? ? ? ? ? ? ? ? current = current.next;

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? node.prev = previous;

? ? ? ? ? ? ? ? previous.next = node;

? ? ? ? ? ? ? ? node.next = current;

? ? ? ? ? ? ? ? current.prev = node;

? ? ? ? ? ? }

? ? ? ? ? ? length++;

? ? ? ? ? ? return true;

? ? ? ? }else {

? ? ? ? ? ? return false;

? ? ? ? }

? ? }

? ? this.removeAt = function(position) {

? ? ? ? if(position>=0 && position < length) {

? ? ? ? ? ? var current = head,

? ? ? ? ? ? ? ? previous,

? ? ? ? ? ? ? ? index = 0;

? ? ? ? ? ? if(position === 0) {

? ? ? ? ? ? ? ? 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 null;

? ? ? ? }

? ? }

? ? this.remove = function(element) {

? ? ? ? const position = this.indexOf(element);

? ? ? ? return this.removeAt(position);

? ? }

? ? this.indexOf = function(element) {

? ? ? ? var current = head,

? ? ? ? ? ? previous,

? ? ? ? ? ? index = 0;

? ? ? ? while(current) {

? ? ? ? ? ? if(element === current.element) {

? ? ? ? ? ? ? ? return index;

? ? ? ? ? ? }

? ? ? ? ? ? index++;

? ? ? ? ? ? current = current.next;

? ? ? ? }

? ? }

? ? this.isEmpty = function() {

? ? ? ? return length === 0

? ? }

? ? this.size = function() {

? ? ? ? return this.length

? ? }

? ? this.getHead = function() {

? ? ? ? return head;

? ? }

? ? this.getTail = function() {

? ? ? ? return tail;

? ? }

? ? this.toString = function() {

? ? ? ? var current = head,

? ? ? ? string = '';

? ? ? ? while(current) {

? ? ? ? ? ? string += String(current.element);

? ? ? ? ? ? current = current.next;

? ? ? ? }

? ? ? ? return string;

? ? }

}

var list = new DoubleLinkedList();

list.append('aa');

list.append('bb');

console.log(list.toString()); // aabb

list.insert(1, 'cc')

list.insert(1, 'dd')

console.log(list.toString()); // aaddccbb

list.removeAt(1);

console.log(list.toString()); // aaccbb

console.log(list.indexOf('cc')); // 1

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容

  • 如需轉(zhuǎn)載, 請咨詢作者, 并且注明出處.有任何問題, 可以關(guān)注我的微博: coderwhy, 或者添加我的微信: ...
    coderwhy閱讀 7,900評論 1 30
  • 一. 認識雙向鏈表 雙向鏈表介紹 單向鏈表: 我們可以輕松的到達下一個節(jié)點, 但是回到錢一個節(jié)點是很難的. 但是,...
    小碼哥教育520it閱讀 15,218評論 0 0
  • 1數(shù)組 1.1方法列表 數(shù)組的常用方法如下: concat: 鏈接兩個或者更多數(shù)據(jù),并返回結(jié)果。 every: 對...
    奮斗1216閱讀 297評論 0 1
  • 關(guān)于石頭愛情傳奇(游大連 一島) <一> 那個靜坐千年的石頭 終日面對大海 苦思冥想 面對我的第一次到來 它的...
    云水夢閱讀 344評論 0 1
  • 沒有宙斯的神話沒有星光的銀河沒有太陽的宇宙只剩下你,只剩下我…… 我不想帶你去看肅殺的景致怕灼傷你無辜的雙眼請你,...
    Minnie沫閱讀 128評論 0 0