From charlesreid1

(Created page with "To use lists in Go, use the "container/lists" package. This will import a linked list object. <code>import "container/list"</code> Link: https://golang.org/pkg/container/list...")
 
No edit summary
Line 11: Line 11:


     // Initialize list
     // Initialize list
    l := list.New()
    // Populate list
    e4 := l.PushBack(4)
    e1 := l.PushFront(1)
    l.InsertBefore(3, e4)
    l.InsertAfter(2, e1)


     // Iterate over list
     // Iterate over list

Revision as of 14:27, 13 December 2018

To use lists in Go, use the "container/lists" package. This will import a linked list object. import "container/list"

Link: https://golang.org/pkg/container/list/

package main

import "container/list"

func main() {

    // Initialize list
    l := list.New()

    // Populate list
    e4 := l.PushBack(4)
    e1 := l.PushFront(1)
    l.InsertBefore(3, e4)
    l.InsertAfter(2, e1)

    // Iterate over list
    for e := l.Front(); e != nil; e = e.Next() {
	// do something with e.Value
    }
}

Flags