前端資料結構 鍊錶(單向 雙向 迴圈)

2021-09-25 16:28:07 字數 4017 閱讀 6335

偶然一次接觸到雙向鍊錶結構,有點懵,前端不都是用陣列儲存資料麼,既然沒聽過就翻翻書,漲漲見識,哈哈

鍊錶與陣列的區別在於:

陣列的大小是固定的,從陣列的起點或中間插入 或移除項的成本很高,因為需要移動元素。而鍊錶新增或移除元素的時候不需要移動其他元素。

陣列可以直接訪問任何位置的任何元素,而要想訪問鍊錶中間的乙個元素,需要從起點(表頭)開始迭代列表直到找到 所需的元素

如此,是不是可以理解為,如果是查詢操作較多的資料,則以陣列儲存;如果是增刪改等操作較多的,則以鍊錶儲存 ??(未經驗證)

以下為鍊錶結構的**實現,以及新增,刪除,查詢,獲取長度等方法的實現

function linkedlist ()

let length = 0; //列表項的數量的

let head = null; //第乙個節點的引用

// 1.向列表尾部新增乙個新的項

let node = new node(element),

current;

if(head === null)else

current.next = node;

}length++; // 更新長度

}; // 2.向列表的特定位置插入乙個新的項

this.insert = function(position,element)

let node = new node(element),

current = head,

pre,index=0;

// 插入在第乙個位置

if(position === 0)else

let current = head,

pre,index=0;

if(position ===0)else

pre.next = current.next;

}length--;

return current.element;

};// 4.輸出元素的值。

this.tostring = function()

return str;

};// 5.返回元素在列表中的索引。如果列表中沒有該元素則返回-1

this.indexof = function(element)

current = current.next;

}return -1;

};// 6.從列表中移除一項

this.remove = function(element);

// 7.如果鍊錶中不包含任何元素,返回true,如果鍊錶長度大於0則返回false

this.isempty = function();

// 8.返回鍊錶包含的元素個數

this.size = function();

// 9.獲取head

this.gethead = function();

// 10.列印

如下為雙向鍊錶的**實現,這裡採用es6的語法。

備註:tostring,tostring2,indexof,remove方法為自己擴充套件的

class node 

}class doublylinkedlist

// 1.在任意位置插入新元素

insert(position, element)

let node = new node(element),

curent = this.head,

pre, index = 0;

if (position === 0) else

} else if (position === this.length) else

node.next = curent;

node.pre = pre;

}this.length++;

return true;

} // 2.從任意位置移除元素

removeat(position)

let curent = this.head,

preele, nextele, index = 0;

if (position === 0) else

} else if (position === this.length - 1) else

preele = curent.pre;

nextele = curent.next;

preele.next = nextele;

nextele.pre = preele;

}this.length--;

return curent.element;

} // 3.輸出元素的值(順序)

tostring()

return str

} // 4.輸出元素的值(倒序)

tostring2()

return str

} // 5.返回元素在列表中的索引。如果列表中沒有該元素則返回-1

indexof(element)

curent = curent.next;

}return -1

} // 6.從列表中刪除一項指定元素

remove(element) else if (curent.next === null) else

}curent = curent.next

}return false

}}let list = new doublylinkedlist();

list.insert(0, 10)

list.insert(1, 11)

list.insert(2, 12)

list.insert(3, 13)

list.insert(4, 14)

console.log(list.tostring());

console.log(list.tostring2());

list.removeat(2);

console.log(list.tostring());

console.log(list.indexof(13));

console.log(list.remove(13));

console.log(list.tostring());

迴圈鍊錶可以像鍊錶一樣只有單向引用,也可以像雙向鍊錶一樣有雙向引用。迴圈鍊錶和鏈 表之間唯一的區別在於,最後乙個元素指向下乙個元素的指標(tail.next)不是引用null, 而是指向第乙個元素(head),如下圖所示:

雙向迴圈鍊錶有指向head元素的tail.next,和指向tail元素的head.prev。 

資料結構 鍊錶 單向鍊錶

鍊錶 linked list 是由一連串的結構 稱為結點 組成的,其中每個結點都包含指向鏈中下乙個結點的指標。鍊錶中的最後乙個結點包含乙個空指標。鍊錶與陣列不同,陣列的線性序是由陣列的下標決定的,而鍊錶中的順序是由各結點的指標域所決定的。鍊錶可以靈活地表示動態集合。採用鍊錶表示線性表,無論向表中插入...

資料結構 鍊錶 單向鍊錶

單向鍊錶 插入刪除效率比陣列高 建立節點 node class node class list 追加節點 將值變為節點 let newnode new node element 判斷是否為空鍊錶 if this.head null else current.next newnode 長度必須加1 t...

資料結構 鍊錶 單向鍊錶

鍊錶 linked list 是由一連串的結構 稱為結點 組成的,其中每個結點都包含指向鏈中下乙個結點的指標。鍊錶中的最後乙個結點包含乙個空指標。鍊錶與陣列不同,陣列的線性序是由陣列的下標決定的,而鍊錶中的順序是由各結點的指標域所決定的。鍊錶可以靈活地表示動態集合。採用鍊錶表示線性表,無論向表中插入...