雙向鍊錶(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中文網其他相關文章!

本文解釋了GO的軟件包導入機制:命名imports(例如導入“ fmt”)和空白導入(例如導入_ fmt; fmt;)。 命名導入使包裝內容可訪問,而空白導入僅執行t

本文解釋了Beego的NewFlash()函數,用於Web應用程序中的頁間數據傳輸。 它專注於使用newflash()在控制器之間顯示臨時消息(成功,錯誤,警告),並利用會話機制。 Lima

本文詳細介紹了MySQL查詢結果的有效轉換為GO結構切片。 它強調使用數據庫/SQL的掃描方法來最佳性能,避免手動解析。 使用DB標籤和Robus的結構現場映射的最佳實踐

本文演示了創建模擬和存根進行單元測試。 它強調使用接口,提供模擬實現的示例,並討論最佳實踐,例如保持模擬集中並使用斷言庫。 文章

本文探討了GO的仿製藥自定義類型約束。 它詳細介紹了界面如何定義通用功能的最低類型要求,從而改善了類型的安全性和代碼可重複使用性。 本文還討論了局限性和最佳實踐

本文詳細介紹了在GO中詳細介紹有效的文件,將OS.WriteFile(適用於小文件)與OS.openfile和緩衝寫入(最佳大型文件)進行比較。 它強調了使用延遲並檢查特定錯誤的可靠錯誤處理。

本文使用跟踪工具探討了GO應用程序執行流。 它討論了手冊和自動儀器技術,比較諸如Jaeger,Zipkin和Opentelemetry之類的工具,並突出顯示有效的數據可視化


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

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

SublimeText3 Linux新版
SublimeText3 Linux最新版