TypeScript實(shí)現(xiàn)單鏈表的示例代碼
鏈表的概念
鏈表是一種物理存儲單元上非連續(xù)、非順序的存儲結(jié)構(gòu),它由一系列結(jié)點(diǎn)組成,其特點(diǎn)在于結(jié)點(diǎn)可以在運(yùn)行時(shí)動(dòng)態(tài)生成。
鏈表的存儲結(jié)構(gòu)特點(diǎn)
- 鏈表的每個(gè)結(jié)點(diǎn)包括兩個(gè)部分:
- 一個(gè)是存儲數(shù)據(jù)元素的數(shù)據(jù)域
- 另一個(gè)存儲下一個(gè)結(jié)點(diǎn)地址的指針域
- 鏈表可以用任意一組存儲單元來存儲其中的數(shù)據(jù)結(jié)構(gòu),與數(shù)組不同的是它的存儲單元可以是不連續(xù)的。
單鏈表
鏈表通過每個(gè)結(jié)點(diǎn)的鏈域?qū)⒕€性表的n個(gè)結(jié)點(diǎn)按其邏輯順序鏈接在一起構(gòu)成單鏈表。單鏈表即單向鏈表,單向指的是其指針域所存儲的信息只能為一個(gè)方向。具體來說,單鏈表中的每個(gè)存儲單元中,除了需要存儲每個(gè)單元的數(shù)據(jù)外,還必須附帶儲存其直接后繼存儲單元的地址信息。如圖所示:

單鏈表的結(jié)點(diǎn)
鏈表是由一個(gè)一個(gè)節(jié)點(diǎn)通過某種關(guān)系建立關(guān)聯(lián)構(gòu)成的一種數(shù)據(jù)結(jié)構(gòu),單鏈表也是如此。單鏈表中的所有節(jié)點(diǎn)只有一個(gè)指向各自直接后繼節(jié)點(diǎn)的指針域以及各自的數(shù)據(jù)域:

由于在TypeScript中沒有指針類型,所以我們需要用一個(gè)類來模擬一個(gè)結(jié)點(diǎn):
由于在Typescript中泛型可以提高我們代碼的可復(fù)用性和靈活性,所以下面在定義結(jié)點(diǎn)和創(chuàng)建鏈表時(shí)會用到泛型。
class ListNode<T>{
value : T //數(shù)據(jù)域
next : ListNode<T> | null //指針域
constructor(value: T){
this.value = value;
this.next = null
}
}
單鏈表的基本操作
增加結(jié)點(diǎn)
增加結(jié)點(diǎn)是指為單鏈表增添節(jié)點(diǎn),增加元素的方法有很多:
- 從鏈表左端增加結(jié)點(diǎn)
- 從鏈表右端增加結(jié)點(diǎn)
- 從指定位置增加結(jié)點(diǎn)
- …
這里我們介紹從鏈表右端增加一個(gè)結(jié)點(diǎn):
//添加結(jié)點(diǎn)
add(value:T){
const newNode = new ListNode(value); //首先需要?jiǎng)?chuàng)建一個(gè)新結(jié)點(diǎn)
//頭結(jié)點(diǎn)為空,那么這個(gè)新結(jié)點(diǎn)就是鏈表的頭結(jié)點(diǎn)
if(!this.head){
this.head=newNode;
}
//頭結(jié)點(diǎn)非空
else{
let current = this.head;
//遍歷鏈表,直到找到鏈表的末尾
while(current.next)
{
current = current.next
}
current.next = newNode;//將鏈表的最后一個(gè)節(jié)點(diǎn)的指針域指向新創(chuàng)建的結(jié)點(diǎn)
}
}
刪除結(jié)點(diǎn)
刪除結(jié)點(diǎn)是從鏈表中刪除一個(gè)結(jié)點(diǎn),同樣刪除的方法也有很多:
- 按照結(jié)點(diǎn)的索引號刪除鏈表中的單個(gè)結(jié)點(diǎn)
- 按照索引號刪除鏈表中某個(gè)節(jié)點(diǎn)及其之后的結(jié)點(diǎn)
- 給定兩個(gè)索引號a,b,刪除[a,b]的一段結(jié)點(diǎn)
- …
這里我們介紹刪除指定數(shù)據(jù)所在的結(jié)點(diǎn):
remove(value : T){
const newNode = new ListNode(value);
let current = this.head;
if(!current){
console.log('該結(jié)點(diǎn)不存在,刪除失敗')
}
//刪除的結(jié)點(diǎn)是頭結(jié)點(diǎn)
else if(current && current.value == value){
this.head = current.next;
console.log('刪除成功');
}
else{
while(current.next){
if(current.next.value==value){
//讓所要?jiǎng)h除結(jié)點(diǎn)的上一個(gè)結(jié)點(diǎn)的指針域
//直接指向所要?jiǎng)h除結(jié)點(diǎn)的下一個(gè)結(jié)點(diǎn)即可
current.next = current.next.next;
console.log('刪除成功');
return;
}
current = current.next;
}
console.log('該結(jié)點(diǎn)不存在,刪除失敗')
}
}
打印鏈表
打印鏈表很簡單,就是將這個(gè)鏈表的每個(gè)結(jié)點(diǎn)都遍歷一次并且每遍歷到一個(gè)結(jié)點(diǎn)就將這個(gè)結(jié)點(diǎn)的數(shù)據(jù)輸出即可
//打印鏈表
print(){
let current = this.head;
while(current){
console.log(current);
current = current.next
}
}
鏈表功能測試
增加結(jié)點(diǎn)

測試結(jié)果如下:

刪除結(jié)點(diǎn)

測試結(jié)果如下:

完整代碼實(shí)現(xiàn)
//鏈表
//定義一個(gè)結(jié)點(diǎn)類:
class ListNode<T>{
value : T
next : ListNode<T> | null
constructor(value: T){
this.value = value;
this.next = null
}
}
//定義一個(gè)鏈表類
class LinkList<T>{
head:null | ListNode<T>
constructor(){
this.head = null;
}
//定義方法
//添加結(jié)點(diǎn)
add(value:T){
const newNode = new ListNode(value);
//頭結(jié)點(diǎn)為空
if(!this.head){
this.head=newNode;
}
//頭結(jié)點(diǎn)非空
else{
let current = this.head;
while(current.next)
{
current = current.next
}
current.next = newNode;
}
}
//刪除結(jié)點(diǎn)
remove(value : T){
const newNode = new ListNode(value);
let current = this.head;
if(!current){
console.log('該結(jié)點(diǎn)不存在,刪除失敗')
}
else if(current && current.value == value){
this.head = current.next;
console.log('刪除成功');
}
else{
while(current.next){
if(current.next.value==value){
current.next = current.next.next;
console.log('刪除成功');
return;
}
current = current.next;
}
console.log('該結(jié)點(diǎn)不存在,刪除失敗')
}
}
//打印鏈表
print(){
let current = this.head;
while(current){
console.log(current);
current = current.next
}
}
}
let list = new LinkList()
list.add(10);
list.add('hello');
list.add(true);
list.print();
list.remove('hello');
list.print();到此這篇關(guān)于TypeScript實(shí)現(xiàn)單鏈表的示例代碼的文章就介紹到這了,更多相關(guān)TypeScript 單鏈表內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳談js中數(shù)組(array)和對象(object)的區(qū)別
下面小編就為大家?guī)硪黄斦刯s中數(shù)組(array)和對象(object)的區(qū)別。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02
原生JS實(shí)現(xiàn)的簡單輪播圖功能【適合新手】
這篇文章主要介紹了原生JS實(shí)現(xiàn)的簡單輪播圖功能,結(jié)合實(shí)例形式分析了基于javascript定時(shí)器控制頁面元素動(dòng)態(tài)變換實(shí)現(xiàn)輪播圖的相關(guān)操作技巧,需要的朋友可以參考下2018-08-08
詳解使用grunt完成requirejs的合并壓縮和js文件的版本控制
這篇文章主要介紹了詳解使用grunt完成requirejs的合并壓縮和js文件的版本控制 ,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-03-03
jqplot通過ajax動(dòng)態(tài)畫折線圖的方法及思路
這篇文章主要介紹了2013-12-12
分享javascript計(jì)算時(shí)間差的示例代碼
這篇文章主要為大家介紹了javascript計(jì)算時(shí)間差的示例代碼,,一般來說都是計(jì)算當(dāng)前時(shí)間和一個(gè)指定時(shí)間點(diǎn)之間的差距,感興趣的小伙伴們可以參考一下2016-01-01
火狐下table中創(chuàng)建form導(dǎo)致兩個(gè)table之間出現(xiàn)空白
js加入form導(dǎo)致兩個(gè)table之間出現(xiàn)空白,還有另一種說法在table中創(chuàng)建form表單是不符合DOM標(biāo)準(zhǔn)的,會導(dǎo)致post失效,以及js數(shù)據(jù)傳輸失效2013-09-09
深入探討如何利用Canvas實(shí)現(xiàn)圖片壓縮與Base64轉(zhuǎn)換
隨著Web應(yīng)用的日益普及,圖片的處理和優(yōu)化已經(jīng)成為現(xiàn)代開發(fā)的關(guān)鍵部分,本文主要介紹了如何利用Canvas技術(shù),將圖片進(jìn)行壓縮,并將其轉(zhuǎn)換為Base64格式,感興趣的小伙伴可以學(xué)習(xí)下2023-10-10

