Golang:List


List的接口

 1 func New() *List                                                    //創建List
 2 func (l *List) Back() *Element                                      //返回List的上一個元素
 3 func (l *List) Front() *Element                                     //返回List下一個元素
 4 func (l *List) Init() *List                                         //初始化List
 5 func (l *List) InsertAfter(v interface{}, mark *Element) *Element   //在指定節點后插入,成功返回插入節點的指針,失敗返回nil, 時間復雜度O(1)
 6 func (l *List) InsertBefore(v interface{}, mark *Element) *Element  //在指定節點之前插入, 成功返回插入節點的指針,失敗返回nil, 時間復雜度O(1)
 7 func (l *List) Len() int                                            //返回鏈表長度,時間復雜度O(1)
 8 func (l *List) MoveAfter(e, mark *Element)                          //移動節點e到mark節點之后,時間復雜度O(1), 處理方式:先刪除然后再插入
 9 func (l *List) MoveBefore(e, mark *Element)                         //移動節點e到mark節點之前,時間復雜度O(1), 處理方式:先刪除然后再插入
10 func (l *List) MoveToBack(e *Element)                               //移動節點e到鏈表的尾部
11 func (l *List) MoveToFront(e *Element)                              //移動節點e到鏈表的頭部
12 func (l *List) PushBack(v interface{}) *Element                     //在鏈表尾部追加值為v的新節點
13 func (l *List) PushBackList(other *List)                            //把鏈表other所有節點追加到當前鏈表的尾部
14 func (l *List) PushFront(v interface{}) *Element                    //在鏈表的頭部插入新節點
15 func (l *List) PushFrontList(other *List)                           //把鏈表other所有節點追加到當前鏈表頭部
16 func (l *List) Remove(e *Element) interface{}                       //刪除指定節點

從這些接口我們可以看到Go的list應該是一個雙向鏈表,不然InsertBefore這種操作應該不會放出來。

然后我們再從源碼看看List的結構

 1 // Element is an element of a linked list.
 2 type Element struct {
 3     // Next and previous pointers in the doubly-linked list of elements.
 4     // To simplify the implementation, internally a list l is implemented
 5     // as a ring, such that &l.root is both the next element of the last
 6     // list element (l.Back()) and the previous element of the first list
 7     // element (l.Front()).
 8     next, prev *Element
 9 
10     // The list to which this element belongs.
11     list *List
12 
13     // The value stored with this element.
14     Value interface{}
15 }
1 // List represents a doubly linked list.
2 // The zero value for List is an empty list ready to use.
3 type List struct {
4     root Element // sentinel list element, only &root, root.prev, and root.next are used
5     len  int     // current list length excluding (this) sentinel element
6 }

從這里證實了上面的猜想,這是一個雙向鏈表

List的使用

 1 package main
 2 
 3 import (
 4     "container/list"
 5     "fmt"
 6 )
 7 
 8 func main() {
 9 
10     list0 := list.New()
11     e4 := list0.PushBack(4)
12     e1 := list0.PushFront(1)
13     list0.InsertBefore(3, e4)
14     list0.InsertAfter(2, e1)
15 
16     for e := list0.Front(); e != nil; e = e.Next() {
17         fmt.Print(e.Value, " ")
18     }
19     fmt.Println("\r")
20 
21     front := list0.Front()
22     back := list0.Back()
23 
24     fmt.Println("front is:", front.Value)
25     fmt.Println("back is:", back.Value)
26     fmt.Println("length is", list0.Len())
27 
28     list0.InsertBefore(0, front)
29     list0.InsertAfter(5, back)
30 
31     list0.PushFront(-1)
32     list0.PushBack(6)
33     for e := list0.Front(); e != nil; e = e.Next() {
34         fmt.Print(e.Value, " ")
35     }
36     fmt.Println("\r")
37     list0.Remove(list0.Front())
38     for e := list0.Front(); e != nil; e = e.Next() {
39         fmt.Print(e.Value, " ")
40     }
41 
42     fmt.Println("\r")
43     list1 := list.New()
44     list1.PushBack(7)
45     list1.PushBack(8)
46     list0.PushBackList(list1)
47     for e := list0.Front(); e != nil; e = e.Next() {
48         fmt.Print(e.Value, " ")
49     }
50     fmt.Println("\r")
51 }

10-14行往list寫入1,2,3,4

16行遍歷打印list

21-16行打印front 和back節點

42-48行把另一個list追加到list的back

 

運行結果是:

1 2 3 4 
front is: 1
back is: 4
length is 4
-1 0 1 2 3 4 5 6 
0 1 2 3 4 5 6 
0 1 2 3 4 5 6 7 8 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM