zoukankan      html  css  js  c++  java
  • go container/list双向链使用实例

    package main
     
    import (
        "container/list"
        "fmt"
    )
     
    func main() {
        l := list.New() //创建一个新的list
        for i := 0; i < 5; i++ {
            l.PushBack(i)
        }
        for e := l.Front(); e != nil; e = e.Next() {
            fmt.Print(e.Value) //输出list的值,01234
        }
        fmt.Println("")
        fmt.Println(l.Front().Value) //输出首部元素的值,0
        fmt.Println(l.Back().Value)  //输出尾部元素的值,4
        l.InsertAfter(6, l.Front())  //首部元素之后插入一个值为6的元素
        for e := l.Front(); e != nil; e = e.Next() {
            fmt.Print(e.Value) //输出list的值,061234
        }
        fmt.Println("")
        l.MoveBefore(l.Front().Next(), l.Front()) //首部两个元素位置互换
        for e := l.Front(); e != nil; e = e.Next() {
            fmt.Print(e.Value) //输出list的值,601234
        }
        fmt.Println("")
        l.MoveToFront(l.Back()) //将尾部元素移动到首部
        for e := l.Front(); e != nil; e = e.Next() {
            fmt.Print(e.Value) //输出list的值,460123
        }
        fmt.Println("")
        l2 := list.New()
        l2.PushBackList(l) //将l中元素放在l2的末尾
        for e := l2.Front(); e != nil; e = e.Next() {
            fmt.Print(e.Value) //输出l2的值,460123
        }
        fmt.Println("")
        l.Init()           //清空l</span>
        fmt.Print(l.Len()) //0
        for e := l.Front(); e != nil; e = e.Next() {
            fmt.Print(e.Value) //输出list的值,无内容
        }
     
    }
  • 相关阅读:
    Bugly和dispatch_once Crash
    IQKeyboardManager
    Storyboard References
    Book
    Git管理
    iOS开发之RunLoop--转
    H264之PPS、SPS了解
    iOS之UI设置随记
    使用 github 本地项目上传到github上 步骤
    spring中自定义注解
  • 原文地址:https://www.cnblogs.com/kevin-yang123/p/14814632.html
Copyright © 2011-2022 走看看