github.com/code-reading/golang@v0.0.0-20220303082512-ba5bc0e589a3/go/src/container/list/example_test.go (about)

     1  // Copyright 2013 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package list_test
     6  
     7  import (
     8  	"container/list"
     9  	"fmt"
    10  )
    11  
    12  // 链表的简单示例
    13  func Example() {
    14  	// Create a new list and put some numbers in it.
    15  	l := list.New()
    16  	e4 := l.PushBack(4)
    17  	e1 := l.PushFront(1)
    18  	l.InsertBefore(3, e4)
    19  	l.InsertAfter(2, e1)
    20  
    21  	// Iterate through list and print its contents.
    22  	for e := l.Front(); e != nil; e = e.Next() {
    23  		fmt.Println(e.Value)
    24  	}
    25  
    26  	// Output:
    27  	// 1
    28  	// 2
    29  	// 3
    30  	// 4
    31  }