日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

Go語言列表List獲取元素的4種方式_Golang

作者:張志翔 ? 更新時(shí)間: 2022-06-19 編程語言

Golang的列表元素的獲取可以使用內(nèi)置的 Front 函數(shù)獲取頭結(jié)點(diǎn),使用 Back 函數(shù)獲取尾結(jié)點(diǎn),使用 Prev 獲取前一個(gè)結(jié)點(diǎn),使用 Next 獲取下一個(gè)結(jié)點(diǎn)。

1、獲取列表頭結(jié)點(diǎn)

Front() *Element

package main
import (
    "container/list"
    "fmt"
)
func main() {
    fmt.Println("嗨客網(wǎng)(www.haicoder.net)")
    //使用列表內(nèi)置的 Front() 函數(shù),獲取列表的頭結(jié)點(diǎn)
    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é)點(diǎn)。

2、獲取列表尾結(jié)點(diǎn)

Back () *Element

package main
import (
    "container/list"
    "fmt"
)
func main() {
    fmt.Println("嗨客網(wǎng)(www.haicoder.net)")
    //使用列表內(nèi)置的 Back() 函數(shù),獲取列表的尾結(jié)點(diǎn)
    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é)點(diǎn)。

3、獲取上一個(gè)結(jié)點(diǎn)

Prev() *Element

package main
import (
    "container/list"
    "fmt"
)
func main() {
    fmt.Println("嗨客網(wǎng)(www.haicoder.net)")
    //使用列表內(nèi)置的 Prev() 函數(shù),獲取列表的上一個(gè)結(jié)點(diǎn)
    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ù),獲取列表的上一個(gè)結(jié)點(diǎn)。

4、獲取下一個(gè)結(jié)點(diǎn)

Next() *Element

package main
import (
    "container/list"
    "fmt"
)
func main() {
    fmt.Println("嗨客網(wǎng)(www.haicoder.net)")
    //使用列表內(nèi)置的 Next() 函數(shù),獲取列表的下一個(gè)結(jié)點(diǎn)
    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ù),獲取列表的下一個(gè)結(jié)點(diǎn)。

原文鏈接:https://blog.csdn.net/qq_19734597/article/details/121097335

欄目分類
最近更新