# package list
`import "container/list"`
list包实现了双向链表。要遍历一个链表:
```
for e := l.Front(); e != nil; e = e.Next() {
// do something with e.Value
}
```
Example
```
// Create a new list and put some numbers in it.
l := list.New()
e4 := l.PushBack(4)
e1 := l.PushFront(1)
l.InsertBefore(3, e4)
l.InsertAfter(2, e1)
// Iterate through list and print its contents.
for e := l.Front(); e != nil; e = e.Next() {
fmt.Println(e.Value)
}
```
Output:
```
1
2
3
4
```
## Index
* [type Element](#Element)
* [func (e \*Element) Next() \*Element](#Element.Next)
* [func (e \*Element) Prev() \*Element](#Element.Prev)
* [type List](#List)
* [func New() \*List](#New)
* [func (l \*List) Init() \*List](#List.Init)
* [func (l \*List) Len() int](#List.Len)
* [func (l \*List) Front() \*Element](#List.Front)
* [func (l \*List) Back() \*Element](#List.Back)
* [func (l \*List) PushFront(v interface{}) \*Element](#List.PushFront)
* [func (l \*List) PushFrontList(other \*List)](#List.PushFrontList)
* [func (l \*List) PushBack(v interface{}) \*Element](#List.PushBack)
* [func (l \*List) PushBackList(other \*List)](#List.PushBackList)
* [func (l \*List) InsertBefore(v interface{}, mark \*Element) \*Element](#List.InsertBefore)
* [func (l \*List) InsertAfter(v interface{}, mark \*Element) \*Element](#List.InsertAfter)
* [func (l \*List) MoveToFront(e \*Element)](#List.MoveToFront)
* [func (l \*List) MoveToBack(e \*Element)](#List.MoveToBack)
* [func (l \*List) MoveBefore(e, mark \*Element)](#List.MoveBefore)
* [func (l \*List) MoveAfter(e, mark \*Element)](#List.MoveAfter)
* [func (l \*List) Remove(e \*Element) interface{}](#List.Remove)
### Examples
* [package](#example-package)
## type [Element](https://github.com/golang/go/blob/master/src/container/list/list.go#L15 "View Source")
```
type Element struct {
// 元素保管的值
Value interface{}
// 内含隐藏或非导出字段
}
```
Element类型代表是双向链表的一个元素。
### func (\*Element) [Next](https://github.com/golang/go/blob/master/src/container/list/list.go#L31 "View Source")
```
func (e *Element) Next() *Element
```
Next返回链表的后一个元素或者nil。
### func (\*Element) [Prev](https://github.com/golang/go/blob/master/src/container/list/list.go#L39 "View Source")
```
func (e *Element) Prev() *Element
```
Prev返回链表的前一个元素或者nil。
## type [List](https://github.com/golang/go/blob/master/src/container/list/list.go#L48 "View Source")
```
type List struct {
// 内含隐藏或非导出字段
}
```
List代表一个双向链表。List零值为一个空的、可用的链表。
### func [New](https://github.com/golang/go/blob/master/src/container/list/list.go#L62 "View Source")
```
func New() *List
```
New创建一个链表。
### func (\*List) [Init](https://github.com/golang/go/blob/master/src/container/list/list.go#L54 "View Source")
```
func (l *List) Init() *List
```
Init清空链表。
### func (\*List) [Len](https://github.com/golang/go/blob/master/src/container/list/list.go#L66 "View Source")
```
func (l *List) Len() int
```
Len返回链表中元素的个数,复杂度O(1)。
### func (\*List) [Front](https://github.com/golang/go/blob/master/src/container/list/list.go#L69 "View Source")
```
func (l *List) Front() *Element
```
Front返回链表第一个元素或nil。
### func (\*List) [Back](https://github.com/golang/go/blob/master/src/container/list/list.go#L77 "View Source")
```
func (l *List) Back() *Element
```
Back返回链表最后一个元素或nil。
### func (\*List) [PushFront](https://github.com/golang/go/blob/master/src/container/list/list.go#L131 "View Source")
```
func (l *List) PushFront(v interface{}) *Element
```
PushBack将一个值为v的新元素插入链表的第一个位置,返回生成的新元素。
### func (\*List) [PushFrontList](https://github.com/golang/go/blob/master/src/container/list/list.go#L211 "View Source")
```
func (l *List) PushFrontList(other *List)
```
PushFrontList创建链表other的拷贝,并将拷贝的最后一个位置连接到链表l的第一个位置。
### func (\*List) [PushBack](https://github.com/golang/go/blob/master/src/container/list/list.go#L137 "View Source")
```
func (l *List) PushBack(v interface{}) *Element
```
PushBack将一个值为v的新元素插入链表的最后一个位置,返回生成的新元素。
### func (\*List) [PushBackList](https://github.com/golang/go/blob/master/src/container/list/list.go#L202 "View Source")
```
func (l *List) PushBackList(other *List)
```
PushBack创建链表other的拷贝,并将链表l的最后一个位置连接到拷贝的第一个位置。
### func (\*List) [InsertBefore](https://github.com/golang/go/blob/master/src/container/list/list.go#L144 "View Source")
```
func (l *List) InsertBefore(v interface{}, mark *Element) *Element
```
InsertDefore将一个值为v的新元素插入到mark前面,并返回生成的新元素。如果mark不是l的元素,l不会被修改。
### func (\*List) [InsertAfter](https://github.com/golang/go/blob/master/src/container/list/list.go#L154 "View Source")
```
func (l *List) InsertAfter(v interface{}, mark *Element) *Element
```
InsertAfter将一个值为v的新元素插入到mark后面,并返回新生成的元素。如果mark不是l的元素,l不会被修改。
### func (\*List) [MoveToFront](https://github.com/golang/go/blob/master/src/container/list/list.go#L164 "View Source")
```
func (l *List) MoveToFront(e *Element)
```
MoveToFront将元素e移动到链表的第一个位置,如果e不是l的元素,l不会被修改。
### func (\*List) [MoveToBack](https://github.com/golang/go/blob/master/src/container/list/list.go#L174 "View Source")
```
func (l *List) MoveToBack(e *Element)
```
MoveToBack将元素e移动到链表的最后一个位置,如果e不是l的元素,l不会被修改。
### func (\*List) [MoveBefore](https://github.com/golang/go/blob/master/src/container/list/list.go#L184 "View Source")
```
func (l *List) MoveBefore(e, mark *Element)
```
MoveBefore将元素e移动到mark的前面。如果e或mark不是l的元素,或者e==mark,l不会被修改。
### func (\*List) [MoveAfter](https://github.com/golang/go/blob/master/src/container/list/list.go#L193 "View Source")
```
func (l *List) MoveAfter(e, mark *Element)
```
MoveAfter将元素e移动到mark的后面。如果e或mark不是l的元素,或者e==mark,l不会被修改。
### func (\*List) [Remove](https://github.com/golang/go/blob/master/src/container/list/list.go#L121 "View Source")
```
func (l *List) Remove(e *Element) interface{}
```
Remove删除链表中的元素e,并返回e.Value。
- 库
- package achive
- package tar
- package zip
- package bufio
- package builtin
- package bytes
- package compress
- package bzip2
- package flate
- package gzip
- package lzw
- package zlib
- package container
- package heap
- package list
- package ring
- package crypto
- package aes
- package cipher
- package des
- package dsa
- package ecdsa
- package elliptic
- package hmac
- package md5
- package rand
- package rc4
- package rsa
- package sha1
- package sha256
- package sha512
- package subtle
- package tls
- package x509
- package pkix
- package database
- package sql
- package driver
- package encoding
- package ascii85
- package asn1
- package base32
- package base64
- package binary
- package csv
- package gob
- package hex
- package json
- package pem
- package xml
- package errors
- package expvar
- package flag
- package fmt
- package go
- package doc
- package format
- package parser
- package printer
- package hash
- package adler32
- package crc32
- package crc64
- package fnv
- package html
- package template
- package image
- package color
- package palette
- package draw
- package gif
- package jpeg
- package png
- package index
- package suffixarray
- package io
- package ioutil
- package log
- package syslog
- package math
- package big
- package cmplx
- package rand
- package mime
- package multipart
- package net
- package http
- package cgi
- package cookiejar
- package fcgi
- package httptest
- package httputil
- package pprof
- package mail
- package rpc
- package jsonrpc
- package smtp
- package textproto
- package url
- package os
- package exec
- package signal
- package user
- package path
- package filepath
- package reflect
- package regexp
- package runtime
- package cgo
- package debug
- package pprof
- package race
- package sort
- package strconv
- package strings
- package sync
- package atomic
- package text
- package scanner
- package tabwriter
- package template
- package time
- package unicode
- package utf16
- package utf8
- package unsafe