Go語言列表List獲取元素的4種方式
Golang的列表元素的獲取可以使用內(nèi)置的 Front 函數(shù)獲取頭結(jié)點,使用 Back 函數(shù)獲取尾結(jié)點,使用 Prev 獲取前一個結(jié)點,使用 Next 獲取下一個結(jié)點。
1、獲取列表頭結(jié)點
Front() *Element
package main
import (
"container/list"
"fmt"
)
func main() {
fmt.Println("嗨客網(wǎng)(www.haicoder.net)")
//使用列表內(nèi)置的 Front() 函數(shù),獲取列表的頭結(jié)點
listHaiCoder := list.New()
listHaiCoder.PushFront("Hello")
listHaiCoder.PushFront("HaiCoder")
listHaiCoder.PushFront("嗨客網(wǎng)")
element := listHaiCoder.Front()
fmt.Println("Front =", element.Value)
}
使用列表內(nèi)置的 Front() 函數(shù),獲取列表的頭結(jié)點。
2、獲取列表尾結(jié)點
Back () *Element
package main
import (
"container/list"
"fmt"
)
func main() {
fmt.Println("嗨客網(wǎng)(www.haicoder.net)")
//使用列表內(nèi)置的 Back() 函數(shù),獲取列表的尾結(jié)點
listHaiCoder := list.New()
listHaiCoder.PushFront("Hello")
listHaiCoder.PushFront("HaiCoder")
listHaiCoder.PushFront("嗨客網(wǎng)")
element := listHaiCoder.Back()
fmt.Println("Back =", element.Value)
}
使用列表內(nèi)置的 Back() 函數(shù),獲取列表的尾結(jié)點。
3、獲取上一個結(jié)點
Prev() *Element
package main
import (
"container/list"
"fmt"
)
func main() {
fmt.Println("嗨客網(wǎng)(www.haicoder.net)")
//使用列表內(nèi)置的 Prev() 函數(shù),獲取列表的上一個結(jié)點
listHaiCoder := list.New()
listHaiCoder.PushFront("Hello")
element := listHaiCoder.PushFront("HaiCoder")
listHaiCoder.PushFront("嗨客網(wǎng)")
preElement := element.Prev()
fmt.Println("preElement =", preElement.Value)
}
使用列表內(nèi)置的 Prev() 函數(shù),獲取列表的上一個結(jié)點。
4、獲取下一個結(jié)點
Next() *Element
package main
import (
"container/list"
"fmt"
)
func main() {
fmt.Println("嗨客網(wǎng)(www.haicoder.net)")
//使用列表內(nèi)置的 Next() 函數(shù),獲取列表的下一個結(jié)點
listHaiCoder := list.New()
listHaiCoder.PushFront("Hello")
element := listHaiCoder.PushFront("HaiCoder")
listHaiCoder.PushFront("嗨客網(wǎng)")
nextElement := element.Next()
fmt.Println("nextElement =", nextElement.Value)
}
使用列表內(nèi)置的 Next() 函數(shù),獲取列表的下一個結(jié)點。
到此這篇關(guān)于Go語言列表List獲取元素的4種方式的文章就介紹到這了,更多相關(guān)Go 列表List獲取元素內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Goland激活碼破解永久版及安裝詳細(xì)教程(親測可以)
這篇文章主要介紹了Goland激活碼破解永久版及安裝詳細(xì)教程(親測可以),本文通過實例圖文相結(jié)合給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10
go語言數(shù)據(jù)結(jié)構(gòu)之前綴樹Trie
這篇文章主要介紹了go語言數(shù)據(jù)結(jié)構(gòu)之前綴樹Trie,文章圍繞主題展開詳細(xì)內(nèi)容介紹,具有一定得參考價值,需要的小伙伴可以參考一下2022-05-05
Go語言學(xué)習(xí)函數(shù)+結(jié)構(gòu)體+方法+接口
這篇文章主要介紹了Go語言學(xué)習(xí)函數(shù)+結(jié)構(gòu)體+方法+接口,文章圍繞主題的相關(guān)資料展開詳細(xì)的文章說明,具有一定的參考價值,需要的小伙伴可以參考一下2022-05-05

