雙向鍊錶(Doubly linked list)是一種常用的資料結構,它使得我們可以在 O(1) 時間複雜度內在鍊錶的任意位置執行插入、刪除或查詢操作。
在 Golang 中實作雙向鍊錶需要使用指針,因為 Golang 中的所有類型都是值類型,無法直接修改原始資料。透過指針,我們可以輕鬆地進行值的修改和傳遞,從而實現雙向鍊錶的操作。
下面是一個簡單的Golang 實作的雙向鍊錶:
package main import "fmt" type Node struct { data int previous *Node next *Node } type LinkedList struct { head *Node tail *Node } func (list *LinkedList) insertAtBeginning(data int) { newNode := &Node{ data: data, previous: nil, next: nil, } if list.head == nil { list.head = newNode list.tail = newNode return } newNode.next = list.head list.head.previous = newNode list.head = newNode } func (list *LinkedList) insertAtEnd(data int) { newNode := &Node{ data: data, previous: nil, next: nil, } if list.tail == nil { list.head = newNode list.tail = newNode return } newNode.previous = list.tail list.tail.next = newNode list.tail = newNode } func (list *LinkedList) delete(data int) bool { currentNode := list.head for currentNode != nil { if currentNode.data == data { if currentNode == list.head { list.head = currentNode.next list.head.previous = nil } else if currentNode == list.tail { list.tail = currentNode.previous list.tail.next = nil } else { currentNode.previous.next = currentNode.next currentNode.next.previous = currentNode.previous } return true } currentNode = currentNode.next } return false } func (list *LinkedList) display() { currentNode := list.head for currentNode != nil { fmt.Printf("%d ", currentNode.data) currentNode = currentNode.next } fmt.Println() } func main() { list := LinkedList{} list.insertAtEnd(1) list.insertAtEnd(2) list.insertAtEnd(3) list.insertAtBeginning(4) list.display() list.delete(3) list.display() }
在上面的程式碼中,我們首先定義了一個Node
結構體,該結構體包含鍊錶中的每個節點所需的三個資料:data
、previous
和next
。其中,data
儲存節點的值,previous
儲存上一個節點的位址,next
儲存下一個節點的位址。
然後,我們定義了一個 LinkedList
結構體來表示整個鍊錶。此結構體包含鍊錶的頭指標 head
和尾指標 tail
。
下面是實現雙向鍊錶所需的幾個方法:
// 在链表头部插入一个元素 func (list *LinkedList) insertAtBeginning(data int) { newNode := &Node{ data: data, previous: nil, next: nil, } if list.head == nil { list.head = newNode list.tail = newNode return } newNode.next = list.head list.head.previous = newNode list.head = newNode } // 在链表尾部插入一个元素 func (list *LinkedList) insertAtEnd(data int) { newNode := &Node{ data: data, previous: nil, next: nil, } if list.tail == nil { list.head = newNode list.tail = newNode return } newNode.previous = list.tail list.tail.next = newNode list.tail = newNode } // 删除链表中指定的元素 func (list *LinkedList) delete(data int) bool { currentNode := list.head for currentNode != nil { if currentNode.data == data { if currentNode == list.head { list.head = currentNode.next list.head.previous = nil } else if currentNode == list.tail { list.tail = currentNode.previous list.tail.next = nil } else { currentNode.previous.next = currentNode.next currentNode.next.previous = currentNode.previous } return true } currentNode = currentNode.next } return false } // 打印链表的所有元素 func (list *LinkedList) display() { currentNode := list.head for currentNode != nil { fmt.Printf("%d ", currentNode.data) currentNode = currentNode.next } fmt.Println() }
在定義了這幾個方法後,我們可以在main
函數中實例化一個鍊錶物件並進行操作:
func main() { list := LinkedList{} list.insertAtEnd(1) list.insertAtEnd(2) list.insertAtEnd(3) list.insertAtBeginning(4) list.display() list.delete(3) list.display() }
在上面的程式碼中,我們首先實例化了一個LinkedList
物件list
,然後我們按順序插入了四個元素: 1、2、3 和4。我們在第一次呼叫display
方法時,將輸出鍊錶的內容:
4 1 2 3
接著,我們刪除了元素3,並再次呼叫display
方法,輸出鍊錶的最新內容:
4 1 2
這個簡單的Golang 實作的雙向鍊錶示範如何使用指標來建立和修改鍊錶,以及如何實作鍊錶的插入、刪除和查詢等操作。如果你需要使用雙向鍊錶來建立高效的資料結構,請參考上面的程式碼來學習如何在 Golang 中實作雙向鍊錶。
以上是golang實作雙向鍊錶的詳細內容。更多資訊請關注PHP中文網其他相關文章!

goisidealforbuildingscalablesystemsduetoitssimplicity,效率和建築物內currencysupport.1)go'scleansyntaxandaxandaxandaxandMinimalisticDesignenhanceProductivityAndRedCoductivityAndRedCuceErr.2)ItSgoroutinesAndInesAndInesAndInesAndineSandChannelsEnablenableNablenableNableNablenableFifficConcurrentscorncurrentprogragrammentworking torkermenticmminging

Initfunctionsingorunautomationbeforemain()andareusefulforsettingupenvorments和InitializingVariables.usethemforsimpletasks,避免使用輔助效果,andbecautiouswithTestingTestingTestingAndLoggingTomaintAnainCodeCodeCodeClarityAndTestesto。

goinitializespackagesintheordertheordertheyimported,thenexecutesInitFunctionswithinApcageIntheirdeFinityOrder,andfilenamesdetermineTheOrderAcractacractacrosmultiplefiles.thisprocessCanbeCanbeinepessCanbeInfleccessByendercrededBydeccredByDependenciesbetenciesbetencemendencenciesbetnependendpackages,whermayleLeadtocomplexinitialitialializizesizization

CustomInterfacesingoarecrucialforwritingFlexible,可維護,andTestableCode.TheyEnableDevelostOverostOcusonBehaviorBeiroveration,增強ModularityAndRobustness.byDefiningMethodSigntulSignatulSigntulSignTypaterSignTyperesthattypesmustemmustemmustemmustemplement,InterfaceSallowForCodeRepodEreusaperia

使用接口進行模擬和測試的原因是:接口允許定義合同而不指定實現方式,使得測試更加隔離和易於維護。 1)接口的隱式實現使創建模擬對像變得簡單,這些對像在測試中可以替代真實實現。 2)使用接口可以輕鬆地在單元測試中替換服務的真實實現,降低測試複雜性和時間。 3)接口提供的靈活性使得可以為不同測試用例更改模擬行為。 4)接口有助於從一開始就設計可測試的代碼,提高代碼的模塊化和可維護性。

在Go中,init函數用於包初始化。 1)init函數在包初始化時自動調用,適用於初始化全局變量、設置連接和加載配置文件。 2)可以有多個init函數,按文件順序執行。 3)使用時需考慮執行順序、測試難度和性能影響。 4)建議減少副作用、使用依賴注入和延遲初始化以優化init函數的使用。

go'SselectStatementTreamLinesConcurrentProgrambyMultiplexingOperations.1)itallowSwaitingOnMultipleChannEloperations,執行thefirstreadyone.2)theDefirstreadyone.2)thedefefcasepreventlocksbysbysbysbysbysbythoplocktrograpraproxrograpraprocrecrecectefnoopeready.3)

contextancandwaitgroupsarecrucialingoformanaginggoroutineseflect.1)context contextsallowsAllowsAllowsAllowsAllowsAllingCancellationAndDeadLinesAcrossapibiboundaries,確保GoroutinesCanbestoppedGrace.2)WaitGroupsSynChronizeGoroutines,確保Allimizegoroutines,確保AllizeNizeGoROutines,確保AllimizeGoroutines


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

Dreamweaver Mac版
視覺化網頁開發工具

記事本++7.3.1
好用且免費的程式碼編輯器

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

Dreamweaver CS6
視覺化網頁開發工具