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

熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

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

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

OpenSSL,作為廣泛應用於安全通信的開源庫,提供了加密算法、密鑰和證書管理等功能。然而,其歷史版本中存在一些已知安全漏洞,其中一些危害極大。本文將重點介紹Debian系統中OpenSSL的常見漏洞及應對措施。 DebianOpenSSL已知漏洞:OpenSSL曾出現過多個嚴重漏洞,例如:心臟出血漏洞(CVE-2014-0160):該漏洞影響OpenSSL1.0.1至1.0.1f以及1.0.2至1.0.2beta版本。攻擊者可利用此漏洞未經授權讀取服務器上的敏感信息,包括加密密鑰等。

Go語言中用於浮點數運算的庫介紹在Go語言(也稱為Golang)中,進行浮點數的加減乘除運算時,如何確保精度是�...

Go爬蟲Colly中的Queue線程問題探討在使用Go語言的Colly爬蟲庫時,開發者常常會遇到關於線程和請求隊列的問題。 �...

後端學習路徑:從前端轉型到後端的探索之旅作為一名從前端開發轉型的後端初學者,你已經有了nodejs的基礎,...

本文討論了GO編程中的GO FMT命令,該命令將代碼格式化以遵守官方樣式準則。它突出了GO FMT在維持代碼一致性,可讀性和降低樣式辯論方面的重要性。 FO的最佳實踐

本文介紹在Debian系統下監控PostgreSQL數據庫的多種方法和工具,助您全面掌握數據庫性能監控。一、利用PostgreSQL內置監控視圖PostgreSQL自身提供多個視圖用於監控數據庫活動:pg_stat_activity:實時展現數據庫活動,包括連接、查詢和事務等信息。 pg_stat_replication:監控複製狀態,尤其適用於流複製集群。 pg_stat_database:提供數據庫統計信息,例如數據庫大小、事務提交/回滾次數等關鍵指標。二、借助日誌分析工具pgBadg
